You are here

public function CshsOptionsFromHelper::settingsForm in Client-side Hierarchical Select 8

Same name and namespace in other branches
  1. 8.3 src/CshsOptionsFromHelper.php \Drupal\cshs\CshsOptionsFromHelper::settingsForm()
  2. 8.2 src/CshsOptionsFromHelper.php \Drupal\cshs\CshsOptionsFromHelper::settingsForm()

Returns a form to configure settings.

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 settings.

2 calls to CshsOptionsFromHelper::settingsForm()
CshsTaxonomyIndex::buildExposeForm in src/Plugin/views/filter/CshsTaxonomyIndex.php
CshsWidget::settingsForm in src/Plugin/Field/FieldWidget/CshsWidget.php
Returns a form to configure settings for the widget.

File

src/CshsOptionsFromHelper.php, line 140

Class

CshsOptionsFromHelper
Defines a class for getting options for a cshs form element from vocabulary.

Namespace

Drupal\cshs

Code

public function settingsForm(array $form, FormStateInterface $form_state) : array {
  $vocabulary = $this
    ->getVocabulary();
  \assert($vocabulary !== NULL);
  $options = [];

  // Build options for parent select field.
  foreach ($this
    ->getOptions($vocabulary
    ->id()) as $key => $value) {
    $options[$key] = $value['name'];
  }
  $element['parent'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Parent'),
    '#options' => $options,
    '#description' => $this
      ->t('Select a parent term to use only a subtree of a vocabulary for this field.'),
    '#default_value' => $this
      ->getSetting('parent'),
  ];
  foreach (HIERARCHY_OPTIONS as $option_name => [
    $title,
    $description,
  ]) {
    $description[] = '<i>Ignored when the deepest selection is enforced.</i>';
    $element[$option_name] = [
      '#min' => 0,
      '#type' => 'number',
      '#title' => $title,
      '#description' => $this
        ->t(\implode(' ', $description)),
      '#default_value' => $this
        ->getSetting($option_name),
      '#states' => [
        'disabled' => [
          ':input[name*="force_deepest"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  $element['force_deepest'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force selection of deepest level'),
    '#description' => $this
      ->t('If checked the user will be forced to select terms from the deepest level.'),
    '#default_value' => $this
      ->getSetting('force_deepest'),
  ];

  // This method can be called during Views filter configuration where
  // the "$this->fieldDefinition" is not available. Moreover, we don't
  // need to provide the "save_lineage" there.
  if ($this instanceof WidgetBase) {
    $field_storage = $this->fieldDefinition
      ->getFieldStorageDefinition();
    \assert($field_storage instanceof FieldStorageDefinitionInterface);
    $is_unlimited = $field_storage
      ->getCardinality() === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED;
    $element['save_lineage'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Save lineage'),
      '#description' => $this
        ->t('Save all parents of selected terms. The field must allow an unlimited number of items.'),
      '#default_value' => $is_unlimited && $this
        ->getSetting('save_lineage'),
      '#disabled' => !$is_unlimited,
    ];
  }
  $element['level_labels'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Labels per hierarchy-level'),
    '#description' => $this
      ->t('Enter labels for each hierarchy-level separated by comma.'),
    '#default_value' => $this
      ->getTranslatedLevelLabels(),
  ];
  $element['#element_validate'][] = [
    $this,
    'validateSettingsForm',
  ];
  $form_state
    ->set('vocabulary', $vocabulary);
  return $element;
}