0

We can search all the default views on our relatively new Drupal 8 site, to see what view resides at a particular path, with a query like:

$view_trim_path = 'blog';
$query = $this->entityQuery->get('view')
  ->condition('display.page_1.display_options.path', $view_trim_path)

Unfortunately, this relies upon a hardcoded display name of page_1, which will not hold true in general. In my dreams I would use a wildcard query like

  ->condition('display.page*.display_options.path', ...)

Is there a way to do either of the following?

  1. create a set of entityQuery conditions to find views that have display.*.display_options.path equal to a certain value, no matter what the value of * ... preferably when * begins with the page_ prefix
  2. use another Drupal / View function to look up a view ID from its path (I can't find one)

I can see how to find this by loading the config entities for all views and traversing all those arrays, but my guess is that Drupal has provided a simpler means for this. Also it seems the database abstraction layer would be of use, and if that's the best practice then it would really help me to have an example of how to use it for the above query.

1
  • I think #1 is the option. The * will look for N depth between display and display_options, and there should only be 1 anyway. I think you would want all results anyway because any view that has a path is going to be relevant for your search. Also the operator for condition can be changed to STARTS_WITH, which could be helpful.
    – mradcliffe
    May 15, 2016 at 12:48

1 Answer 1

0

The page prefix is meaningless, it can be changed by the user and can be anything.

So you just want display.*.display_options.path.

It would match on other displays that have a path setting (e.g. REST or feed). If you want to avoid that, you add a second condition on display_plugin = page.

5
  • I swear: before I posted this question I looked through every shred of documentation I could find on entityQuery, and I found not one suggestion that "wildcards" were implemented, so I posted this question in complete ignorance that wildcard queries were even possible. Please @Berdir if you know any official doc that describes the specification or limitations of wildcards, would you post it here? (It is also a great help for you to confirm what I suspected: that the page_1 machine name is arbitrary.)
    – rphair
    May 16, 2016 at 22:08
  • I'm not sure about any docs, there are some docs in the code and I personally would start looking with the change record that adds config entity support for entity query. That is however pretty lacking: drupal.org/node/1902034. It does however link to the issue that added it, where you can find the patch, that should contain examples in the added tests. I'd love more help with writing documentation, would belong somewhere below, drupal.org/developing/api/entity, might even have a page already.
    – Berdir
    May 16, 2016 at 22:50
  • Will document this after I finish practical implementation (menu based breadcrumbs). To complete the answer, how do we reference the data corresponding to that wildcard match? To get the "fuzzy" part of the config I wish I could just load the view entity ($view) and say $view_options = $view->get('display.*.display_options')... sadly (and temporarily) I have hard-coded page_1 back in as an array reference ($view_options = $view_display['page_1']['display_options']). Using PHP preg_grep to search every array key seems too wasteful for production, so again hoping for a Drupal way.
    – rphair
    Jun 5, 2016 at 14:15
  • In my struggle for clarity, can't keep up with 5 minute time limit here: my workaround above, explicitly, is $view_options = $view->get('display')['page_1']['display_options'].
    – rphair
    Jun 5, 2016 at 14:25
  • Finally found relief from this (so no need to respond @Berdir) after accidentally kint()-ing MenuActiveTrail::getActiveLink on a View page, revealing a class ViewsMenuLink that I hadn't seen documented anywhere. This has the structure, properties and methods of MenuLinkContent, sharing parent class MenuLinkBase. So, rather than "wildcard" array references to the array of config entity data, the menu settings for Views can be obtained through this class. Again, the moral: if you run into problems like mine above, you are probably doing things the wrong way.
    – rphair
    Jun 8, 2016 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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