3

I'm converting a module from D7 to D8 for a client. Some of the code is working programmatically with views - something I haven't done that much in D7, since I just write my own queries.

In the code is this line:

$contextual_filter = method_exists($view, 'get_items') ? $view->get_items('argument', $display->id) : 0;

I need to convert this to D8, but I'm not sure entirely what I'm working with here.

What I do know:

  • In D8, $view is an object of type Drupal\views\ViewExecutable
  • In D8, there is no get_items() method, nor a getItems() method on ViewExecutable
  • ViewExecutable has a property, args, and also a property argument

My questions:

  1. How can I get the items from ViewExecutable

  2. How can I get the contextual filter from ViewExecutable

  3. Do I even need to do #1 to get #2?

Further down the code, I can see that $contextual filter is used as follows:

'contextual_filter' => !empty($contextual_filter) ? 1 : 0

So it would appear I'm just trying to get a boolean indicating whether or not a contextual filter exists.

Can anyone who has worked more with D7 Views more than I give me some insight as to what is happening with this code, and ideally how I can update it to D8? (Though insight as to what is happening in D7 may be enough for me to figure it out in D8).

1 Answer 1

3

In D8 the settings of a view are stored in a configuration entity. You can load the entity with View::load() or get it from the view executable directly and then load a display and check the contextual filters (stored in the key arguments of the display options):

$storage = $view->storage;
$display = &$storage->getDisplay('default');
$arguments = $display['display_options']['arguments'];

'contextual_filter' => !empty($arguments) ? 1 : 0
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.