docusign_signature-1.0.x-dev/modules/examples/src/Controller/OverviewController.php
modules/examples/src/Controller/OverviewController.php
<?php
declare(strict_types=1);
namespace Drupal\docusign_signature_examples\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Manage DocuSign examples overview.
*
* @package Drupal\docusign_signature_examples\Controller
*/
class OverviewController extends ControllerBase {
/**
* The menu link tree manager.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected MenuLinkTreeInterface $menuTree;
/**
* The active menu trail service.
*
* @var \Drupal\Core\Menu\MenuActiveTrailInterface
*/
protected MenuActiveTrailInterface $menuActiveTrail;
/**
* Constructs a OverviewController object.
*
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
* The menu tree manager.
* @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
* The active menu trail service.
*/
public function __construct(
MenuLinkTreeInterface $menu_tree,
MenuActiveTrailInterface $menu_active_trail
) {
$this->menuTree = $menu_tree;
$this->menuActiveTrail = $menu_active_trail;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('menu.link_tree'),
$container->get('menu.active_trail')
);
}
/**
* Display all DocuSign examples.
*
* Do not check access because access page is not allowed
* if DocuSign access token doesn't exist or is expired.
*
* @return array
* The list of DocuSign examples.
*/
public function page(): array {
$content = [];
$instance = $this->menuActiveTrail->getActiveLink('admin');
// Only find the children of this link.
$link_id = $instance->getPluginId();
$parameters = new MenuTreeParameters();
$parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
$tree = $this->menuTree->load(NULL, $parameters);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $this->menuTree->transform($tree, $manipulators);
foreach ($tree as $key => $element) {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = $element->link;
$content[$key]['title'] = $link->getTitle();
$content[$key]['options'] = $link->getOptions();
$content[$key]['description'] = $link->getDescription();
$content[$key]['url'] = $link->getUrlObject();
}
ksort($content);
return [
'#theme' => 'admin_block_content',
'#content' => $content,
];
}
}
