【问题标题】:Drupal 8 - Adding/Creating Menu Item with HTML in itDrupal 8 - 添加/创建带有 HTML 的菜单项
【发布时间】:2017-11-19 13:22:25
【问题描述】:

我正在使用 Drupal 8 创建一个网站。我想创建一个菜单项链接,我可以在其中添加 HTML/Javascript 代码(我试图显示一个在菜单中单击而不是显示的小部件它在菜单旁边的自己的块中)。我可以看到添加菜单项的唯一方法是链接到页面。

【问题讨论】:

    标签: php html drupal drupal-8


    【解决方案1】:

    您可以使用衍生产品。这使您可以自定义几乎所有内容并控制要制作的内容。下面是一个例子:

    注意:我假设您对自定义模块有一定的了解。如果没有关注this link

    在您的自定义模块中创建以下文件:

    # my_module.links.menu.yml
    my_module.custom_links:
      deriver: \Drupal\my_module\Plugin\Derivative\CustomLinkDerivative
    

    现在是衍生类(位于 my_module/src/Plugin/Derivative 下)

    <?php
    
    namespace Drupal\my_module\Plugin\Derivative;
    
    use Drupal\Component\Plugin\Derivative\DeriverBase;
    use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    class CustomLinkDerivative extends DeriverBase implements ContainerDeriverInterface {
    
      public static function create(ContainerInterface $container, $base_plugin_id) {
        return new static();
      }
    
      /**
       * {@inheritdoc}
       */
      public function getDerivativeDefinitions($base_plugin_definition) {
        $links['custom_menulink'] = [
          'title' => t('Custom menulink'),
          'menu_name' => 'main',
          'route_name' => 'entity.node.canonical',
          'parent' => footer,
          'route_parameters' => [
            'node' => 1,
          ]
        ] + $base_plugin_definition;
    
        return $links;
      }
    }
    

    注意:在重建缓存期间会触发衍生产品!

    这只是在页脚中创建一个指向节点 1 的链接。您可以根据自己的喜好添加各种内容和逻辑。希望这对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多