You are here

public function ParagraphsClassicAsymmetricWidget::formMultipleElements in Paragraphs asymmetric translation widgets 8

Special handling to create form elements for multiple values.

Handles generic features for multiple fields:

  • number of widgets
  • AHAH-'add more' button
  • table display and drag-n-drop value reordering

Overrides InlineParagraphsWidget::formMultipleElements

File

src/Plugin/Field/FieldWidget/ParagraphsClassicAsymmetricWidget.php, line 571

Class

ParagraphsClassicAsymmetricWidget
Plugin implementation of the 'paragraphs_classic_asymmetric' widget.

Namespace

Drupal\paragraphs_asymmetric_translation_widgets\Plugin\Field\FieldWidget

Code

public function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
  $field_name = $this->fieldDefinition
    ->getName();
  $cardinality = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality();
  $this->fieldParents = $form['#parents'];
  $field_state = static::getWidgetState($this->fieldParents, $field_name, $form_state);
  $max = $field_state['items_count'];
  $entity_type_manager = \Drupal::entityTypeManager();

  // Consider adding a default paragraph for new host entities.
  if ($max == 0 && $items
    ->getEntity()
    ->isNew()) {
    $default_type = $this
      ->getDefaultParagraphTypeMachineName();

    // Checking if default_type is not none and if is allowed.
    if ($default_type) {

      // Place the default paragraph.
      $target_type = $this
        ->getFieldSetting('target_type');
      $paragraphs_entity = $entity_type_manager
        ->getStorage($target_type)
        ->create([
        'type' => $default_type,
      ]);
      $paragraphs_entity
        ->setParentEntity($items
        ->getEntity(), $field_name);
      $field_state['selected_bundle'] = $default_type;
      $display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this
        ->getSetting('form_display_mode'));
      $field_state['paragraphs'][0] = [
        'entity' => $paragraphs_entity,
        'display' => $display,
        'mode' => 'edit',
        'original_delta' => 1,
      ];
      $max = 1;
      $field_state['items_count'] = $max;
    }
  }
  $this->realItemCount = $max;
  $is_multiple = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->isMultiple();
  $title = $this->fieldDefinition
    ->getLabel();
  $description = FieldFilteredMarkup::create(\Drupal::token()
    ->replace($this->fieldDefinition
    ->getDescription()));
  $elements = array();
  $this->fieldIdPrefix = implode('-', array_merge($this->fieldParents, array(
    $field_name,
  )));
  $this->fieldWrapperId = Html::getUniqueId($this->fieldIdPrefix . '-add-more-wrapper');
  $elements['#prefix'] = '<div id="' . $this->fieldWrapperId . '">';
  $elements['#suffix'] = '</div>';
  $field_state['ajax_wrapper_id'] = $this->fieldWrapperId;

  // Persist the widget state so formElement() can access it.
  static::setWidgetState($this->fieldParents, $field_name, $form_state, $field_state);
  if ($max > 0) {
    for ($delta = 0; $delta < $max; $delta++) {

      // Add a new empty item if it doesn't exist yet at this delta.
      if (!isset($items[$delta])) {
        $items
          ->appendItem();
      }

      // For multiple fields, title and description are handled by the wrapping
      // table.
      $element = array(
        '#title' => $is_multiple ? '' : $title,
        '#description' => $is_multiple ? '' : $description,
      );
      $element = $this
        ->formSingleElement($items, $delta, $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' => $this
              ->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' => $items[$delta]->_weight ?: $delta,
            '#weight' => 100,
          );
        }

        // Access for the top element is set to FALSE only when the paragraph
        // was removed. A paragraphs that a user can not edit has access on
        // lower level.
        if (isset($element['#access']) && !$element['#access']) {
          $this->realItemCount--;
        }
        else {
          $elements[$delta] = $element;
        }
      }
    }
  }
  $field_state = static::getWidgetState($this->fieldParents, $field_name, $form_state);
  $field_state['real_item_count'] = $this->realItemCount;
  static::setWidgetState($this->fieldParents, $field_name, $form_state, $field_state);
  $elements += [
    '#element_validate' => [
      [
        $this,
        'multipleElementValidate',
      ],
    ],
    '#required' => $this->fieldDefinition
      ->isRequired(),
    '#field_name' => $field_name,
    '#cardinality' => $cardinality,
    '#max_delta' => $max - 1,
  ];
  if ($this->realItemCount > 0) {
    $elements += array(
      '#theme' => 'field_multiple_value_form',
      '#cardinality_multiple' => $is_multiple,
      '#title' => $title,
      '#description' => $description,
    );
  }
  else {
    $classes = $this->fieldDefinition
      ->isRequired() ? [
      'form-required',
    ] : [];
    $elements += [
      '#type' => 'container',
      '#theme_wrappers' => [
        'container',
      ],
      '#cardinality_multiple' => TRUE,
      'title' => [
        '#type' => 'html_tag',
        '#tag' => 'strong',
        '#value' => $title,
        '#attributes' => [
          'class' => $classes,
        ],
      ],
      'text' => [
        '#type' => 'container',
        'value' => [
          '#markup' => $this
            ->t('No @title added yet.', [
            '@title' => $this
              ->getSetting('title'),
          ]),
          '#prefix' => '<em>',
          '#suffix' => '</em>',
        ],
      ],
    ];
    if ($this->fieldDefinition
      ->isRequired()) {
      $elements['title']['#attributes']['class'][] = 'form-required';
    }
    if ($description) {
      $elements['description'] = [
        '#type' => 'container',
        'value' => [
          '#markup' => $description,
        ],
        '#attributes' => [
          'class' => [
            'description',
          ],
        ],
      ];
    }
  }
  $host = $items
    ->getEntity();
  $this
    ->initIsTranslating($form_state, $host);
  if (($this->realItemCount < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state
    ->isProgrammed() && (!$this->isTranslating || $this->fieldDefinition
    ->isTranslatable())) {
    $elements['add_more'] = $this
      ->buildAddActions();
  }
  $elements['#attached']['library'][] = 'paragraphs_asymmetric_translation_widgets/drupal.paragraphs_asymmetric_translation_widgets.widget';
  return $elements;
}