You are here

public static function BynderSelect2SimpleElement::processBynderSelect2 in Bynder 8

Same name and namespace in other branches
  1. 8.3 modules/bynder_select2/src/Element/BynderSelect2SimpleElement.php \Drupal\bynder_select2\Element\BynderSelect2SimpleElement::processBynderSelect2()
  2. 8.2 modules/bynder_select2/src/Element/BynderSelect2SimpleElement.php \Drupal\bynder_select2\Element\BynderSelect2SimpleElement::processBynderSelect2()
  3. 4.0.x modules/bynder_select2/src/Element/BynderSelect2SimpleElement.php \Drupal\bynder_select2\Element\BynderSelect2SimpleElement::processBynderSelect2()

Processes a bynder_select2 form element.

File

modules/bynder_select2/src/Element/BynderSelect2SimpleElement.php, line 32

Class

BynderSelect2SimpleElement
Provides a simple bynder_select2 form element.

Namespace

Drupal\bynder_select2\Element

Code

public static function processBynderSelect2(&$element, FormStateInterface $form_state, &$complete_form) {
  $element = parent::processSelect($element, $form_state, $complete_form);
  $class = 'bynder-select2-' . hash('md5', Html::getUniqueId('bynder-select2-simple-element'));
  $base_url = \Drupal::request()
    ->getSchemeAndHttpHost();
  $element['#attributes']['class'][] = $class;
  $select2_settings = [
    'selector' => '.' . $class,
    'placeholder_text' => $element['#placeholder_text'],
    'multiple' => $element['#multiple'],
    'base_url' => $base_url,
  ];
  if (isset($element['#loadRemoteData'])) {
    $select2_settings['loadRemoteData'] = [
      'url' => $base_url . $element['#loadRemoteData'],
    ];
  }
  $element['#attached']['drupalSettings']['bynder_select2'][$class] = $select2_settings;
  $element['#attached']['library'] = [
    'bynder_select2/bynder_select2.widget',
  ];
  return $element;
}