2009-08-19 20:19:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Builds placeholder replacement tokens for user-related data.
|
|
|
|
*/
|
|
|
|
|
2015-03-29 22:13:25 +00:00
|
|
|
use Drupal\Component\Utility\SafeMarkup;
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
use Drupal\Core\Datetime\Entity\DateFormat;
|
|
|
|
use Drupal\Core\Render\BubbleableMetadata;
|
Issue #2322195 by rpayanm, nlisgo, cilefen, balagan, LinL, akashjain132, filijonka, epari.siva, Tebro, Temoor, pcambra, unstatu, Poornima3, Shivam Agarwal, abhi170893, Dom., PieterJanPut, Mile23, JeroenT, disasm: Replace all instances of user_load(), user_load_multiple(), entity_load('user') and entity_load_multiple('user') with static method calls
2015-04-27 02:00:41 +00:00
|
|
|
use Drupal\user\Entity\User;
|
2014-06-13 02:33:48 +00:00
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
/**
|
2009-12-04 16:49:48 +00:00
|
|
|
* Implements hook_token_info().
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
|
|
|
function user_token_info() {
|
|
|
|
$types['user'] = array(
|
|
|
|
'name' => t('Users'),
|
|
|
|
'description' => t('Tokens related to individual user accounts.'),
|
|
|
|
'needs-data' => 'user',
|
|
|
|
);
|
|
|
|
$types['current-user'] = array(
|
|
|
|
'name' => t('Current user'),
|
|
|
|
'description' => t('Tokens related to the currently logged in user.'),
|
|
|
|
'type' => 'user',
|
|
|
|
);
|
|
|
|
|
|
|
|
$user['uid'] = array(
|
|
|
|
'name' => t('User ID'),
|
|
|
|
'description' => t("The unique ID of the user account."),
|
|
|
|
);
|
|
|
|
$user['name'] = array(
|
|
|
|
'name' => t("Name"),
|
|
|
|
'description' => t("The login name of the user account."),
|
|
|
|
);
|
|
|
|
$user['mail'] = array(
|
|
|
|
'name' => t("Email"),
|
|
|
|
'description' => t("The email address of the user account."),
|
|
|
|
);
|
|
|
|
$user['url'] = array(
|
|
|
|
'name' => t("URL"),
|
|
|
|
'description' => t("The URL of the account profile page."),
|
|
|
|
);
|
|
|
|
$user['edit-url'] = array(
|
|
|
|
'name' => t("Edit URL"),
|
2010-10-16 20:09:17 +00:00
|
|
|
'description' => t("The URL of the account edit page."),
|
2009-08-19 20:19:37 +00:00
|
|
|
);
|
2009-09-30 18:37:30 +00:00
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
$user['last-login'] = array(
|
|
|
|
'name' => t("Last login"),
|
|
|
|
'description' => t("The date the user last logged in to the site."),
|
|
|
|
'type' => 'date',
|
|
|
|
);
|
|
|
|
$user['created'] = array(
|
|
|
|
'name' => t("Created"),
|
|
|
|
'description' => t("The date the user account was created."),
|
|
|
|
'type' => 'date',
|
|
|
|
);
|
|
|
|
|
|
|
|
return array(
|
2010-03-07 23:18:06 +00:00
|
|
|
'types' => $types,
|
2009-08-19 20:19:37 +00:00
|
|
|
'tokens' => array('user' => $user),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-04 16:49:48 +00:00
|
|
|
* Implements hook_tokens().
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
function user_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
|
2013-04-18 07:24:35 +00:00
|
|
|
|
2013-09-16 03:58:06 +00:00
|
|
|
$token_service = \Drupal::token();
|
2009-08-19 20:19:37 +00:00
|
|
|
$url_options = array('absolute' => TRUE);
|
2012-08-27 03:25:18 +00:00
|
|
|
if (isset($options['langcode'])) {
|
2014-03-31 17:29:01 +00:00
|
|
|
$url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
|
2012-08-27 03:25:18 +00:00
|
|
|
$langcode = $options['langcode'];
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-03-02 18:46:16 +00:00
|
|
|
$langcode = NULL;
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
$sanitize = !empty($options['sanitize']);
|
|
|
|
|
|
|
|
$replacements = array();
|
|
|
|
|
|
|
|
if ($type == 'user' && !empty($data['user'])) {
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
/** @var \Drupal\user\UserInterface $account */
|
2009-08-19 20:19:37 +00:00
|
|
|
$account = $data['user'];
|
|
|
|
foreach ($tokens as $name => $original) {
|
|
|
|
switch ($name) {
|
|
|
|
// Basic user account information.
|
|
|
|
case 'uid':
|
2010-06-29 18:24:10 +00:00
|
|
|
// In the case of hook user_presave uid is not set yet.
|
2013-07-11 17:29:02 +00:00
|
|
|
$replacements[$original] = $account->id() ?: t('not yet assigned');
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'name':
|
2012-01-16 02:18:26 +00:00
|
|
|
$name = user_format_name($account);
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
if ($account->isAnonymous()) {
|
|
|
|
$bubbleable_metadata->addCacheableDependency(\Drupal::config('user.settings'));
|
|
|
|
}
|
2015-03-29 22:13:25 +00:00
|
|
|
$replacements[$original] = $sanitize ? SafeMarkup::checkPlain($name) : $name;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mail':
|
2015-03-29 22:13:25 +00:00
|
|
|
$replacements[$original] = $sanitize ? SafeMarkup::checkPlain($account->getEmail()) : $account->getEmail();
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'url':
|
2014-09-27 07:03:46 +00:00
|
|
|
$replacements[$original] = $account->id() ? $account->url('canonical', $url_options) : t('not yet assigned');
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'edit-url':
|
2014-09-27 07:03:46 +00:00
|
|
|
$replacements[$original] = $account->id() ? $account->url('edit-form', $url_options) : t('not yet assigned');
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// These tokens are default variations on the chained tokens handled below.
|
|
|
|
case 'last-login':
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$date_format = DateFormat::load('medium');
|
|
|
|
$bubbleable_metadata->addCacheableDependency($date_format);
|
2013-07-24 19:40:03 +00:00
|
|
|
$replacements[$original] = $account->getLastLoginTime() ? format_date($account->getLastLoginTime(), 'medium', '', NULL, $langcode) : t('never');
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'created':
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$date_format = DateFormat::load('medium');
|
|
|
|
$bubbleable_metadata->addCacheableDependency($date_format);
|
2010-06-29 18:24:10 +00:00
|
|
|
// In the case of user_presave the created date may not yet be set.
|
2013-07-24 19:40:03 +00:00
|
|
|
$replacements[$original] = $account->getCreatedTime() ? format_date($account->getCreatedTime(), 'medium', '', NULL, $langcode) : t('not yet created');
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 07:24:35 +00:00
|
|
|
if ($login_tokens = $token_service->findWithPrefix($tokens, 'last-login')) {
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$replacements += $token_service->generate('date', $login_tokens, array('date' => $account->getLastLoginTime()), $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 07:24:35 +00:00
|
|
|
if ($registered_tokens = $token_service->findWithPrefix($tokens, 'created')) {
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$replacements += $token_service->generate('date', $registered_tokens, array('date' => $account->getCreatedTime()), $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-09 08:01:56 +00:00
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
if ($type == 'current-user') {
|
Issue #2322195 by rpayanm, nlisgo, cilefen, balagan, LinL, akashjain132, filijonka, epari.siva, Tebro, Temoor, pcambra, unstatu, Poornima3, Shivam Agarwal, abhi170893, Dom., PieterJanPut, Mile23, JeroenT, disasm: Replace all instances of user_load(), user_load_multiple(), entity_load('user') and entity_load_multiple('user') with static method calls
2015-04-27 02:00:41 +00:00
|
|
|
$account = User::load(\Drupal::currentUser()->id());
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$bubbleable_metadata->addCacheContexts(['user']);
|
|
|
|
$replacements += $token_service->generate('user', $tokens, array('user' => $account), $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $replacements;
|
|
|
|
}
|