You are here

protected function ParagraphsWidget::buildButtonsAddMode in Paragraphs 8

Builds dropdown button for adding new paragraph.

Return value

array The form element array.

1 call to ParagraphsWidget::buildButtonsAddMode()
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 1604

Class

ParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

protected function buildButtonsAddMode() {
  $options = $this
    ->getAccessibleOptions();
  $add_mode = $this
    ->getSetting('add_mode');
  $paragraphs_type_storage = \Drupal::entityTypeManager()
    ->getStorage('paragraphs_type');
  foreach ($options as $machine_name => $label) {
    $button_key = 'add_more_button_' . $machine_name;
    $add_more_elements[$button_key] = $this
      ->expandButton([
      '#type' => 'submit',
      '#name' => $this->fieldIdPrefix . '_' . $machine_name . '_add_more',
      '#value' => $add_mode == 'modal' ? $label : $this
        ->t('Add @type', [
        '@type' => $label,
      ]),
      '#attributes' => [
        'class' => [
          'field-add-more-submit',
          'button--small',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($this->fieldParents, [
          $this->fieldDefinition
            ->getName(),
          'add_more',
        ]),
      ],
      '#submit' => [
        [
          get_class($this),
          'addMoreSubmit',
        ],
      ],
      '#ajax' => [
        'callback' => [
          get_class($this),
          'addMoreAjax',
        ],
        'wrapper' => $this->fieldWrapperId,
      ],
      '#bundle_machine_name' => $machine_name,
    ]);
    if ($add_mode === 'modal' && ($icon_url = $paragraphs_type_storage
      ->load($machine_name)
      ->getIconUrl())) {
      $add_more_elements[$button_key]['#attributes']['style'] = 'background-image: url(' . $icon_url . ');';
    }
  }

  // Define the way how buttons are rendered and add them to a container.
  if ($add_mode == 'dropdown') {
    if (count($add_more_elements) > 1) {
      $add_more_elements = $this
        ->buildDropbutton($add_more_elements);
    }
    else {
      $add_more_elements['#attributes']['class'][] = 'paragraphs-dropbutton-wrapper';
    }
    $add_more_elements['#suffix'] = $this
      ->t('to %type', [
      '%type' => $this->fieldDefinition
        ->getLabel(),
    ]);
  }
  elseif ($add_mode == 'modal') {
    $this
      ->buildModalAddForm($add_more_elements);
    $add_more_elements['add_modal_form_area']['#suffix'] = '<span class="paragraphs-add-suffix">' . $this
      ->t('to %type', [
      '%type' => $this->fieldDefinition
        ->getLabel(),
    ]) . '</span>';
  }
  $add_more_elements['#type'] = 'container';
  $add_more_elements['#weight'] = 1;
  return $add_more_elements;
}