protected function FileWidget::formMultipleElements

Overrides \Drupal\field\Plugin\Type\Widget\WidgetBase::formMultipleElements().

Special handling for draggable multiple widgets and 'add more' button.

Overrides WidgetBase::formMultipleElements

1 call to FileWidget::formMultipleElements()
ImageWidget::formMultipleElements in drupal/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php
Overrides \Drupal\file\Plugin\field\widget\FileWidget::formMultipleElements().
1 method overrides FileWidget::formMultipleElements()
ImageWidget::formMultipleElements in drupal/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php
Overrides \Drupal\file\Plugin\field\widget\FileWidget::formMultipleElements().

File

drupal/core/modules/file/lib/Drupal/file/Plugin/field/widget/FileWidget.php, line 57
Contains \Drupal\file\Plugin\field\widget\FileWidget.

Class

FileWidget
Plugin implementation of the 'file_generic' widget.

Namespace

Drupal\file\Plugin\field\widget

Code

protected function formMultipleElements(EntityInterface $entity, array $items, $langcode, array &$form, array &$form_state) {
  $field = $this->field;
  $instance = $this->instance;
  $field_name = $field['field_name'];
  $parents = $form['#parents'];

  // Load the items for form rebuilds from the field state as they might not be
  // in $form_state['values'] because of validation limitations. Also, they are
  // only passed in as $items when editing existing entities.
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
  if (isset($field_state['items'])) {
    $items = $field_state['items'];
  }

  // Determine the number of widgets to display.
  switch ($field['cardinality']) {
    case FIELD_CARDINALITY_UNLIMITED:
      $max = count($items);
      $is_multiple = TRUE;
      break;
    default:
      $max = $field['cardinality'] - 1;
      $is_multiple = $field['cardinality'] > 1;
      break;
  }
  $id_prefix = implode('-', array_merge($parents, array(
    $field_name,
  )));
  $wrapper_id = drupal_html_id($id_prefix . '-add-more-wrapper');
  $title = check_plain($instance['label']);
  $description = field_filter_xss($instance['description']);
  $elements = array();
  $delta = 0;

  // Add an element for every existing item.
  foreach ($items as $item) {
    $element = array(
      '#title' => $title,
      '#description' => $description,
    );
    $element = $this
      ->formSingleElement($entity, $items, $delta, $langcode, $element, $form, $form_state);
    if ($element) {

      // Input field for the delta (drag-n-drop reordering).
      if ($is_multiple) {

        // We name the element '_weight' to avoid clashing with elements
        // defined by widget.
        $element['_weight'] = array(
          '#type' => 'weight',
          '#title' => t('Weight for row @number', array(
            '@number' => $delta + 1,
          )),
          '#title_display' => 'invisible',
          // Note: this 'delta' is the FAPI #type 'weight' element's property.
          '#delta' => $max,
          '#default_value' => isset($item['_weight']) ? $item['_weight'] : $delta,
          '#weight' => 100,
        );
      }
      $elements[$delta] = $element;
      $delta++;
    }
  }
  $empty_single_allowed = $this->field['cardinality'] == 1 && $delta == 0;
  $empty_multiple_allowed = ($this->field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta < $this->field['cardinality']) && empty($form_state['programmed']);

  // Add one more empty row for new uploads except when this is a programmed
  // multiple form as it is not necessary.
  if ($empty_single_allowed || $empty_multiple_allowed) {
    $element = array(
      '#title' => $title,
      '#description' => $description,
    );
    $element = $this
      ->formSingleElement($entity, $items, $delta, $langcode, $element, $form, $form_state);
    if ($element) {
      $element['#required'] = $element['#required'] && $delta == 0;
      $elements[$delta] = $element;
    }
  }
  if ($is_multiple) {

    // The group of elements all-together need some extra functionality after
    // building up the full list (like draggable table rows).
    $elements['#file_upload_delta'] = $delta;
    $elements['#theme'] = 'file_widget_multiple';
    $elements['#theme_wrappers'] = array(
      'details',
    );
    $elements['#process'] = array(
      'file_field_widget_process_multiple',
    );
    $elements['#title'] = $title;
    $elements['#description'] = $description;
    $elements['#field_name'] = $element['#field_name'];
    $elements['#language'] = $element['#language'];
    $elements['#display_field'] = !empty($this->field['settings']['display_field']);

    // Add some properties that will eventually be added to the file upload
    // field. These are added here so that they may be referenced easily
    // through a hook_form_alter().
    $elements['#file_upload_title'] = t('Add a new file');
    $elements['#file_upload_description'] = theme('file_upload_help', array(
      'description' => '',
      'upload_validators' => $elements[0]['#upload_validators'],
    ));
  }
  return $elements;
}