You are here

public static function ParagraphsSets::buildSelectSetSelection in Paragraphs Sets 8.2

Builds select element for set selection.

Parameters

array $elements: Form elements to build the selection for.

array $context: Required context for the set selection.

\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 ParagraphsSets::buildSelectSetSelection()
paragraphs_sets_field_widget_multivalue_form_alter in ./paragraphs_sets.module
Implements hook_field_widget_multivalue_form_alter().

File

src/ParagraphsSets.php, line 82

Class

ParagraphsSets
Utitlity class for paragraphs_sets.

Namespace

Drupal\paragraphs_sets

Code

public static function buildSelectSetSelection(array $elements, array $context, FormStateInterface $form_state, $default = NULL) {

  /** @var \Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget $widget */
  $widget = $context['widget'];
  if (!$widget instanceof ParagraphsWidget) {
    return [];
  }
  $settings = $widget
    ->getThirdPartySettings('paragraphs_sets');
  $items = $context['items'];
  $field_definition = $items
    ->getFieldDefinition();
  $field_name = $field_definition
    ->getName();
  $title = $field_definition
    ->getLabel();
  $cardinality = $field_definition
    ->getFieldStorageDefinition()
    ->getCardinality();
  $field_parents = $context['form']['#parents'];
  $field_id_prefix = implode('-', array_merge($field_parents, [
    $field_name,
  ]));
  $field_wrapper_id = Html::getId($field_id_prefix . '-add-more-wrapper');
  $field_state = static::getWidgetState($field_parents, $field_name, $form_state);

  // Get a list of all Paragraphs types allowed in this field.
  $field_allowed_paragraphs_types = $widget
    ->getAllowedTypes($field_definition);
  $options = static::getSetsOptions(array_keys($field_allowed_paragraphs_types), $cardinality);

  // Further limit sets available from widget settings.
  if (isset($settings['paragraphs_sets']['sets_allowed']) && count(array_filter($settings['paragraphs_sets']['sets_allowed']))) {
    $options = array_intersect_key($options, array_filter($settings['paragraphs_sets']['sets_allowed']));
  }
  $options = [
    '_none' => t('- None -'),
  ] + $options;
  $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' => t('@title set', [
      '@title' => $widget
        ->getSetting('title'),
    ]),
    '#label_display' => 'hidden',
  ];
  $selection_elements['set_selection_button'] = [
    '#type' => 'submit',
    '#name' => strtr($field_id_prefix, '-', '_') . '_set_selection',
    '#value' => t('Select set'),
    '#attributes' => [
      'class' => [
        'field-set-selection-submit',
      ],
    ],
    '#limit_validation_errors' => [
      array_merge($field_parents, [
        $field_name,
        'set_selection',
      ]),
    ],
    '#submit' => [
      [
        '\\Drupal\\paragraphs_sets\\ParagraphsSets',
        'setSetSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        '\\Drupal\\paragraphs_sets\\ParagraphsSets',
        'setSetAjax',
      ],
      'wrapper' => $field_wrapper_id,
      'effect' => 'fade',
    ],
  ];
  $selection_elements['set_selection_button']['#prefix'] = '<div class="paragraphs-set-button paragraphs-set-button-set">';
  $selection_elements['set_selection_button']['#suffix'] = t('for %type', [
    '%type' => $title,
  ]) . '</div>';
  if ($field_state['items_count'] && ($field_state['items_count'] < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state
    ->isProgrammed() && (isset($elements['#allow_reference_changes']) && $elements['#allow_reference_changes'])) {
    $selection_elements['append_selection_button'] = [
      '#type' => 'submit',
      '#name' => strtr($field_id_prefix, '-', '_') . '_append_selection',
      '#value' => t('Append set'),
      '#attributes' => [
        'class' => [
          'field-append-selection-submit',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($field_parents, [
          $field_name,
          'append_selection',
        ]),
      ],
      '#submit' => [
        [
          '\\Drupal\\paragraphs_sets\\ParagraphsSets',
          'setSetSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          '\\Drupal\\paragraphs_sets\\ParagraphsSets',
          'setSetAjax',
        ],
        'wrapper' => $field_wrapper_id,
        'effect' => 'fade',
      ],
    ];
    $selection_elements['append_selection_button']['#prefix'] = '<div class="paragraphs-set-button paragraphs-set-button-append">';
    $selection_elements['append_selection_button']['#suffix'] = t('to %type', [
      '%type' => $title,
    ]) . '</div>';
  }
  return $selection_elements;
}