diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 36d6675..09c66ad 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 @@ -865,6 +873,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. *