【发布时间】:2019-12-12 09:59:42
【问题描述】:
我在自定义模块中创建了新块,例如插件。此块必须呈现登录/注册链接。下面是函数 build() 代码:
public function build() {
// Init metadata.
$cacheableMetadata = new CacheableMetadata();
$build = [
'#cache' => [
'contexts' => [
'user',
],
],
];
if ($this->currentUser->isAnonymous()) {
$build['links']['login'] = [
'#title' => $this->t('Login'),
'#type' => 'link',
'#url' => Url::fromRoute('user.login')
];
$build['links']['register'] = [
'#title' => $this->t('Register'),
'#type' => 'link',
'#url' => Url::fromRoute('user.register')
];
} else {
$build['links']['cabinet'] = [
'#title' => $this->t('My cabinet'),
'#type' => 'link',
'#url' => Url::fromRoute('user.page')
];
$build['links']['logout'] = [
'#title' => $this->t('Logout'),
'#type' => 'link',
'#url' => Url::fromRoute('user.logout')
];
}
// Apply metadata.
$cacheableMetadata->applyTo($build);
return $build;
}
如何使用<li class="header__top__li"> 包装每个链接
并且还用
<ul class="header__top__ul">
【问题讨论】: