Drupal 8怎样编写自定义Block代码

By admin, 20 十月, 2015

简单得超乎想象!

https://www.drupal.org/node/2101565

个人遇到两个坑:

1. Drupal 8不能再像Drupal 7那样通过arg获取参数了。下面是获取参数的一个例子:

   

    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node)
      $nid = $node->id();

2. Drupal 8会自动缓存很多东西,如果不声明缓存上下文,那么block的内容就相当于静态的。指定缓存的方法是给返回的元素增加以下属性:

'#cache' => array('contexts' => array('url')),

具体contexts的值参考:Cache Contexts文档

标签

评论1

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
请输入"Drupal10"