如何 return html 来自自定义块 Drupal 8 的标记

How to return html markup from custom block Drupal 8

我在自定义模块中像插件一样创建了新块。此块必须呈现 Login/Register links。这是函数 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;
  }

如何将每个 link 换成 <li class="header__top__li">

并且还用 <ul class="header__top__ul">

你可以使用 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>

希望!有帮助。

刚刚向链接添加了所需的属性:

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>