Current field-api have provided hook_field_widget_form_alter and hook_field_widget_WIDGET_TYPE_form_alter, that allow modules to alter the field widget form element. However in case the field's 'Number of values' setting is "Unlimited", hook_field_widget_form_alter only in turn alters widget form element in widget form. I think we should allow other modules to alter the whole field_widget form not only every element in widget form. For example we want to use field set to wrapper elements in stead of table.

http://drupal.org/files/fields_0.png

<?php
function field_multiple_value_form($field, $instance, $langcode, $items, &$form, &$form_state) {
   // code....
    if ($field_elements) {
      $field_elements += array(
        '#theme' => 'field_multiple_value_form',
        '#field_name' => $field['field_name'],
        '#cardinality' => $field['cardinality'],
        '#title' => $title,
        '#required' => $instance['required'],
        '#description' => $description,
        '#prefix' => '<div id="' . $wrapper_id . '">',
        '#suffix' => '</div>',
        '#max_delta' => $max,
      );
      // Add 'add more' button, if not working with a programmed form.
      if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) {
        $field_elements['add_more'] = array(
          '#type' => 'submit',
          '#name' => strtr($id_prefix, '-', '_') . '_add_more',
          '#value' => t('Add another item'),
          '#attributes' => array('class' => array('field-add-more-submit')),
          '#limit_validation_errors' => array(array_merge($parents, array($field_name, $langcode))),
          '#submit' => array('field_add_more_submit'),
          '#ajax' => array(
            'callback' => 'field_add_more_js',
            'wrapper' => $wrapper_id,
            'effect' => 'fade',
          ),
        );
      }
    }
  }

  return $field_elements;
?>

We need change.

<?php
function field_multiple_value_form($field, $instance, $langcode, $items, &$form, &$form_state) {
 //....
 // allow another module alter for widget form elements.
 drupal_alter('field_element_widget_form', $field_elements, $context);
 return $field_elements;
}
?>
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

johnv’s picture

Title: Add hook alter for field_multiple_value_form » Add hook alter to override field_multiple_value_form()

An example is to have a multivalue widget where the user is not allowed to sort the data, and you need to remove the 'Order' column and the drag&drop js.

An alternative method is the following:
- create a MYMODULE_field_multiple_value_form($variables) function in MYMODULE.theme.inc , which overrides the original function. You can copy and modify the original code of theme_field_multiple_value_form;
- add the following in MYMODULE.module:

/**
 * Implements hook_theme().
 */
function MYMODULE_theme ($existing, $type, $theme, $path) {
  $base = array(
    'file' => 'MYMODULE.theme.inc',
    'path' => "$path",
  );
  $themes = array(
    'field_multiple_value_form' => $base + array('render element' => 'element'),
  );
  return $themes;
}
robmalon’s picture

I'm using Drupal 7.17. Discovered a module using this:

function hook_preprocess_field_multiple_value_form(&$variables)

So, I used it myself in one of my own custom modules and it's working well. Maybe that's not -exactly- the same thing as this, but perhaps useful to mention. I didn't find any documentation on implementing this as a hook.

monish_deb’s picture

Same here, didn't find any information related to hook_preprocess_field_multiple_value_form. Does this feature do something like Field Collection module where we can add multiple field elements on choosing multiple values of different widget type ?

rogical’s picture

Status: Active » Needs review
FileSize
7 KB
449 bytes

This is a simple enhancement, I'm using this to add another control button for references thumbnail.

rogical’s picture

FileSize
612 bytes
wwedding’s picture

It's been a while but I thought I'd make a note, since this issue is high in search results and it seems like this is still something a lot of people struggle with.

hook_preprocess_field_multiple_value_form(&$variables) is not a specific undocumented hook but is an implementation of hook_preprocess_HOOK(). The theme being preprocessed is theme_field_multiple_value_form().

The patch in #5 looks good and simple. However, I kind of feel like it would make more sense to refactor the logic that calls field_multiple_value_form() so that it could be modified using existing known hooks like hook_form_alter() or maybe even the widget alter hooks. Adding a specific hook just for this form element seems unnecessary.

wwedding’s picture

Issue summary: View changes

change name of hook_field_alter.

Chi’s picture

Issue summary: View changes
FileSize
987 bytes

field_multiple_value_form() is a huge form. Sometimes it would be useful to be able replace entire form as well. This patch is required for Multiple Value Widget module (7.x-2.x branch).

StephenRobinson’s picture

Ha Ha I came up with a much simpler option so I could set one image field as many in one content type and limit it to one on my other content types:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if(isset($form['#node_edit_form'])){
    if($form_id=='mycontenttype1_node_form'){
      unset($form['field_image']['und'][1]);
    }
  }
}
dillix’s picture

Status: Needs review » Reviewed & tested by the community
wwedding’s picture

Status: Reviewed & tested by the community » Needs review

It's possible I missed an IRC conversation, but it seems there are a couple of different patches that take entirely different approaches and no real feedback about any of them, so I'm not 100% sure what got reviewed and tested.

I'd say that patch #5 is probably the way to go, if anything is going to be committed (and I'm guilty of not submitting any patch alternatives). #7 is an interesting idea but I don't think using variable_set() to control what function builds a form fits expectations created by other Form API patterns, which generally depend on using alter hooks to modify or replace default form structures.

nevergone’s picture

Please review this patch.

asrob’s picture

Status: Needs review » Reviewed & tested by the community

Hi!

I've tested it under simplytest.me with Drupal 7.x. It seems working well so I changed its status to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: 1592814-override-field_multiple_value_form-11.patch, failed testing.

wwedding’s picture

Status: Needs work » Needs review
FileSize
1.91 KB
441 bytes

It passed simpletest in my environment so I'm not sure what that is about.

Tweaking the API documentation slightly and re-testing. Let's see what happens this time!

Status: Needs review » Needs work

The last submitted patch, 14: 1592814-override-field_multiple_value_form-14.patch, failed testing.

wwedding’s picture

Status: Needs work » Needs review

Last re-test passed. Failures seem random and not related. Not switching it directly to RCTB just because I made a (minor) change.

David_Rothstein’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Needs review » Needs work
Issue tags: +Needs backport to D7

Pretty sure this would be relevant for Drupal 8 too, right? (I don't see an equivalent hook or way to alter the whole multivalued form structure there either.)

Quick review in the meantime:

+ *   - "form": The form structure where widgets are being attached to. This
+ *     might be a full form structure, or a sub-element of a larger form.
+ *   - "field": The field structure.
+ *   - "instance": The field instance structure.
+ *   - "langcode": The language associated with $items.
+ *   - "items": Array of default values for this field.

Don't think these should have quotes around them?

+  if ($context['instance']['field_name'] == 'body') {
+    // @todo Needs function body.
+  }

Would be nice to fill that in :)

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

NickDickinsonWilde’s picture

Status: Needs work » Closed (duplicate)

I didn't find this when I search for it back in 2016.... so I made an issue #2822460: No hook to edit Field Widget wrappers created by WidgetBase::formMultipleElements, with a functional patch however without tests. But I got busy and didn't push it/write tests and *another* issue was created #2940201: hook_field_widget_form_alter() can no longer affect the whole widget for multi-value fields with effectively identical functionality + tests. That one got into 8.5.x. So despite this issue being the oldest of the three, closing it as duplicate.