1

When I have a node on the main menu, I can find its Menu Settings with an entityQuery on the content entity for its menu link, for example:

$system_uri = "entity:node/42";
$query = \Drupal::entityQuery('menu_link_content')
  ->condition('menu_name', 'main')
  ->condition('link.uri', $system_uri);
$result = $query->execute();
$menu_link_id = reset($result);
$menu_link = MenuLinkContent::load($menu_link_id);

... then get the Menu Link Title and Parent Item with these methods from the relevant class:

$menu_link_title = $menu_link->getTitle();
$menu_link_parent = $menu_link->getParentId();

I can't do the same thing with a view. Even an entityQuery of all menu_link_content entities on the main menu doesn't return views that are on that menu: only nodes and taxonomy term entities.

And I can't see these settings in what I believed would be the corresponding configuration entity for a View, for example:

$view_id = 'frontpage';
$query = \Drupal::entityQuery('view')
  ->condition('id', $view_id);
$result = $query->execute();
$view_id = reset($result);
$view = \Drupal::service('entity.manager')->getStorage('view')->load($view_id);

The resulting Drupal\views\Entity\View object doesn't look like it has any properties or methods to access the configuration data in the Views UI, specifically the Menu Link Title and Parent under Page Settings. What is the correct way to extract this information?

1 Answer 1

0

The menu tree is built from two sources, menu link content and menu config. For views you find the menu configuration in each view. There are several ways to access this information. You can use the config api to read the values in php, export the view to a yml file and inspect it or use drush.

For example to show the view for block content with drush:

drush cedit views.view.block_content

This contains this configuration for the page:

 page_1:
    display_plugin: page
    id: page_1
    display_title: Page
    position: 1
    display_options:
      display_extenders: {  }
      path: admin/structure/block/block-content
      menu:
        type: tab
        title: 'Custom block library'
        description: ''
        parent: block.admin_display
        weight: 0
        context: '0'
        menu_name: admin
    cache_metadata:
      max-age: 0
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - url.query_args
        - user.permissions
      tags: {  }

After inspection the structure of this array you can use the configuration api to access it in php:

Simple Configuration API

4
  • I am really happy to know the proper way of doing this & displaying that configuration object. But I also see that $view->get('display') after my entityQuery gives exactly the same result as $config = \Drupal::config('views.view.frontpage'); $config->get('display'). Is there a general reason why we should use the Simple Config API rather than entityQuery, other than simplicity? For instance it would be nice to use \Drupal::entityQuery('view')->condition('display.page_1.display_options.menu.menu_name', 'main') to get all the views on the main menu.
    – rphair
    May 14, 2016 at 9:38
  • I assumed from your question you were not able to find the menu links with entityQuery. The method I described was how to locate the information quickly, if you don't have any clue how this config data is organized. A module as Views can do this in any way it likes to feed menu links or routes dynamically. After you know where to look for the data, I would prefer entityQuery for object orientated coding inside drupal and the config api to organize the configuration in general.
    – 4uk4
    May 14, 2016 at 12:58
  • You were right, I was not able to find them in the kint() of that configuration object... I only came up with that query after being sure of the array hierarchy based on the output of drush cget views.view.frontpage. I'd also seen the menu data listed in views.display.schema.yml, and knew theoretically that views were config entities, but didn't put the whole thing together until you sent that link about the Simple Config API. thanks /r
    – rphair
    May 14, 2016 at 19:35
  • I have a related question about how to get around the presence of the page_1 term in the array hierarchy above. I've realised we can't rely on this, and guessing it's not as simple as using a "wildcard". I've created the new post since it's beyond the scope of the original question here.
    – rphair
    May 15, 2016 at 11:32

Your Answer

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

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