【问题标题】:How to return html markup from custom block Drupal 8如何从自定义块 Drupal 8 返回 html 标记
【发布时间】: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">

【问题讨论】:

    标签: drupal drupal-8


    【解决方案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>
    

    希望!它有帮助。

    【讨论】:

    • 感谢您的回答!我选择了另一种,我认为是最简单的方法,我只是在 twig 中的链接和样式块中添加了所需的属性
    【解决方案2】:

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

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多