You are here

public function TermReferenceTree::settingsForm in Taxonomy Term Reference Tree Widget 8

Same name in this branch
  1. 8 src/Plugin/Field/FieldFormatter/TermReferenceTree.php \Drupal\term_reference_tree\Plugin\Field\FieldFormatter\TermReferenceTree::settingsForm()
  2. 8 src/Plugin/Field/FieldWidget/TermReferenceTree.php \Drupal\term_reference_tree\Plugin\Field\FieldWidget\TermReferenceTree::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 WidgetBase::settingsForm

File

src/Plugin/Field/FieldWidget/TermReferenceTree.php, line 47

Class

TermReferenceTree
Plugin implementation of the 'term_reference_tree' widget.

Namespace

Drupal\term_reference_tree\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $form['start_minimized'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Start minimized'),
    '#description' => $this
      ->t('Make the tree appear minimized on the form by default'),
    '#default_value' => $this
      ->getSetting('start_minimized'),
  ];
  $form['leaves_only'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Leaves only'),
    '#description' => $this
      ->t("Don't allow the user to select items that have children"),
    '#default_value' => $this
      ->getSetting('leaves_only'),
    '#return_value' => 1,
  ];
  $form['select_parents'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Select parents automatically'),
    '#description' => $this
      ->t("When turned on, this option causes the widget to automatically select the ancestors of all selected items. In Leaves Only mode, the parents will be added invisibly to the selected value.  <em>This option is only valid if an unlimited number of values can be selected.</em>"),
    '#default_value' => $this
      ->getSetting('select_parents'),
    '#return_value' => 1,
  ];
  $form['cascading_selection'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Cascading selection'),
    '#description' => $this
      ->t('On parent selection, automatically select children if none were selected. Some may then be manually unselected. In the same way, on parent unselection, unselect children if all were selected. <em>This option is only valid if an unlimited number of values can be selected.</em>'),
    '#default_value' => $this
      ->getSetting('cascading_selection'),
    '#options' => [
      self::CASCADING_SELECTION_NONE => $this
        ->t('None'),
      self::CASCADING_SELECTION_BOTH => $this
        ->t('Select / deselect'),
      self::CASCADING_SELECTION_SELECT => $this
        ->t('Only select'),
      self::CASCADING_SELECTION_DESELECT => $this
        ->t('Only deselect'),
    ],
  ];
  if ($this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality() !== FieldStorageConfig::CARDINALITY_UNLIMITED) {
    $form['select_parents']['#disabled'] = TRUE;
    $form['select_parents']['#default_value'] = FALSE;
    $form['select_parents']['#description'] .= ' <em>' . $this
      ->t("This option is only valid if an unlimited number of values can be selected.") . '</em>';
    $form['cascading_selection']['#disabled'] = TRUE;
    $form['cascading_selection']['#default_value'] = self::CASCADING_SELECTION_NONE;
    $form['cascading_selection']['#description'] .= ' <em>' . $this
      ->t("This option is only valid if an unlimited number of values can be selected.") . '</em>';
  }
  $form['max_depth'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum Depth'),
    '#description' => $this
      ->t("Only show items up to this many levels deep."),
    '#default_value' => $this
      ->getSetting('max_depth'),
    '#min' => 0,
  ];
  return $form;
}