You are here

public static function OptionsShsWidget::afterBuild in Simple hierarchical select 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldWidget/OptionsShsWidget.php \Drupal\shs\Plugin\Field\FieldWidget\OptionsShsWidget::afterBuild()

After-build handler for field elements in a form.

This stores the final location of the field within the form structure so that flagErrors() can assign validation errors to the right form element.

Overrides WidgetBase::afterBuild

File

src/Plugin/Field/FieldWidget/OptionsShsWidget.php, line 217

Class

OptionsShsWidget
Plugin implementation of the 'options_shs' widget.

Namespace

Drupal\shs\Plugin\Field\FieldWidget

Code

public static function afterBuild(array $element, FormStateInterface $form_state) {
  $element = parent::afterBuild($element, $form_state);
  if (empty($element['#shs'])) {

    // Simply return the unaltered element if there is no information attached
    // about SHS (i.e. on field config forms).
    return $element;
  }
  $context = [
    'settings' => empty($element['#shs']['settings']) ? [] : $element['#shs']['settings'],
  ];

  // Create unique key for field.
  $element_key = Html::getUniqueId(sprintf('shs-%s', $element['#field_name']));
  $element['#attributes'] = array_merge($element['#attributes'], [
    'data-shs-selector' => $element_key,
  ]);
  $element['#shs'] += [
    'classes' => shs_get_class_definitions($element['#field_name'], $context),
  ];
  $element['#attached'] = $element['#attached'] ?: [];
  $element['#attached'] = array_merge($element['#attached'], [
    'drupalSettings' => [
      'shs' => [
        $element_key => $element['#shs'],
      ],
    ],
  ]);
  return $element;
}