You are here

public function ParagraphViewModeWidget::settingsForm in Paragraph View Mode 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/ParagraphViewModeWidget.php \Drupal\paragraph_view_mode\Plugin\Field\FieldWidget\ParagraphViewModeWidget::settingsForm()

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 StringTextfieldWidget::settingsForm

File

src/Plugin/Field/FieldWidget/ParagraphViewModeWidget.php, line 60

Class

ParagraphViewModeWidget
Plugin implementation of the 'paragraph_view_mode' widget.

Namespace

Drupal\paragraph_view_mode\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element['view_modes'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Available view modes'),
    '#description' => $this
      ->getViewModesFieldDescription(),
    '#options' => $this
      ->defaultSettings()['view_modes'],
    '#default_value' => array_keys($this
      ->getEnabledViewModes()),
    '#required' => FALSE,
    '#ajax' => [
      'callback' => [
        __CLASS__,
        'defaultViewModes',
      ],
      'event' => 'change',
      'wrapper' => 'paragraph-view-mode-default',
    ],
  ];
  if ($this
    ->getSetting('view_modes')) {
    $element['default_view_mode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default value'),
      '#description' => $this
        ->t('View mode to be used as a default field value.'),
      '#options' => $element['view_modes']['#options'],
      '#default_value' => $this
        ->getSetting('default_view_mode'),
      '#required' => FALSE,
      '#weight' => 2,
      '#prefix' => '<div id="paragraph-view-mode-default">',
      '#suffix' => '</div>',
    ];
  }
  return $element;
}