The views arguments always return empty value

Created on 20 March 2023, over 1 year ago

Problem

The passed value for 'views_arguments' of the 'autocomplete_route_parameters' property doesn't execute view with these arguments.

Steps to reproduce

Use an example from the module's README file:
1) Create a view via Structure- > views -> Add new view.
2) Alter a field via hook_form_alter in .module.
or
2) Create an custom field with autocompletion.

// To make it autocomplete, here is an example.

  $form['example_autocomplete'] = [
    '#type' => 'textfield',
    '#autocomplete_route_name' => 'views_autocomplete_api',
    '#autocomplete_route_parameters' => [
        'view_name' => 'my_view_1',
        'display_id' => 'default',
        'views_arguments' => 'args1',
     ],
   ];

Try to find data using autocomplete based on the views contextual filter. The filter is ignored.

Proposed resolution

\Drupal\views_autocomplete_api\Controller\ViewsAutocompleteApiController::getViewsDataJson() executing for views_autocomplete_api route and there we have a code:

    $args_views = $this->viewsAutocompleteManager->prepareArgumentViews(
      $views_arguments,
      count($view_name)
    );

where $views_arguments coming from 'views_arguments' value of a property "autocomplete_route_parameters" that we rewrited before. So, that means we'll pass a value "args1" to prepareArgumentViews() method. Inside of this method we have the next code:

  public function prepareArgumentViews($views_arguments, $count_view) {
    // Prepare arguments.
    $args_views = [];
    if (!empty($views_arguments)) {
      return $args_views;
    }
...

There we have a condition that always returns an empty array when we want to pass a views argument. The result of that is not passed views arguments for views execute.
I propose to update condition to return empty array only when $views_arguments is empty.

πŸ› Bug report
Status

Needs review

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine Brolad

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024