function AnnouncementsFeedHooks::toolbar

Implements hook_toolbar().

File

core/modules/announcements_feed/src/Hook/AnnouncementsFeedHooks.php, line 40

Class

AnnouncementsFeedHooks
Hook implementations for announcements_feed.

Namespace

Drupal\announcements_feed\Hook

Code

public function toolbar() {
    if (!\Drupal::currentUser()->hasPermission('access announcements')) {
        return [
            '#cache' => [
                'contexts' => [
                    'user.permissions',
                ],
            ],
        ];
    }
    $items['announcement'] = [
        '#type' => 'toolbar_item',
        'tab' => [
            '#lazy_builder' => [
                'announcements_feed.lazy_builders:renderAnnouncements',
                [],
            ],
            '#create_placeholder' => TRUE,
            '#cache' => [
                'tags' => [
                    'announcements_feed:feed',
                ],
            ],
        ],
        '#wrapper_attributes' => [
            'class' => [
                'announce-toolbar-tab',
            ],
        ],
        '#cache' => [
            'contexts' => [
                'user.permissions',
            ],
        ],
        '#weight' => 3399,
    ];
    // \Drupal\toolbar\Element\ToolbarItem::preRenderToolbarItem adds an
    // #attributes property to each toolbar item's tab child automatically.
    // Lazy builders don't support an #attributes property so we need to
    // add another render callback to remove the #attributes property. We start by
    // adding the defaults, and then we append our own pre render callback.
    $items['announcement'] += \Drupal::service('plugin.manager.element_info')->getInfo('toolbar_item');
    $items['announcement']['#pre_render'][] = [
        RenderCallbacks::class,
        'removeTabAttributes',
    ];
    return $items;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.