From 0e49ebeb72a856a5a62bd7e344b541083f4b8b70 Mon Sep 17 00:00:00 2001 From: Ariel Barreiro Date: Thu, 12 May 2022 14:41:40 -0300 Subject: [PATCH] Adding drupal_userdata --- src/TwigExtension.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 4ccbfc0..06f54cc 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -178,6 +178,14 @@ class TwigExtension extends AbstractExtension { // @endcode new TwigFunction('drupal_config', [$this, 'drupalConfig']), + // - Drupal User Data - + // + // @code + // {{ drupal_userdata('my_module', 'name') }} + // {{ drupal_userdata('my_module', 'name', 1) }} + // @endcode + new TwigFunction('drupal_userdata', [$this, 'drupalUserData']), + // - Drupal Dump - // // @code @@ -863,6 +871,27 @@ class TwigExtension extends AbstractExtension { return \Drupal::config($name)->get($key); } + /** + * Retrieves data from a given user. + * + * @param string $module + * The name of the module the data is associated with. + * @param string $name + * The name of the data key. + * @param int $uid + * (optional) The user account ID the data is associated with if present, + * otherwise the current user will be used. + * + * @return mixed + * The data that was requested. + */ + public function drupalUserData($module, $name, $uid = NULL) { + if (!$uid) { + $uid = \Drupal::currentUser()->id(); + } + return \Drupal::service('user.data')->get($module, $uid, $name); + } + /** * Dumps information about variables. *