Score:0

Show links for all the views in the administrator menu

in flag

With the admin toolbar enabled, one of the things I miss from Drupal 7 is hovering through the administrator menu on Structure >> Views and seeing a list of all views and being able to click on one to edit it.

See this screenshot for the current behavior.

screenshot

Is there a way to provide this feature from a custom module? I know I can get a list of all views with Views::getAllViews() but I am not sure how to add them programmatically. If I use a custom module, and use hook_update_n() to add to the menu, what happens when a new view is added? Would it automatically be updated with the added view, or just list the ones that were present when the module was installed? If not, how can this be done to always show the current views list?

Kevin avatar
in flag
Why not install admin toolbar module?
Jaypan avatar
de flag
I don't get the question - when I go to admin -> structure -> views on a vanilla Drupal installation, I see a list of all views.
pglatz avatar
in flag
I do have admin toolbar installed. I just did a vanilla 9.4 install to verify. When I click on structure/views the only option is "Add view" -- I guess I didn't make my question clear enough; what I want to do is hover over structure/views and see the list of views as edit links, without having to go to the views page itself. That's the way 7 did it, and it saves me a page load. I always liked it.
pglatz avatar
in flag
So my question is, how do I append these links to the menu and have them list any views I may have created after I install my code to create the menu links? If I use hook_install() I'm guessing the links would be just the list at the time I installed it. I guess it would need to be dynamically on each page load, if the logged in user has admin rights.
Jaypan avatar
de flag
The default in Drupal 8/9 is to show the list of views at admin/structure/views, as you describe. If you're not seeing it, some module or something on your system is breaking that.
Jaypan avatar
de flag
Do you have the Views UI module installed? It's part of core, but is required to provide the admin -> structure -> views page.
berliner avatar
bd flag
I can confirm what pglatz describes. He is indeed talking about the admin toolbar dropdown menu, which only shows the "Add view" menu item when hovering through the menu structure. His question is **not** about the views listing page itself.
Jaypan avatar
de flag
Now I get it, thanks for the clarification.
Score:2
de flag

You can add them yourself in a custom module.

[MODULE].links.menu.yml:

[MODULE].views.list:
  deriver: 'Drupal\[MODULE]\Plugin\Derivative\ViewMenuLinks'

[MODULE]/src/Plugin/Derivative/ViewMenuLinks.php:

<?php

namespace Drupal\[MODULE]\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides menu links for individual views.
 */
class ViewMenuLinks extends DeriverBase implements ContainerDeriverInterface {

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static();
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = [];

    foreach (Views::getAllViews() as $view_id => $view) {
      $url = $view->toUrl();
      $this->derivatives['views_ui.views.list.' . $view_id] = [
        'route_name' => $url->getRouteName(),
        'route_parameters' => $url->getRouteParameters(),
        'title' => $view->label(),
        'parent' => 'entity.view.collection',
      ];
    }

    foreach ($this->derivatives as &$entry) {
      $entry += $base_plugin_definition;
    }

    return $this->derivatives;
  }

}

Note that you'll need to replace all instances of [MODULE] with the actual module name of your module.

pglatz avatar
in flag
Perfect - thanks so much, Jaypan. An elegant and simple solution; I'm extremely pleased.
Jaypan avatar
de flag
Glad to help - can you please mark this as the accepted answer?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.