Unlimited values field - how to hide extra field on node edit

by Fidelix   Last Updated December 28, 2017 20:07 PM

When creating a new node, I have this unlimited values field with the "Add new item" button.

Unlimited Values field - New Node

That's OK, wonderful. But when I edit an existing node, I get an UNDESIRED empty extra value added for me: Unlimited Values field - Edit Node

My real case is with a big compound field, but I thought using this small field as an example would be easier to understand.

I there any way to prevent this "extra" field from being intrusively added?

Tags : 7 fields forms


Answers 5


You could write your own module with a hook_form_FORM_ID_alter to remove the unwanted field. Something like...

function my_module_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  // Do checks here to make sure this is an existing node...

  // Get the index of the last input element in this field
  $last_index = $form['field_field_name'][LANGUAGE_NONE]['#max_delta'];
  // Get rid of the last input element
  unset($form['field_field_name'][LANGUAGE_NONE][$last_index]);
  // Move back the last index so that any new elements are added correctly
  $form['field_field_name'][LANGUAGE_NONE]['#max_delta'] = $last_index-1;
}
nmc
nmc
September 13, 2011 23:47 PM

This would work for all node edit forms:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function MYMODULE_form_node_form_alter(&$form, &$form_state, $form_id) {
  $field_name = 'field_YOURFIELD';

  if (empty($form[$field_name])) {
    return;
  }

  $field_language = $form[$field_name]['#language'];
  $max_delta = $form[$field_name][$field_language]['#max_delta'];
  unset($form[$field_name][$field_language][$max_delta]);
}
tim.plunkett
tim.plunkett
September 14, 2011 01:08 AM

if from a backend perspective is not possible for now, an alternative could be the following Jquery. This code will remove only the first target when is loaded the page, and only will remove the target when the user will add files, so you will not have problems when you already have files:

<script>
if(!$('div.filefield-file-info').length){
$('table#field_test_file_values tr.even:eq(0)').remove();
}
</script>

(put before the body closing tag of your page.tpl.php)

According my test, the images are uploaded without problems, and when you edit the node, all files are listed.

note: to this example 'field_test_file' is the name of the CCK field, so remember to change according the name of your cck field.

cigotete
cigotete
September 14, 2011 03:25 AM

This would work for all node edit forms without even specifying the field names.

/**
 * Implements hook_form_NODE_FORM_alter().
 */
function MY_MODULE_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node']->nid) && $form['#node']->nid > 0) {
    $form_fields = array_keys($form);

    foreach ($form_fields as $index => $field_name) {
      if (substr($field_name, 0, 6) == "field_") { // Check if the field is a custom field
        $field_language = $form[$field_name]['#language'];
        if (isset($form[$field_name][$field_language]['#cardinality']) && 
            $form[$field_name][$form[$field_name]['#language']]['#cardinality'] == -1) {

          if (empty($form[$field_name])) {
            continue;
          }

          $max_delta = $form[$field_name][$field_language]['#max_delta'];
          unset($form[$field_name][$field_language][$max_delta]);

        }
      }
    }

  }
}
Vikas
Vikas
November 08, 2013 11:54 AM

What would change for this in drupal 8?

Brad Flewelling
Brad Flewelling
December 28, 2017 19:23 PM

Related Questions





Error before executing the submit handler

Updated June 13, 2016 08:03 AM