function examples_toolbar

Same name in other branches
  1. 4.0.x examples.module \examples_toolbar()

Implements hook_toolbar().

Related topics

File

./examples.module, line 35

Code

function examples_toolbar() {
    // First, build an array of all example modules and their routes.
    // We resort to this hard-coded way so as not to muck up each example.
    $examples = _examples_toolbar_routes();
    // Build a list of links for the menu.
    $links = [];
    foreach ($examples as $module => $route) {
        // Check if module is installed or not.
        if (\Drupal::moduleHandler()->moduleExists($module)) {
            // Get the module info (title, description) from Drupal.
            $info = \Drupal::service('extension.list.module')->getExtensionInfo($module);
            // If there's no info, the example isn't enabled, so don't display it.
            if (!empty($info)) {
                $links[$module] = [
                    'title' => Html::escape($info['name']),
                    'url' => Url::fromRoute($route),
                    'attributes' => [
                        'class' => [
                            Html::getClass($module),
                        ],
                        'title' => Html::escape($info['description']),
                    ],
                ];
            }
        }
    }
    // Add a link to enable all examples.
    $links['enable_examples'] = [
        'title' => t('Enable Examples'),
        'url' => Url::fromRoute('system.modules_list'),
        'options' => [
            'title' => t('Enable more examples in on the Extend page.'),
        ],
        'fragment' => 'edit-modules-example-modules',
    ];
    // Create the examples toolbar render array.
    $items['examples'] = [
        '#type' => 'toolbar_item',
        'tab' => [
            '#type' => 'link',
            '#title' => t('Examples'),
            '#url' => Url::fromRoute('<front>'),
            '#attributes' => [
                'title' => t('Developer Examples'),
                'class' => [
                    'toolbar-icon',
                    'toolbar-icon-examples',
                ],
            ],
        ],
        'tray' => [
            '#heading' => t('Developer Examples'),
            'shortcuts' => [
                '#theme' => 'links__toolbar_example',
                '#links' => $links,
                '#attributes' => [
                    'class' => [
                        'toolbar-menu',
                    ],
                ],
            ],
        ],
        '#weight' => 99,
        '#attached' => [
            'library' => [
                'examples/examples.icons',
            ],
        ],
    ];
    return $items;
}