You are here

public static function EntitySelector::valueCallback in Opigno Learning path 3.x

Determines how user input is mapped to an element's #value property.

Parameters

array $element: An associative array containing the properties of the element.

mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

mixed The value to assign to the element.

Overrides Select::valueCallback

File

modules/opigno_user_selection/src/Element/EntitySelector.php, line 48

Class

EntitySelector
Provides a form element for entity selection.

Namespace

Drupal\opigno_user_selection\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  if (!empty($element['#entity_selector_option']) && !empty($element['#entity_selector_parameters']) && ($group = $element['#entity_selector_parameters']['group']) instanceof Group) {

    /** @var \Drupal\opigno_user_selection\UserSelectionHelper $controller */

    /** @var \Drupal\Core\Controller\ControllerResolver $controller_resolver */
    $controller_resolver = \Drupal::service('controller_resolver');
    $callable = $controller_resolver
      ->getControllerFromDefinition($element['#entity_selector_option']);
    list($options, $default) = call_user_func_array($callable, [
      $group,
    ]);
    $element["#options"] = array_map(static function ($value) {
      return $value['value'];
    }, $options);
    if (empty($element['#show_exists'])) {
      $element["#options"] = array_filter($element["#options"], static function ($value, $key) use ($default) {
        return !in_array($key, $default, TRUE);
      }, ARRAY_FILTER_USE_BOTH);
      $element['#default_value'] = [];
    }
    elseif (!is_null($default) && !isset($element['#default_value'])) {
      $element['#default_value'] = $default;
    }
  }
  unset($element["#options"]["_none"]);
  return parent::valueCallback($element, $input, $form_state);
}