You are here

public function ParagraphsWidget::buildHeaderActions in Paragraphs 8

Builds header actions.

Parameters

array[] $field_state: Field widget state.

\Drupal\Core\Form\FormStateInterface $form_state: Current form state.

Return value

array[] The form element array.

1 call to ParagraphsWidget::buildHeaderActions()
ParagraphsWidget::formMultipleElements in src/Plugin/Field/FieldWidget/ParagraphsWidget.php
Special handling to create form elements for multiple values.

File

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

Class

ParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

public function buildHeaderActions(array $field_state, FormStateInterface $form_state) {
  $actions = [];
  $field_name = $this->fieldDefinition
    ->getName();
  $id_prefix = implode('-', array_merge($this->fieldParents, [
    $field_name,
  ]));
  if (empty($this->fieldParents)) {

    // Only show the drag&drop mode if we have some items to actually drag
    // around and can find the sortable library.
    $library_discovery = \Drupal::service('library.discovery');
    $library = $library_discovery
      ->getLibraryByName('paragraphs', 'paragraphs-dragdrop');
    if ($this->realItemCount > 0 && ($library || \Drupal::state()
      ->get('paragraphs_test_dragdrop_force_show', FALSE))) {
      $actions['dropdown_actions']['dragdrop_mode'] = $this
        ->expandButton([
        '#type' => 'submit',
        '#name' => $this->fieldIdPrefix . '_dragdrop_mode',
        '#value' => $this
          ->t('Drag & drop'),
        '#attributes' => [
          'class' => [
            'field-dragdrop-mode-submit',
          ],
        ],
        '#submit' => [
          [
            get_class($this),
            'dragDropModeSubmit',
          ],
        ],
        '#weight' => 8,
        '#ajax' => [
          'callback' => [
            get_class($this),
            'dragDropModeAjax',
          ],
          'wrapper' => $this->fieldWrapperId,
        ],
        '#limit_validation_errors' => [
          array_merge($this->fieldParents, [
            $field_name,
            'dragdrop_mode',
          ]),
        ],
        '#access' => $this
          ->allowReferenceChanges(),
      ]);
    }
  }

  // Collapse & expand all.
  if ($this->fieldDefinition
    ->getType() == 'entity_reference_revisions' && $this->realItemCount > 1 && $this
    ->isFeatureEnabled('collapse_edit_all')) {
    $collapse_all = $this
      ->expandButton([
      '#type' => 'submit',
      '#value' => $this
        ->t('Collapse all'),
      '#submit' => [
        [
          get_class($this),
          'changeAllEditModeSubmit',
        ],
      ],
      '#name' => $id_prefix . '_collapse_all',
      '#paragraphs_mode' => 'closed',
      '#limit_validation_errors' => [
        array_merge($this->fieldParents, [
          $field_name,
          'collapse_all',
        ]),
      ],
      '#ajax' => [
        'callback' => [
          get_class($this),
          'allActionsAjax',
        ],
        'wrapper' => $this->fieldWrapperId,
      ],
      '#weight' => -1,
      '#paragraphs_show_warning' => TRUE,
    ]);
    $edit_all = $this
      ->expandButton([
      '#type' => 'submit',
      '#value' => $this
        ->t('Edit all'),
      '#submit' => [
        [
          get_class($this),
          'changeAllEditModeSubmit',
        ],
      ],
      '#name' => $id_prefix . '_edit-all',
      '#paragraphs_mode' => 'edit',
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => [
          get_class($this),
          'allActionsAjax',
        ],
        'wrapper' => $this->fieldWrapperId,
      ],
    ]);

    // Take the default edit mode if we don't have anything in state.
    $mode = isset($field_state['paragraphs'][0]['mode']) ? $field_state['paragraphs'][0]['mode'] : $this->settings['edit_mode'];

    // Depending on the state of the widget output close/edit all in the right
    // order and with the right settings.
    if ($mode === 'closed') {
      $edit_all['#attributes'] = [
        'class' => [
          'paragraphs-icon-button',
          'paragraphs-icon-button-edit',
          'button--extrasmall',
        ],
        'title' => $this
          ->t('Edit all'),
      ];
      $edit_all['#title'] = $this
        ->t('Edit All');
      $actions['actions']['edit_all'] = $edit_all;
      $actions['dropdown_actions']['collapse_all'] = $collapse_all;
    }
    else {
      $collapse_all['#attributes'] = [
        'class' => [
          'paragraphs-icon-button',
          'paragraphs-icon-button-collapse',
          'button--extrasmall',
        ],
        'title' => $this
          ->t('Collapse all'),
      ];
      $actions['actions']['collapse_all'] = $collapse_all;
      $actions['dropdown_actions']['edit_all'] = $edit_all;
    }
  }

  // Add paragraphs_header flag which we use later in preprocessor to move
  // header actions to table header.
  if ($actions) {

    // Set actions.
    $actions['#type'] = 'paragraphs_actions';
    $actions['#paragraphs_header'] = TRUE;
  }
  return $actions;
}