You are here

public function ParagraphsWidget::settingsForm in 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 WidgetBase::settingsForm

File

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

Class

ParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elements = array();
  $elements['title'] = array(
    '#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'] = array(
    '#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['edit_mode'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Edit mode'),
    '#description' => $this
      ->t('The mode the paragraph is in by default.'),
    '#options' => $this
      ->getSettingOptions('edit_mode'),
    '#default_value' => $this
      ->getSetting('edit_mode'),
    '#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,
    '#states' => [
      'visible' => [
        'select[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][edit_mode]"]' => [
          'value' => 'closed',
        ],
      ],
    ],
  ];
  $elements['closed_mode_threshold'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Closed mode threshold'),
    '#default_value' => $this
      ->getSetting('closed_mode_threshold'),
    '#description' => $this
      ->t('Number of items considered to leave paragraphs open e.g the threshold is 3, if a paragraph has less than 3 items, leave it open.'),
    '#min' => 0,
    '#states' => [
      'invisible' => [
        'select[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][edit_mode]"]' => [
          'value' => 'open',
        ],
      ],
    ],
  ];
  $elements['add_mode'] = array(
    '#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'] = array(
    '#type' => 'select',
    '#options' => \Drupal::service('entity_display.repository')
      ->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,
  );
  $options = [];
  foreach ($this
    ->getAllowedTypes() as $key => $bundle) {
    $options[$key] = $bundle['label'];
  }
  $elements['default_paragraph_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default paragraph type'),
    '#empty_value' => '_none',
    '#default_value' => $this
      ->getDefaultParagraphTypeMachineName(),
    '#options' => $options,
    '#description' => $this
      ->t('When creating a new host entity, a paragraph of this type is added.'),
  ];
  $elements['features'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Enable widget features'),
    '#options' => $this
      ->getSettingOptions('features'),
    '#default_value' => $this
      ->getSetting('features'),
    '#description' => $this
      ->t('When editing, available as action. "Add above" only works in add mode "Modal form"'),
    '#multiple' => TRUE,
  ];
  return $elements;
}