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',
),
);
}