如何从自定义区块Drupal 8返回HTML标记

时间:2019-08-04 18:08:08

标签: drupal drupal-8

我在自定义模块中创建了一个类似于插件的新块。此块必须呈现“登录/注册”链接。这是函数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">

2 个答案:

答案 0 :(得分:1)

您可以为此使用hook_theme

function hook_theme() {
  return array(
   'block_name' => array(
            'variables' => array(),
            'template' => 'block_name',
        ),
  );
}

在block_name.twig文件中,您可以像这样

<ul class="header__top__ul">
<li class="header__top__li"><a href="{{ links.login.url}}">{{ links.login.title }}</a></li>
<li class="header__top__li"><a href="{{ links.register.url}}">{{ links.register.title }}</a></li></li>
</ul>

希望!它会有所帮助。

答案 1 :(得分:0)

只需将所需的属性添加到链接:

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'),
        '#attributes' => [
          'class' => [
            'header__top__a'
          ]
        ]
      ];

      $build['links']['register'] = [
        '#title' => $this->t('Register'),
        '#type' => 'link',
        '#url' => Url::fromRoute('user.register'),
        '#attributes' => [
          'class' => [
            'header__top__a'
          ]
        ]
      ];
    } else {
      $build['links']['cabinet'] = [
        '#title' => $this->t('My cabinet'),
        '#type' => 'link',
        '#url' => Url::fromRoute('user.page'),
        '#attributes' => [
          'class' => [
            'header__top__a'
          ]
        ]
      ];

      $build['links']['logout'] = [
        '#title' => $this->t('Logout'),
        '#type' => 'link',
        '#url' => Url::fromRoute('user.logout'),
        '#attributes' => [
          'class' => [
            'header__top__a'
          ]
        ]
      ];
    }

    // Apply metadata.
    $cacheableMetadata->applyTo($build);

    return $build;
  }

并在block.twig.file中创建了一个html结构:

<div class="header__top__right">
    <ul class="header__top__ul">
        {% for link in content.links %}
            {{ link }}
        {% endfor %}
    </ul>
</div>