function announcements_feed_toolbar

Same name and namespace in other branches
  1. 10 core/modules/announcements_feed/announcements_feed.module \announcements_feed_toolbar()

Implements hook_toolbar().

File

core/modules/announcements_feed/announcements_feed.module, line 34

Code

function announcements_feed_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.