You are here

public static function TaxonomyManagerTree::valueCallback in Taxonomy Manager 2.0.x

Same name and namespace in other branches
  1. 8 src/Element/TaxonomyManagerTree.php \Drupal\taxonomy_manager\Element\TaxonomyManagerTree::valueCallback()

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 FormElement::valueCallback

File

src/Element/TaxonomyManagerTree.php, line 70

Class

TaxonomyManagerTree
Taxonomy Manager Tree Form Element.

Namespace

Drupal\taxonomy_manager\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {

  // Validate that all submitted terms belong to the original vocabulary and
  // are not faked via manual $_POST changes.
  $selected_terms = [];
  if (is_array($input) && !empty($input)) {
    foreach ($input as $tid) {
      $term = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->load($tid);
      if ($term && $term
        ->bundle() == $element['#vocabulary']) {
        $selected_terms[] = $tid;
      }
    }
  }
  return $selected_terms;
}