I'm writing my first Drupal 8 theme.
Now for a block I need all some custom fields of the user. So that I can use it in my theme.
I create a theme file with collectionmanager_preprocess_html method.
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\Entity\User;
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*
* Adds body classes if certain regions have content.
*/
function collectionmanager_preprocess_html(&$variables) {
// Load the current user.
$user = User::load(\Drupal::currentUser()->id());
// get field data from that user
$firstname = $user->field_first_name->value;
$lastname = $user->field_last_name->value;
$variables['custom_firstname'] = $user->field_first_name->value;
$variables['custom_lastname'] = $user->field_last_name->value;
}
If I'm correct I can access the variables in twig with {{custom_firstname}}. But it show always a null value. Can someone help me with this? I can't find my issue on this. The variable $firstname and $lastname contains the correct values.