You are here

protected function EntityReferenceFilterAjaxController::extractViewArgs in Views Reference Filter 8

Extract and convert filter arguments to the actual values.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Request object.

string $dependent_filter_name: Dependent filter name.

array $filters: Views filter handlers.

Return value

array Calculated filter arguments.

1 call to EntityReferenceFilterAjaxController::extractViewArgs()
EntityReferenceFilterAjaxController::ajaxFiltersValuesRebuild in src/Controller/EntityReferenceFilterAjaxController.php
Loads and renders a view via AJAX.

File

src/Controller/EntityReferenceFilterAjaxController.php, line 282

Class

EntityReferenceFilterAjaxController
Defines a controller to build dependent entityreference filters.

Namespace

Drupal\entityreference_filter\Controller

Code

protected function extractViewArgs(Request $request, $dependent_filter_name, array $filters) {
  $args = [];
  $view_data = $request->request
    ->get('view');
  $parent_view_args = $view_data['view_args'] ?? [];
  $parent_view_context_args = $view_data['view_context_args'] ?? [];
  $parent_view_args = Html::decodeEntities($parent_view_args);
  $parent_view_args = isset($parent_view_args) && $parent_view_args !== '' ? explode('/', $parent_view_args) : [];

  // Arguments can be empty, make sure they are passed on as NULL so that
  // argument validation is not triggered.
  $parent_view_args = array_map(static function ($parent_view_arg) {
    return $parent_view_arg === '' ? NULL : $parent_view_arg;
  }, $parent_view_args);
  $reference_arguments = $filters[$dependent_filter_name]['reference_arguments'];
  if (isset($reference_arguments)) {
    $arg_str = trim($reference_arguments);
    if ($arg_str !== '') {
      $args = explode('/', $arg_str);
      foreach ($args as $i => $arg) {
        $arg = trim($arg);
        $first_char = mb_substr($arg, 0, 1);

        // URL argument.
        if ($first_char === '!') {
          $arg_no = (int) mb_substr($arg, 1) - 1;
          if ($arg_no >= 0) {
            $args[$i] = $parent_view_args[$arg_no] ?? NULL;
          }
        }

        // Exposed filter as argument.
        if ($first_char === '[' && mb_substr($arg, -1) === ']') {
          $args[$i] = NULL;

          // Collect expose filters.
          $controlling_filter = mb_substr($arg, 1, -1);
          $controlling_filter_value = $request->request
            ->get($controlling_filter);
          if (empty($filters[$controlling_filter]['exposed'])) {
            continue;
          }
          $args[$i] = !empty($controlling_filter_value) ? $controlling_filter_value : NULL;

          // Glue multiple values.
          if (is_array($args[$i]) && !empty($args[$i])) {
            $args[$i] = implode('+', $args[$i]);
          }
        }

        // Contextual filter as argument.
        if ($first_char === '#' && !empty($parent_view_context_args)) {
          $arg_no = (int) mb_substr($arg, 1) - 1;
          $args[$i] = $parent_view_context_args[$arg_no] ?? NULL;
        }

        // Overwrite empty values to NULL.
        if ($args[$i] === 'All' || $args[$i] === []) {
          $args[$i] = NULL;
        }
      }
    }
  }
  return $args;
}