function ViewsSelection::settingsFormValidate

Same name in other branches
  1. 8.9.x core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::settingsFormValidate()
  2. 10 core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::settingsFormValidate()
  3. 11.x core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::settingsFormValidate()

Element validate; Check View is valid.

File

core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php, line 320

Class

ViewsSelection
Plugin implementation of the 'selection' entity_reference.

Namespace

Drupal\views\Plugin\EntityReferenceSelection

Code

public static function settingsFormValidate($element, FormStateInterface $form_state, $form) {
    // Split view name and display name from the 'view_and_display' value.
    if (!empty($element['view_and_display']['#value'])) {
        [
            $view,
            $display,
        ] = explode(':', $element['view_and_display']['#value']);
    }
    else {
        $form_state->setError($element, new TranslatableMarkup('The views entity selection mode requires a view.'));
        return;
    }
    // Explode the 'arguments' string into an actual array. Beware, explode()
    // turns an empty string into an array with one empty string. We'll need an
    // empty array instead.
    $arguments_string = trim($element['arguments']['#value']);
    if ($arguments_string === '') {
        $arguments = [];
    }
    else {
        // array_map() is called to trim whitespaces from the arguments.
        $arguments = array_map('trim', explode(',', $arguments_string));
    }
    $value = [
        'view_name' => $view,
        'display_name' => $display,
        'arguments' => $arguments,
    ];
    $form_state->setValueForElement($element, $value);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.