2009-08-19 20:19:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Builds placeholder replacement tokens system-wide data.
|
|
|
|
*
|
2013-07-24 12:00:06 +00:00
|
|
|
* This file handles tokens for the global 'site' and 'date' tokens.
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
|
|
|
|
2015-03-29 22:13:25 +00:00
|
|
|
use Drupal\Component\Utility\SafeMarkup;
|
2014-04-07 15:01:20 +00:00
|
|
|
use Drupal\Component\Utility\Xss;
|
|
|
|
|
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 system_token_info() {
|
|
|
|
$types['site'] = array(
|
|
|
|
'name' => t("Site information"),
|
|
|
|
'description' => t("Tokens for site-wide settings and other global information."),
|
|
|
|
);
|
|
|
|
$types['date'] = array(
|
|
|
|
'name' => t("Dates"),
|
|
|
|
'description' => t("Tokens related to times and dates."),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Site-wide global tokens.
|
|
|
|
$site['name'] = array(
|
|
|
|
'name' => t("Name"),
|
|
|
|
'description' => t("The name of the site."),
|
|
|
|
);
|
|
|
|
$site['slogan'] = array(
|
|
|
|
'name' => t("Slogan"),
|
|
|
|
'description' => t("The slogan of the site."),
|
|
|
|
);
|
|
|
|
$site['mail'] = array(
|
|
|
|
'name' => t("Email"),
|
|
|
|
'description' => t("The administrative email address for the site."),
|
|
|
|
);
|
|
|
|
$site['url'] = array(
|
|
|
|
'name' => t("URL"),
|
|
|
|
'description' => t("The URL of the site's front page."),
|
|
|
|
);
|
2009-10-02 14:49:10 +00:00
|
|
|
$site['url-brief'] = array(
|
|
|
|
'name' => t("URL (brief)"),
|
|
|
|
'description' => t("The URL of the site's front page without the protocol."),
|
2010-01-30 07:59:26 +00:00
|
|
|
);
|
2009-08-19 20:19:37 +00:00
|
|
|
$site['login-url'] = array(
|
|
|
|
'name' => t("Login page"),
|
|
|
|
'description' => t("The URL of the site's login page."),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Date related tokens.
|
2009-08-25 10:27:15 +00:00
|
|
|
$date['short'] = array(
|
|
|
|
'name' => t("Short format"),
|
|
|
|
'description' => t("A date in 'short' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'short'))),
|
2009-08-19 20:19:37 +00:00
|
|
|
);
|
|
|
|
$date['medium'] = array(
|
|
|
|
'name' => t("Medium format"),
|
|
|
|
'description' => t("A date in 'medium' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'medium'))),
|
|
|
|
);
|
2009-08-25 10:27:15 +00:00
|
|
|
$date['long'] = array(
|
|
|
|
'name' => t("Long format"),
|
|
|
|
'description' => t("A date in 'long' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'long'))),
|
2009-08-19 20:19:37 +00:00
|
|
|
);
|
|
|
|
$date['custom'] = array(
|
|
|
|
'name' => t("Custom format"),
|
2014-09-16 11:07:20 +00:00
|
|
|
'description' => t('A date in a custom format. See <a href="http://php.net/manual/function.date.php">the PHP documentation</a> for details.'),
|
2009-08-19 20:19:37 +00:00
|
|
|
);
|
|
|
|
$date['since'] = array(
|
|
|
|
'name' => t("Time-since"),
|
2014-08-05 10:39:21 +00:00
|
|
|
'description' => t("A date in 'time-since' format. (%date)", array('%date' => \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - 360, 2))),
|
2009-08-19 20:19:37 +00:00
|
|
|
);
|
|
|
|
$date['raw'] = array(
|
|
|
|
'name' => t("Raw timestamp"),
|
|
|
|
'description' => t("A date in UNIX timestamp format (%date)", array('%date' => REQUEST_TIME)),
|
|
|
|
);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'types' => $types,
|
|
|
|
'tokens' => array(
|
|
|
|
'site' => $site,
|
|
|
|
'date' => $date,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-04 16:49:48 +00:00
|
|
|
* Implements hook_tokens().
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
|
|
|
function system_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
2013-09-16 03:58:06 +00:00
|
|
|
$token_service = \Drupal::token();
|
2013-04-18 07:24:35 +00:00
|
|
|
|
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'])) {
|
2015-01-18 09:47:33 +00:00
|
|
|
$url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
|
2012-08-27 03:25:18 +00:00
|
|
|
$langcode = $options['langcode'];
|
2011-08-26 09:55:42 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-08-27 03:25:18 +00:00
|
|
|
$langcode = NULL;
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
$sanitize = !empty($options['sanitize']);
|
|
|
|
|
|
|
|
$replacements = array();
|
|
|
|
|
|
|
|
if ($type == 'site') {
|
|
|
|
foreach ($tokens as $name => $original) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'name':
|
2013-09-16 03:58:06 +00:00
|
|
|
$site_name = \Drupal::config('system.site')->get('name');
|
2015-03-29 22:13:25 +00:00
|
|
|
$replacements[$original] = $sanitize ? SafeMarkup::checkPlain($site_name) : $site_name;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'slogan':
|
2013-09-16 03:58:06 +00:00
|
|
|
$slogan = \Drupal::config('system.site')->get('slogan');
|
2014-04-07 15:01:20 +00:00
|
|
|
$replacements[$original] = $sanitize ? Xss::filterAdmin($slogan) : $slogan;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mail':
|
2013-09-16 03:58:06 +00:00
|
|
|
$replacements[$original] = \Drupal::config('system.site')->get('mail');
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'url':
|
2014-09-27 07:03:46 +00:00
|
|
|
$replacements[$original] = \Drupal::url('<front>', array(), $url_options);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
2009-10-02 14:49:10 +00:00
|
|
|
case 'url-brief':
|
2014-09-27 07:03:46 +00:00
|
|
|
$replacements[$original] = preg_replace(array('!^https?://!', '!/$!'), '', \Drupal::url('<front>', array(), $url_options));
|
2009-10-02 14:49:10 +00:00
|
|
|
break;
|
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
case 'login-url':
|
2014-09-27 07:03:46 +00:00
|
|
|
$replacements[$original] = \Drupal::url('user.page', [], $url_options);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elseif ($type == 'date') {
|
|
|
|
if (empty($data['date'])) {
|
|
|
|
$date = REQUEST_TIME;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$date = $data['date'];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($tokens as $name => $original) {
|
|
|
|
switch ($name) {
|
2009-08-25 10:27:15 +00:00
|
|
|
case 'short':
|
2012-08-27 03:25:18 +00:00
|
|
|
$replacements[$original] = format_date($date, 'short', '', NULL, $langcode);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'medium':
|
2012-08-27 03:25:18 +00:00
|
|
|
$replacements[$original] = format_date($date, 'medium', '', NULL, $langcode);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
2009-08-25 10:27:15 +00:00
|
|
|
case 'long':
|
2012-08-27 03:25:18 +00:00
|
|
|
$replacements[$original] = format_date($date, 'long', '', NULL, $langcode);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'since':
|
2014-08-05 10:39:21 +00:00
|
|
|
$replacements[$original] = \Drupal::service('date.formatter')->formatInterval((REQUEST_TIME - $date), 2, $langcode);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
2010-04-20 09:48:06 +00:00
|
|
|
|
|
|
|
case 'raw':
|
2015-03-29 22:13:25 +00:00
|
|
|
$replacements[$original] = $sanitize ? SafeMarkup::checkPlain($date) : $date;
|
2010-04-20 09:48:06 +00:00
|
|
|
break;
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 07:24:35 +00:00
|
|
|
if ($created_tokens = $token_service->findWithPrefix($tokens, 'custom')) {
|
2009-08-19 20:19:37 +00:00
|
|
|
foreach ($created_tokens as $name => $original) {
|
2012-08-27 03:25:18 +00:00
|
|
|
$replacements[$original] = format_date($date, 'custom', $name, NULL, $langcode);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $replacements;
|
|
|
|
}
|