Nida Akram
Nida Akram

Reputation: 362

Variables value is not passing in twig in 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',
    ),
);
}

Upvotes: 1

Views: 3858

Answers (3)

Vernit Gupta
Vernit Gupta

Reputation: 339

Check this out to create theme and use the variables in twig

File location - module/custom/MODULENAME/MODULENAME.module
    /**
     * @file
     * Twig template for render content
     */
    function MODULENAME_theme($existing, $type, $theme, $path) {
      return [
        'theme_name_template' => [
          'variables' => ['flag' => NULL],
        ],
      ];
    }
    To Use theme function use below code 
    return ['#theme' => 'theme_name_template', '#flag' => 1];

Upvotes: 1

Wimanicesir
Wimanicesir

Reputation: 5122

In your block.php you should add the theme you're using. This is defined in the module file. So your return array should look like this:

    return array(
      '#theme' => 'test_custom_block'
      '#title' => $username,
      '#descriptions' => 'Websolutions Agency is the industry leading Drupal 
      development agency in Croatia', 
     );

because in the modulefile you're saying this

'test_custom_block' => array(...)

Upvotes: 0

Shripal Zala
Shripal Zala

Reputation: 92

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>

Upvotes: 0

Related Questions