You are here

protected function ParagraphsWidget::buildSelectAddMode in Paragraphs 8

Builds list of actions based on paragraphs type.

Return value

array The form element array.

1 call to ParagraphsWidget::buildSelectAddMode()
ParagraphsWidget::buildAddActions in src/Plugin/Field/FieldWidget/ParagraphsWidget.php
Add 'add more' button, if not working with a programmed form.

File

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

Class

ParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

protected function buildSelectAddMode() {
  $field_name = $this->fieldDefinition
    ->getName();
  $field_title = $this->fieldDefinition
    ->getLabel();
  $setting_title = $this
    ->getSetting('title');
  $select_options = $this
    ->getAccessibleOptions();
  $add_more_elements = [
    '#type' => 'container',
  ];
  $add_more_elements['add_more_select'] = [
    '#type' => 'select',
    '#options' => $select_options,
    '#title' => $this
      ->t('@title type', [
      '@title' => $setting_title,
    ]),
    '#label_display' => 'hidden',
  ];

  // Do not present the select element if only one option is available.
  if (count($select_options) === 1) {
    $add_more_elements['add_more_select']['#type'] = 'value';
    $add_more_elements['add_more_select']['#value'] = key($select_options);
  }
  $text = $this
    ->t('Add @title', [
    '@title' => $setting_title,
  ]);
  if ($this->realItemCount > 0) {
    $text = $this
      ->t('Add another @title', [
      '@title' => $setting_title,
    ]);
  }
  $add_more_elements['add_more_button'] = [
    '#type' => 'submit',
    '#name' => strtr($this->fieldIdPrefix, '-', '_') . '_add_more',
    '#value' => $text,
    '#attributes' => [
      'class' => [
        'field-add-more-submit',
      ],
    ],
    '#limit_validation_errors' => [
      array_merge($this->fieldParents, [
        $field_name,
        'add_more',
      ]),
    ],
    '#submit' => [
      [
        get_class($this),
        'addMoreSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        get_class($this),
        'addMoreAjax',
      ],
      'wrapper' => $this->fieldWrapperId,
      'effect' => 'fade',
    ],
  ];
  $add_more_elements['add_more_button']['#suffix'] = $this
    ->t(' to %type', [
    '%type' => $field_title,
  ]);
  return $add_more_elements;
}