You are here

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

Same name and namespace in other branches
  1. 8.3 src/CshsOptionsFromHelper.php \Drupal\cshs\CshsOptionsFromHelper::settingsForm()
  2. 8 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 146

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,
      // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
      '#description' => $this
        ->t(\implode('<br />', $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) {
    $errors = [];
    $field_storage = $this->fieldDefinition
      ->getFieldStorageDefinition();
    \assert($field_storage instanceof FieldStorageConfigInterface);
    $entity_storage = $this
      ->getStorage($field_storage
      ->getTargetEntityTypeId());
    \assert($entity_storage instanceof FieldableEntityStorageInterface);
    $is_unlimited = $field_storage
      ->getCardinality() === FieldStorageConfigInterface::CARDINALITY_UNLIMITED;
    $description = [
      'Save all parents of the selected term. Please note that you will not have a',
      'familiar field when multiple items can be added via the "Add more" button.',
      'In fact, the field will look like a "single" and the selected terms will',
      'be stored each as a separate field value.',
      '',
      '<b>Keep in mind</b> that this setting cannot be changed once the field storage',
      'gets at least one item. The restriction is imposed across all bundles of',
      'the "' . $entity_storage
        ->getEntityType()
        ->getLabel() . '" entity type.',
    ];
    if ($entity_storage
      ->countFieldData($field_storage, TRUE)) {
      $errors[] = 'There is data for this field in the database. This setting can no longer be changed.';
    }
    if (!$is_unlimited) {
      $errors[] = 'The option can be enabled only for fields with unlimited cardinality.';
    }
    if (!empty($errors)) {
      \array_unshift($description, \sprintf('<b class="color-error">%s</b>', \implode('<br />', $errors)));
    }
    $element['save_lineage'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Save lineage'),
      '#disabled' => !empty($errors),
      // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
      '#description' => $this
        ->t(\implode('<br />', $description)),
      '#default_value' => $is_unlimited && $this
        ->getSetting('save_lineage'),
    ];
  }
  $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['none_label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('The "no selection" label'),
    '#description' => $this
      ->t('The label for an empty option.'),
    '#default_value' => $this
      ->getTranslatedNoneLabel(),
  ];
  $element['#element_validate'][] = [
    $this,
    'validateSettingsForm',
  ];
  $form_state
    ->set('vocabulary', $vocabulary);
  return $element;
}