function NavigationLinkBlock::build

Overrides BlockPluginInterface::build

File

core/modules/navigation/src/Plugin/Block/NavigationLinkBlock.php, line 243

Class

NavigationLinkBlock
Defines a link navigation block.

Namespace

Drupal\navigation\Plugin\Block

Code

public function build() : array {
    $config = $this->configuration;
    $build = [];
    // Ensure that user has access to link before rendering it.
    try {
        $url = Url::fromUri($config['uri']);
        $access = $url->access(NULL, TRUE);
        if (!$access->isAllowed()) {
            // Cacheable dependency is explicitly added when access is not granted.
            // It is bubbled when the link is rendered.
            $cacheable_metadata = new CacheableMetadata();
            $cacheable_metadata->addCacheableDependency($access);
            $cacheable_metadata->applyTo($build);
            return $build;
        }
    } catch (\InvalidArgumentException) {
        return $build;
    }
    return $build + [
        '#title' => $config['label'],
        '#theme' => 'navigation_menu',
        '#menu_name' => 'link',
        '#items' => [
            [
                'title' => $config['title'],
                'class' => $config['icon_class'],
                'url' => $url,
            ],
        ],
    ];
}

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