You are here

public function DefaultParagraphsWidget::settingsForm in Default Paragraphs 8

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form definition for the widget settings.

Overrides ParagraphsWidget::settingsForm

File

src/Plugin/Field/FieldWidget/DefaultParagraphsWidget.php, line 120

Class

DefaultParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\default_paragraphs\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elements = [];
  $elements['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Paragraph Title'),
    '#description' => $this
      ->t('Label to appear as title on the button as "Add new [title]", this label is translatable'),
    '#default_value' => $this
      ->getSetting('title'),
    '#required' => TRUE,
  ];
  $elements['title_plural'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Plural Paragraph Title'),
    '#description' => $this
      ->t('Title in its plural form.'),
    '#default_value' => $this
      ->getSetting('title_plural'),
    '#required' => TRUE,
  ];
  $elements['closed_mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Closed mode'),
    '#description' => $this
      ->t('How to display the paragraphs, when the widget is closed. Preview will render the paragraph in the preview view mode and typically needs a custom admin theme.'),
    '#options' => $this
      ->getSettingOptions('closed_mode'),
    '#default_value' => $this
      ->getSetting('closed_mode'),
    '#required' => TRUE,
  ];
  $elements['autocollapse'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Autocollapse'),
    '#description' => $this
      ->t('When a paragraph is opened for editing, close others.'),
    '#options' => $this
      ->getSettingOptions('autocollapse'),
    '#default_value' => $this
      ->getSetting('autocollapse'),
    '#required' => TRUE,
  ];
  $elements['add_mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Add mode'),
    '#description' => $this
      ->t('The way to add new Paragraphs.'),
    '#options' => $this
      ->getSettingOptions('add_mode'),
    '#default_value' => $this
      ->getSetting('add_mode'),
    '#required' => TRUE,
  ];
  $elements['form_display_mode'] = [
    '#type' => 'select',
    '#options' => $this->entityDisplayRepository
      ->getFormModeOptions($this
      ->getFieldSetting('target_type')),
    '#description' => $this
      ->t('The form display mode to use when rendering the paragraph form.'),
    '#title' => $this
      ->t('Form display mode'),
    '#default_value' => $this
      ->getSetting('form_display_mode'),
    '#required' => TRUE,
  ];
  $elements['default_paragraph_types'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Paragraph type'),
      $this
        ->t('Machine name'),
      $this
        ->t('Use as Default'),
      $this
        ->t('Edit mode'),
      $this
        ->t('Weight'),
    ],
    '#empty' => $this
      ->t('There are no items'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'table-sort-weight',
      ],
    ],
    '#element_validate' => [
      [
        $this,
        'settingsFormDefaultParagraphsValidate',
      ],
    ],
  ];

  // We iterate over the allowed paragraph types, if nothing is selected yet.
  $defaults = $this
    ->getSetting('default_paragraph_types');
  $allowed = $this
    ->getAllowedTypes();
  if (!empty($defaults)) {

    // Make sure that defaults array contains all the allowed paragraph types
    // and not only the default ones. The allowed one should be shown at the
    // bottom of the list if they do not exist in the default array.
    foreach ($allowed as $key => $data) {
      if (!isset($defaults[$key])) {
        $defaults[$key] = [
          'value' => 0,
          'weight' => 1000,
        ];
      }
    }
  }
  else {
    $defaults = $allowed;
  }
  foreach ($defaults as $key => $bundle) {
    $elements['default_paragraph_types'][$key] = [
      'name' => [
        '#markup' => $allowed[$key]['label'],
      ],
      'machine_name' => [
        '#markup' => $key,
      ],
      'value' => [
        '#type' => 'checkbox',
        '#default_value' => isset($defaults[$key]['value']) ? $defaults[$key]['value'] : 0,
      ],
      'edit_mode' => [
        '#type' => 'select',
        '#options' => [
          'edit' => $this
            ->t('Open'),
          'closed' => $this
            ->t('Closed'),
        ],
        '#default_value' => isset($defaults[$key]['edit_mode']) ? $defaults[$key]['edit_mode'] : 'closed',
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for @title', [
          '@title' => 'First',
        ]),
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'table-sort-weight',
          ],
        ],
      ],
    ];
    $elements['default_paragraph_types'][$key]['#attributes']['class'][] = 'draggable';
    $elements['default_paragraph_types'][$key]['#weight'] = isset($defaults[$key]['weight']) ? $defaults[$key]['weight'] : 1000;
  }
  return $elements;
}