I need add a custom block in my website, this block must show some users values (from customs fields).
I can show all user variables I want but if a user value change for some reason, I must clear all caches for get the news values from the page where is showed my custom block.
I checked the drupal 8 cache API and try find some example, but I don't find how I do for my variables aren't cached. (or cached but refresh when datas user change)
My code is very simple, I have this hook theme (in my .module) :
function main_menu_theme($existing, $type, $theme, $path)
{
$theme=array();
$theme['block__main_menu_quick_stat_block'] = array(
'base hook' => 'block',
// 'render element' => 'block', // I tried with and without this line..
);
return $theme;
}
This is the build method from my Block Class:
class MainMenuQuickStatBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
// Get the currect Drupal User
$user = _getCurrentUser();
//I tried this...
// $build['main_menu_quick_stat_block'] = array(
// '#one' => $user->customfieldone->value,
// '#two' => $user->customfieldtwo->value,
// );
// return $build;
//I tried this...
// return array(
// 'main_menu_quick_stat_block' => array(
// '#cache' => array('max-age' => 0),
// '#one' => $user->customfieldone->value,
// '#two' => $user->customfieldtwo->value,
// ),
// );
//Last try..
return array(
'#one' => $user->customfieldone->value,
'#two' => $user->customfieldtwo->value,
'#cache' => [
'max-age' => 0,
// 'contexts' => ['user',], // I tried this
// 'contexts' => 'user', // I tried this
],
);
}
}
For my custom template, I copied the default block.html.twig template from the theme directory. I can show variables "one" and "two" from the webpage thanks to the custom template. But impossible to configure theses variables for they become uncacheable !
I also try with cache tags but no success..
EDIT 1 (after 4k4 detailed answer !)
This is my last try : From my custom Block Class :
class MainMenuQuickStatBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
//$user = \Drupal::currentUser(); // If I use this way, I get this error in the "'tags' => $user->getCacheTags()," below : "Error: Call to undefined method Drupal\Core\Session\AccountProxy::getCacheTags() in Drupal\main_menu\Plugin\Block\MainMenuQuickStatBlock->build() (line 52 of modules/custom/main_menu/src/Plugin/Block/MainMenuQuickStatBlock.php)."
$var1 = $user->field_field1->value; //If a log var1, I get a value here ($var1 = 7 by example)
$var2 = 42; //its a test
return [
'#theme' => 'block__main_menu_quick_stat_block',
'#test' => 42,
'#var1' => $var1,
'#cache' => [
'contexts' => ['user'],
'tags' => $user->getCacheTags(),
],
];
}
}
From my .module file :
...
function main_menu_theme($existing, $type, $theme, $path)
{
$theme=array();
$theme['block__main_menu_quick_stat_block'] = array(
'variables' => [
'var1' => NULL,
'test' => NULL,
],
);
return $theme;
}
I cleared caches (drush cr)
My custom template block is called when I load the page, but variables 'var1' and 'test' are null. Below, I log variables array with "{{ kint() }}" in my custom template :
I'm going to find drupal guide for know why my vars are null...