You are here

function multifield_field_widget_form in Multifield 7

Same name and namespace in other branches
  1. 7.2 multifield.field.inc \multifield_field_widget_form()

Implements hook_field_widget_form().

File

./multifield.field.inc, line 544
Field integration for the Multifield module.

Code

function multifield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $machine_name = multifield_extract_multifield_machine_name($field);
  $item = isset($items[$delta]) ? $items[$delta] : array();
  $entity = $element['#entity'];
  $is_default_value_widget = empty($entity);
  if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && !empty($field['settings']['hide_blank_items'])) {
    $field_state = field_form_get_state($element['#field_parents'], $element['#field_name'], $element['#language'], $form_state);
    if ($delta == $field_state['items_count'] && $delta > 0) {

      // Do not add a blank item. Also see multifield_field_attach_form() for
      // correcting #max_delta.
      return FALSE;
    }
    elseif ($field_state['items_count'] == 0) {

      // We show one item, so also specify that as item count. So when the
      // add button is pressed the item count will be 2 and we show to items.
      $field_state['items_count'] = 1;
    }
    field_form_set_state($element['#field_parents'], $element['#field_name'], $element['#language'], $form_state, $field_state);
  }
  $element['#parents'] = array_merge($element['#field_parents'], array(
    $element['#field_name'],
    $element['#language'],
    $element['#delta'],
  ));

  // Force the ID of the pseudo entity to be NULL, to prevent modules like
  // entity reference from trying to use it.
  $pseudo_entity = _multifield_field_item_to_entity($machine_name, $item);
  $pseudo_entity->id = NULL;

  // Rather than calling field_attach_form() here, we have to limit these
  // sub-field widgets to only one cardinality value. So manually invoke
  // field_default_form() for each one.
  foreach (field_info_instances('multifield', $machine_name) as $subfield_name => $subinstance) {
    $subfield = field_info_field($subfield_name);
    $subfield['cardinality'] = 1;

    // If a subfield is required, but this is not the first delta, or this
    // widget it being used in the default value form for the multifield, then
    // disable the subfield's requirement flag.
    if ($subinstance['required'] && ($delta > 0 || $is_default_value_widget)) {
      $subinstance['required'] = 0;
    }
    $subitems = isset($pseudo_entity->{$subfield_name}[LANGUAGE_NONE]) ? $pseudo_entity->{$subfield_name}[LANGUAGE_NONE] : array();
    $element += field_default_form('multifield', $pseudo_entity, $subfield, $subinstance, LANGUAGE_NONE, $subitems, $element, $form_state, 0);
  }

  // If this multifield itself has a cardinality of one value, and this is not
  // being used for the field default value form, then set the wrapping element
  // to be a fieldset for display/grouping purposes.
  if ($field['cardinality'] == 1 && !empty($element['#entity'])) {
    $element['#type'] = 'fieldset';
  }
  $element['id'] = array(
    '#type' => 'value',
    '#value' => !empty($item['id']) ? $item['id'] : NULL,
  );
  $element['actions'] = array(
    '#type' => 'actions',
    //'#attached' => array(

    //  'css' => array(
    //    drupal_get_path('module', 'multifield') . '/multifield.field.css' => array(),
    //  ),

    //),
    '#pre_render' => array(
      '_multifield_show_only_if_visible_children',
    ),
  );
  $element['actions']['remove_button'] = array(
    '#type' => 'submit',
    '#value' => t('Remove'),
    '#name' => implode('_', $element['#parents']) . '_remove_button',
    '#delta' => $delta,
    '#submit' => array(
      'multifield_field_widget_remove_item_submit',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'path' => 'multifield/field-remove-item/ajax',
      'effect' => 'fade',
    ),
    '#attributes' => array(
      'class' => array(
        'remove-button',
        'delta-' . $delta,
      ),
    ),
    '#access' => $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && !$is_default_value_widget,
  );
  _multifield_field_attach_form('multifield', $pseudo_entity, $element, $form_state, LANGUAGE_NONE);

  // Restore the internal entity type and bundle properties.
  $element['#entity_type'] = $instance['entity_type'];
  $element['#entity'] = $entity;
  $element['#bundle'] = $instance['bundle'];

  //$form['#validate'][] = 'multifield_field_widget_validate';

  //$form['#multifields'][] = $element['#parents'];
  return $element;
}