You are here

protected function InlineParagraphsWidget::buildSelectSetSelection in Paragraphs Sets 8

Builds select element for set selection.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $default: Current selected set.

Return value

array The form element array.

1 call to InlineParagraphsWidget::buildSelectSetSelection()
InlineParagraphsWidget::formMultipleElements in src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
Special handling to create form elements for multiple values.

File

src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php, line 288

Class

InlineParagraphsWidget
Plugin definition of the 'entity_reference paragraphs sets' widget.

Namespace

Drupal\paragraphs_sets\Plugin\Field\FieldWidget

Code

protected function buildSelectSetSelection(FormStateInterface $form_state, $default = NULL) {
  $field_name = $this->fieldDefinition
    ->getName();
  $title = $this->fieldDefinition
    ->getLabel();
  $cardinality = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality();
  $options = [
    '_none' => $this
      ->t('- None -'),
  ];
  foreach (static::getSets() as $key => $set) {
    if ($cardinality !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && count($set['paragraphs']) > $cardinality - $this->realItemCount) {

      // Do not add sets having more paragraphs than allowed.
      continue;
    }
    $options[$key] = $set['label'];
  }
  $selection_elements = [
    '#type' => 'container',
    '#theme_wrappers' => [
      'container',
    ],
    '#attributes' => [
      'class' => [
        'set-selection-wrapper',
      ],
    ],
  ];
  $selection_elements['set_selection_select'] = [
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $default,
    '#title' => $this
      ->t('@title set', [
      '@title' => $this
        ->getSetting('title'),
    ]),
    '#label_display' => 'hidden',
  ];
  $selection_elements['set_selection_button'] = [
    '#type' => 'submit',
    '#name' => strtr($this->fieldIdPrefix, '-', '_') . '_set_selection',
    '#value' => $this
      ->t('Select set'),
    '#attributes' => [
      'class' => [
        'field-set-selection-submit',
      ],
    ],
    '#limit_validation_errors' => [
      array_merge($this->fieldParents, [
        $field_name,
        'set_selection',
      ]),
    ],
    '#submit' => [
      [
        get_class($this),
        'setSetSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        get_class($this),
        'setSetAjax',
      ],
      'wrapper' => $this->fieldWrapperId,
      'effect' => 'fade',
    ],
  ];
  $selection_elements['set_selection_button']['#prefix'] = '<div class="paragraphs-set-button paragraphs-set-button-set">';
  $selection_elements['set_selection_button']['#suffix'] = $this
    ->t('for %type', [
    '%type' => $title,
  ]) . '</div>';
  if ($this->realItemCount && ($this->realItemCount < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state
    ->isProgrammed() && !$this->isTranslating) {
    $selection_elements['append_selection_button'] = [
      '#type' => 'submit',
      '#name' => strtr($this->fieldIdPrefix, '-', '_') . '_append_selection',
      '#value' => $this
        ->t('Append set'),
      '#attributes' => [
        'class' => [
          'field-append-selection-submit',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($this->fieldParents, [
          $field_name,
          'append_selection',
        ]),
      ],
      '#submit' => [
        [
          get_class($this),
          'setSetSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          get_class($this),
          'setSetAjax',
        ],
        'wrapper' => $this->fieldWrapperId,
        'effect' => 'fade',
      ],
    ];
    $selection_elements['append_selection_button']['#prefix'] = '<div class="paragraphs-set-button paragraphs-set-button-append">';
    $selection_elements['append_selection_button']['#suffix'] = $this
      ->t('to %type', [
      '%type' => $title,
    ]) . '</div>';
  }
  return $selection_elements;
}