9

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?

  • 2
    To clarify, you want there to be no empty field presented upon edit? So to add another field, they would be forced to use the "Add another item" button? – tim.plunkett Sep 14 '11 at 15:32
  • Yes, you understood it perfectly. – Fidelix Sep 14 '11 at 20:43
  • @Fidelix, did you find a module to do this, or only custom code at this point? – David Barratt Mar 19 '15 at 19:08
  • @DavidBarratt only custom code, I'm afraid. – Fidelix Mar 20 '15 at 2:01
10
+50

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]);
}
| improve this answer | |
  • Is it possible to "Add" a new empty field in this hook? – Fidelix Sep 14 '11 at 20:45
  • Theoretically, you could. Something like $form[$field_name][$field_language][$max_delta + 1] = $form[$field_name][$field_language][$max_delta]; might work. – tim.plunkett Sep 15 '11 at 2:45
  • There is a problem in this solution. If there is no value and max delta is 0 and you click on add another item, it will not add extra field. then when you click again on add another item then it will add extra field. It means double click required on add another item ff the node has no value against that field. – Ahmad Nov 9 '17 at 13:19
3

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]);

        }
      }
    }

  }
}
| improve this answer | |
  • I was just thinking you could check for cardinality reading the top answer then saw you did it! – jowan sebastian Nov 27 '14 at 21:47
  • To avoid removing the only field input (if the field is not required): if ($max_delta > 0) { unset($form[$field_name][$field_language][$max_delta]); } – quotesBro May 2 '17 at 5:35
2

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;
}
| improve this answer | |
0

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.

| improve this answer | |
  • My comment about your CSS solution still stands: why do this when you could do it with PHP? See drupal.stackexchange.com/questions/10481/… – tim.plunkett Sep 14 '11 at 4:59
  • Hello Tim, I can understand your point of view, and I agree in some cases. If am not wrong, I guess that CCK file fields are using javascript to show new fields, and is a good solution (If you disable this feature, or not have javascript, the backend/php way is possible like second alternative). Also, I think that is good to know alternatives to solve the same requirement. – cigotete Sep 14 '11 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.