dounan9070 2018-10-12 06:22
浏览 39

变量值不会在drupal 8中传递

I am learning drupal 8 and making a custom block programmatically and also using twig with it. I am passing two variables to twig but the problem is that only value of first variable is shown on the page value of second variable is not showing. And if I change the variable name of first variable that also disappears from web page. How to solve this problem?

Code of my blocks build function

  public function build() {
  $role = "";
  $username = "";
  $userId = 0;
 $db = Database::getConnection();
 $query = $db->select('user__roles', 'x')
->fields('x', array('roles_target_id','entity_id'))
->condition('x.roles_target_id', 'administrator', '=');
 $data = $query->execute();

// Get all the results
$results = $data->fetchAll(\PDO::FETCH_OBJ);

// Iterate results
 foreach ($results as $row) {
$role = $row->roles_target_id;
$userId = $row->entity_id;
 }
 $query2 = $db->select('users_field_data','u')
    ->fields('u',array('name'))
    ->condition('u.uid',$userId,'=');
    $data2 = $query2->execute();

    // Get all the results
    $results2 = $data2->fetchAll(\PDO::FETCH_OBJ);

    foreach($results2 as $r)
    {
        $username = $r->name;
    }
return array(
  '#title' => $username,
  '#descriptions' => 'Websolutions Agency is the industry leading Drupal development agency in Croatia', 
);
 }  

code of my twig

 <h1> name: {{ title }} </h1>
<h2>{{ descriptions }}</h2>

code of my .module file

 <?php
 /**
* Implements hook_theme().
*/
 function test_custom_theme() {
   return array(
     'test_custom_block' => array(
        'variables' => array('title' => NULL, 'descriptions' => NULL),
        'template' => 'block--test-custom',
    ),
);
}
  • 写回答

3条回答 默认 最新

  • dpowhyh70416 2018-10-16 09:19
    关注

    I have changed the name of #theme and templates to start with module name. Please find below example.

    src/Plugin/Block/[yourblockname].php
    
      public function build() {
        return [
          '#theme' => 'custom_blocks__front_apps',
          '#app' => 'your value',
        ];
      }
    

    custom_blocks.module:

    function custom_blocks_theme($existing, $type, $theme, $path) {
      return [
        'custom_blocks__front_apps' => [
          'variables' => [
            'app' => null
          ],
        ]
      ];
    }
    

    templates/custom-blocks--front-apps.html.twig

    <p>Hello: {{ app }}</p>
    <p>Base Label: {{ label }}</p>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效