You are here

protected function ParagraphsWidget::buildModalAddForm in Paragraphs Sets 8

Builds an add paragraph button for opening of modal form.

Parameters

array $element: Render element.

Overrides ParagraphsWidget::buildModalAddForm

File

src/Plugin/Field/FieldWidget/ParagraphsWidget.php, line 394

Class

ParagraphsWidget
Plugin implementation of 'entity_reference_revisions paragraphs sets' widget.

Namespace

Drupal\paragraphs_sets\Plugin\Field\FieldWidget

Code

protected function buildModalAddForm(array &$element) {

  // Attach the theme for the dialog template.
  $element['#theme'] = 'paragraphs_sets_add_dialog';
  $element['#buttons_title'] = $this
    ->getSetting('title');
  $element['add_modal_form_area'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'paragraph-type-add-modal',
        'first-button',
      ],
    ],
    '#access' => !$this->isTranslating,
    '#weight' => -2000,
  ];
  $element['add_modal_form_area']['add_more'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add @title or Set', [
      '@title' => $this
        ->getSetting('title'),
    ]),
    '#name' => 'button_add_modal',
    '#attributes' => [
      'class' => [
        'paragraph-type-add-modal-button',
        'js-show',
      ],
    ],
  ];
  $cardinality = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality();
  foreach (static::getSets() as $machine_name => $set) {
    if ($cardinality !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && count($set['paragraphs']) > $cardinality) {

      // Do not add sets having more paragraphs than allowed.
      continue;
    }
    $button_key = 'append_selection_button_' . $machine_name;
    $element[$button_key] = $this
      ->expandButton([
      '#type' => 'submit',
      '#name' => $this->fieldIdPrefix . '_' . $machine_name . '_set_set',
      '#value' => $set['label'],
      '#attributes' => [
        'class' => [
          'field-add-more-submit',
          'field-append-set-submit',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($this->fieldParents, [
          $this->fieldDefinition
            ->getName(),
          'set_set',
        ]),
      ],
      '#submit' => [
        [
          get_class($this),
          'setSetSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          get_class($this),
          'setSetAjax',
        ],
        'wrapper' => $this->fieldWrapperId,
      ],
      '#set_machine_name' => $machine_name,
    ]);
  }
  $element['#attached']['library'][] = 'paragraphs/drupal.paragraphs.modal';
  $element['#attached']['library'][] = 'paragraphs_sets/drupal.paragraphs_sets.modal';
}