2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* Builds placeholder replacement tokens system - wide data .
*
* This file handles tokens for the global 'site' and 'date' tokens .
*/
2015-08-27 12:03:05 -07:00
use Drupal\Core\Datetime\Entity\DateFormat ;
use Drupal\Core\Render\BubbleableMetadata ;
2015-08-17 17:00:26 -07:00
/**
* Implements hook_token_info () .
*/
function system_token_info () {
2017-04-13 15:53:35 +01:00
$types [ 'site' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Site information " ),
'description' => t ( " Tokens for site-wide settings and other global information. " ),
2017-04-13 15:53:35 +01:00
];
$types [ 'date' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Dates " ),
'description' => t ( " Tokens related to times and dates. " ),
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
// Site-wide global tokens.
2017-04-13 15:53:35 +01:00
$site [ 'name' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Name " ),
'description' => t ( " The name of the site. " ),
2017-04-13 15:53:35 +01:00
];
$site [ 'slogan' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Slogan " ),
'description' => t ( " The slogan of the site. " ),
2017-04-13 15:53:35 +01:00
];
$site [ 'mail' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Email " ),
'description' => t ( " The administrative email address for the site. " ),
2017-04-13 15:53:35 +01:00
];
$site [ 'url' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " URL " ),
'description' => t ( " The URL of the site's front page. " ),
2017-04-13 15:53:35 +01:00
];
$site [ 'url-brief' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " URL (brief) " ),
'description' => t ( " The URL of the site's front page without the protocol. " ),
2017-04-13 15:53:35 +01:00
];
$site [ 'login-url' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Login page " ),
'description' => t ( " The URL of the site's login page. " ),
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
// Date related tokens.
2017-04-13 15:53:35 +01:00
$date [ 'short' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Short format " ),
2017-04-13 15:53:35 +01:00
'description' => t ( " A date in 'short' format. (%date) " , [ '%date' => format_date ( REQUEST_TIME , 'short' )]),
];
$date [ 'medium' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Medium format " ),
2017-04-13 15:53:35 +01:00
'description' => t ( " A date in 'medium' format. (%date) " , [ '%date' => format_date ( REQUEST_TIME , 'medium' )]),
];
$date [ 'long' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Long format " ),
2017-04-13 15:53:35 +01:00
'description' => t ( " A date in 'long' format. (%date) " , [ '%date' => format_date ( REQUEST_TIME , 'long' )]),
];
$date [ 'custom' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Custom format " ),
'description' => t ( 'A date in a custom format. See <a href="http://php.net/manual/function.date.php">the PHP documentation</a> for details.' ),
2017-04-13 15:53:35 +01:00
];
$date [ 'since' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Time-since " ),
2017-04-13 15:53:35 +01:00
'description' => t ( " A date in 'time-since' format. (%date) " , [ '%date' => \Drupal :: service ( 'date.formatter' ) -> formatTimeDiffSince ( REQUEST_TIME - 360 )]),
];
$date [ 'raw' ] = [
2015-08-17 17:00:26 -07:00
'name' => t ( " Raw timestamp " ),
2017-04-13 15:53:35 +01:00
'description' => t ( " A date in UNIX timestamp format (%date) " , [ '%date' => REQUEST_TIME ]),
];
2015-08-17 17:00:26 -07:00
2017-04-13 15:53:35 +01:00
return [
2015-08-17 17:00:26 -07:00
'types' => $types ,
2017-04-13 15:53:35 +01:00
'tokens' => [
2015-08-17 17:00:26 -07:00
'site' => $site ,
'date' => $date ,
2017-04-13 15:53:35 +01:00
],
];
2015-08-17 17:00:26 -07:00
}
/**
* Implements hook_tokens () .
*/
2015-08-27 12:03:05 -07:00
function system_tokens ( $type , $tokens , array $data , array $options , BubbleableMetadata $bubbleable_metadata ) {
2015-08-17 17:00:26 -07:00
$token_service = \Drupal :: token ();
2017-04-13 15:53:35 +01:00
$url_options = [ 'absolute' => TRUE ];
2015-08-17 17:00:26 -07:00
if ( isset ( $options [ 'langcode' ])) {
$url_options [ 'language' ] = \Drupal :: languageManager () -> getLanguage ( $options [ 'langcode' ]);
$langcode = $options [ 'langcode' ];
}
else {
$langcode = NULL ;
}
2017-04-13 15:53:35 +01:00
$replacements = [];
2015-08-17 17:00:26 -07:00
if ( $type == 'site' ) {
foreach ( $tokens as $name => $original ) {
switch ( $name ) {
case 'name' :
2015-08-27 12:03:05 -07:00
$config = \Drupal :: config ( 'system.site' );
$bubbleable_metadata -> addCacheableDependency ( $config );
$site_name = $config -> get ( 'name' );
2015-10-08 11:40:12 -07:00
$replacements [ $original ] = $site_name ;
2015-08-17 17:00:26 -07:00
break ;
case 'slogan' :
2015-08-27 12:03:05 -07:00
$config = \Drupal :: config ( 'system.site' );
$bubbleable_metadata -> addCacheableDependency ( $config );
$slogan = $config -> get ( 'slogan' );
2015-10-08 11:40:12 -07:00
$build = [
'#markup' => $slogan ,
];
// @todo Fix in https://www.drupal.org/node/2577827
$replacements [ $original ] = \Drupal :: service ( 'renderer' ) -> renderPlain ( $build );
2015-08-17 17:00:26 -07:00
break ;
case 'mail' :
2015-08-27 12:03:05 -07:00
$config = \Drupal :: config ( 'system.site' );
$bubbleable_metadata -> addCacheableDependency ( $config );
$replacements [ $original ] = $config -> get ( 'mail' );
2015-08-17 17:00:26 -07:00
break ;
case 'url' :
2015-08-27 12:03:05 -07:00
/** @var \Drupal\Core\GeneratedUrl $result */
2017-04-13 15:53:35 +01:00
$result = \Drupal :: url ( '<front>' , [], $url_options , TRUE );
2015-08-27 12:03:05 -07:00
$bubbleable_metadata -> addCacheableDependency ( $result );
$replacements [ $original ] = $result -> getGeneratedUrl ();
2015-08-17 17:00:26 -07:00
break ;
case 'url-brief' :
2015-08-27 12:03:05 -07:00
/** @var \Drupal\Core\GeneratedUrl $result */
2017-04-13 15:53:35 +01:00
$result = \Drupal :: url ( '<front>' , [], $url_options , TRUE );
2015-08-27 12:03:05 -07:00
$bubbleable_metadata -> addCacheableDependency ( $result );
2017-04-13 15:53:35 +01:00
$replacements [ $original ] = preg_replace ([ '!^https?://!' , '!/$!' ], '' , $result -> getGeneratedUrl ());
2015-08-17 17:00:26 -07:00
break ;
case 'login-url' :
2015-08-27 12:03:05 -07:00
/** @var \Drupal\Core\GeneratedUrl $result */
$result = \Drupal :: url ( 'user.page' , [], $url_options , TRUE );
$bubbleable_metadata -> addCacheableDependency ( $result );
$replacements [ $original ] = $result -> getGeneratedUrl ();
2015-08-17 17:00:26 -07:00
break ;
}
}
}
elseif ( $type == 'date' ) {
if ( empty ( $data [ 'date' ])) {
$date = REQUEST_TIME ;
2015-08-27 12:03:05 -07:00
// We depend on the current request time, so the tokens are not cacheable
// at all.
$bubbleable_metadata -> setCacheMaxAge ( 0 );
2015-08-17 17:00:26 -07:00
}
else {
$date = $data [ 'date' ];
}
foreach ( $tokens as $name => $original ) {
switch ( $name ) {
case 'short' :
case 'medium' :
case 'long' :
2015-10-08 11:40:12 -07:00
$date_format = DateFormat :: load ( $name );
2015-08-27 12:03:05 -07:00
$bubbleable_metadata -> addCacheableDependency ( $date_format );
2015-10-08 11:40:12 -07:00
$replacements [ $original ] = format_date ( $date , $name , '' , NULL , $langcode );
2015-08-17 17:00:26 -07:00
break ;
case 'since' :
2017-04-13 15:53:35 +01:00
$replacements [ $original ] = \Drupal :: service ( 'date.formatter' ) -> formatTimeDiffSince ( $date , [ 'langcode' => $langcode ]);
2015-08-27 12:03:05 -07:00
$bubbleable_metadata -> setCacheMaxAge ( 0 );
2015-08-17 17:00:26 -07:00
break ;
case 'raw' :
2015-10-08 11:40:12 -07:00
$replacements [ $original ] = $date ;
2015-08-17 17:00:26 -07:00
break ;
}
}
if ( $created_tokens = $token_service -> findWithPrefix ( $tokens , 'custom' )) {
foreach ( $created_tokens as $name => $original ) {
$replacements [ $original ] = format_date ( $date , 'custom' , $name , NULL , $langcode );
}
}
}
return $replacements ;
}