You are here

protected function InlineParagraphsWidget::buildButtonsAddMode in Paragraphs 8

Builds dropdown button for adding new paragraph.

Return value

array The form element array.

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

File

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

Class

InlineParagraphsWidget
Plugin implementation of the 'entity_reference paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

protected function buildButtonsAddMode() {

  // Hide the button when translating.
  $add_more_elements = [
    '#type' => 'container',
    '#theme_wrappers' => [
      'paragraphs_dropbutton_wrapper',
    ],
  ];
  $field_name = $this->fieldDefinition
    ->getName();
  $title = $this->fieldDefinition
    ->getLabel();
  $drop_button = FALSE;
  if (count($this
    ->getAccessibleOptions()) > 1 && $this
    ->getSetting('add_mode') == 'dropdown') {
    $drop_button = TRUE;
    $add_more_elements['#theme_wrappers'] = [
      'dropbutton_wrapper',
    ];
    $add_more_elements['prefix'] = [
      '#markup' => '<ul class="dropbutton dropbutton--multiple dropbutton--extrasmall">',
      '#weight' => -999,
    ];
    $add_more_elements['suffix'] = [
      '#markup' => '</ul>',
      '#weight' => 999,
    ];
    $add_more_elements['#suffix'] = $this
      ->t(' to %type', [
      '%type' => $title,
    ]);
  }
  foreach ($this
    ->getAccessibleOptions() as $machine_name => $label) {
    $add_more_elements['add_more_button_' . $machine_name] = [
      '#type' => 'submit',
      '#name' => strtr($this->fieldIdPrefix, '-', '_') . '_' . $machine_name . '_add_more',
      '#value' => $this
        ->t('Add @type', [
        '@type' => $label,
      ]),
      '#attributes' => [
        'class' => [
          'field-add-more-submit',
          'button--small',
        ],
      ],
      '#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',
      ],
      '#bundle_machine_name' => $machine_name,
    ];
    if ($drop_button) {
      $add_more_elements['add_more_button_' . $machine_name]['#prefix'] = '<li class="dropbutton__item dropbutton__item--extrasmall">';
      $add_more_elements['add_more_button_' . $machine_name]['#suffix'] = '</li>';
    }
  }
  return $add_more_elements;
}