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
*/
2019-04-16 05:38:27 +00:00
use Drupal\Core\Url ;
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 ;
2014-04-07 15:01:20 +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 system_token_info () {
2017-03-04 01:20:24 +00:00
$types [ 'site' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Site information " ),
'description' => t ( " Tokens for site-wide settings and other global information. " ),
2017-03-04 01:20:24 +00:00
];
$types [ 'date' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Dates " ),
'description' => t ( " Tokens related to times and dates. " ),
2017-03-04 01:20:24 +00:00
];
2009-08-19 20:19:37 +00:00
// Site-wide global tokens.
2017-03-04 01:20:24 +00:00
$site [ 'name' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Name " ),
'description' => t ( " The name of the site. " ),
2017-03-04 01:20:24 +00:00
];
$site [ 'slogan' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Slogan " ),
'description' => t ( " The slogan of the site. " ),
2017-03-04 01:20:24 +00:00
];
$site [ 'mail' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Email " ),
'description' => t ( " The administrative email address for the site. " ),
2017-03-04 01:20:24 +00:00
];
Issue #1088112 by Liam Morland, mbovan, Krzysztof Domański, JeroenT, ankithashetty, YurkinPark, jungle, raman.b, Lukas von Blarer, mrshowerman, alexpott, Berdir, darvanen, ao2: Introduce a token to get site's base URL
2023-02-18 16:16:04 +00:00
$site [ 'base-url' ] = [
'name' => t ( " Base URL " ),
'description' => t ( " The base URL of the site, currently: @base_url " ,
[
'@base_url' => \Drupal :: service ( 'router.request_context' ) -> getCompleteBaseUrl (),
]),
];
$site [ 'base-path' ] = [
'name' => t ( " Base path " ),
'description' => t ( " The base path of the site, currently: @base_path " , [ '@base_path' => \Drupal :: request () -> getBasePath ()]),
];
2017-03-04 01:20:24 +00:00
$site [ 'url' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " URL " ),
Issue #1088112 by Liam Morland, mbovan, Krzysztof Domański, JeroenT, ankithashetty, YurkinPark, jungle, raman.b, Lukas von Blarer, mrshowerman, alexpott, Berdir, darvanen, ao2: Introduce a token to get site's base URL
2023-02-18 16:16:04 +00:00
'description' => t ( " The URL of the site's front page with the language prefix, if it exists. " ),
2017-03-04 01:20:24 +00:00
];
$site [ 'url-brief' ] = [
2009-10-02 14:49:10 +00:00
'name' => t ( " URL (brief) " ),
'description' => t ( " The URL of the site's front page without the protocol. " ),
2017-03-04 01:20:24 +00:00
];
$site [ 'login-url' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Login page " ),
'description' => t ( " The URL of the site's login page. " ),
2017-03-04 01:20:24 +00:00
];
2009-08-19 20:19:37 +00:00
2019-01-01 19:15:24 +00:00
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal :: service ( 'date.formatter' );
2009-08-19 20:19:37 +00:00
// Date related tokens.
2022-07-04 14:26:28 +00:00
$request_time = \Drupal :: time () -> getRequestTime ();
2017-03-04 01:20:24 +00:00
$date [ 'short' ] = [
2009-08-25 10:27:15 +00:00
'name' => t ( " Short format " ),
2023-11-16 16:46:55 +00:00
'description' => t ( " The current date in 'short' format. (%date) " , [ '%date' => $date_formatter -> format ( $request_time , 'short' )]),
2017-03-04 01:20:24 +00:00
];
$date [ 'medium' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Medium format " ),
2023-11-16 16:46:55 +00:00
'description' => t ( " The current date in 'medium' format. (%date) " , [ '%date' => $date_formatter -> format ( $request_time , 'medium' )]),
2017-03-04 01:20:24 +00:00
];
$date [ 'long' ] = [
2009-08-25 10:27:15 +00:00
'name' => t ( " Long format " ),
2023-11-16 16:46:55 +00:00
'description' => t ( " The current date in 'long' format. (%date) " , [ '%date' => $date_formatter -> format ( $request_time , 'long' )]),
2017-03-04 01:20:24 +00:00
];
$date [ 'custom' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Custom format " ),
2023-11-16 16:46:55 +00:00
'description' => t ( 'The current date in a custom format. See <a href="https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters">the PHP documentation</a> for details.' ),
2017-03-04 01:20:24 +00:00
];
$date [ 'since' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Time-since " ),
2023-11-16 16:46:55 +00:00
'description' => t ( " The current date in 'time-since' format. (%date) " , [ '%date' => $date_formatter -> formatTimeDiffSince ( $request_time - 360 )]),
2017-03-04 01:20:24 +00:00
];
$date [ 'raw' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " Raw timestamp " ),
2023-11-16 16:46:55 +00:00
'description' => t ( " The current date in UNIX timestamp format (%date) " , [ '%date' => $request_time ]),
2017-03-04 01:20:24 +00:00
];
2009-08-19 20:19:37 +00:00
2017-03-04 01:20:24 +00:00
return [
2009-08-19 20:19:37 +00:00
'types' => $types ,
2017-03-04 01:20:24 +00:00
'tokens' => [
2009-08-19 20:19:37 +00:00
'site' => $site ,
'date' => $date ,
2017-03-04 01:20:24 +00:00
],
];
2009-08-19 20:19:37 +00:00
}
/**
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 system_tokens ( $type , $tokens , array $data , array $options , BubbleableMetadata $bubbleable_metadata ) {
2013-09-16 03:58:06 +00:00
$token_service = \Drupal :: token ();
2013-04-18 07:24:35 +00:00
2017-03-04 01:20:24 +00:00
$url_options = [ '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
}
2017-03-04 01:20:24 +00:00
$replacements = [];
2009-08-19 20:19:37 +00:00
if ( $type == 'site' ) {
foreach ( $tokens as $name => $original ) {
switch ( $name ) {
case 'name' :
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
$config = \Drupal :: config ( 'system.site' );
$bubbleable_metadata -> addCacheableDependency ( $config );
$site_name = $config -> get ( 'name' );
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
$replacements [ $original ] = $site_name ;
2009-08-19 20:19:37 +00:00
break ;
case 'slogan' :
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
$config = \Drupal :: config ( 'system.site' );
$bubbleable_metadata -> addCacheableDependency ( $config );
$slogan = $config -> get ( 'slogan' );
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
$build = [
'#markup' => $slogan ,
];
// @todo Fix in https://www.drupal.org/node/2577827
$replacements [ $original ] = \Drupal :: service ( 'renderer' ) -> renderPlain ( $build );
2009-08-19 20:19:37 +00:00
break ;
case 'mail' :
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
$config = \Drupal :: config ( 'system.site' );
$bubbleable_metadata -> addCacheableDependency ( $config );
$replacements [ $original ] = $config -> get ( 'mail' );
2009-08-19 20:19:37 +00:00
break ;
Issue #1088112 by Liam Morland, mbovan, Krzysztof Domański, JeroenT, ankithashetty, YurkinPark, jungle, raman.b, Lukas von Blarer, mrshowerman, alexpott, Berdir, darvanen, ao2: Introduce a token to get site's base URL
2023-02-18 16:16:04 +00:00
case 'base-url' :
$bubbleable_metadata -> addCacheContexts ([ 'url.site' ]);
$replacements [ $original ] = \Drupal :: service ( 'router.request_context' ) -> getCompleteBaseUrl ();
break ;
case 'base-path' :
$bubbleable_metadata -> addCacheContexts ([ 'url.site' ]);
$replacements [ $original ] = \Drupal :: request () -> getBasePath ();
break ;
2009-08-19 20:19:37 +00:00
case 'url' :
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\Core\GeneratedUrl $result */
2019-04-16 05:38:27 +00:00
$result = Url :: fromRoute ( '<front>' , [], $url_options ) -> toString ( TRUE );
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 -> addCacheableDependency ( $result );
$replacements [ $original ] = $result -> getGeneratedUrl ();
2009-08-19 20:19:37 +00:00
break ;
2009-10-02 14:49:10 +00:00
case 'url-brief' :
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\Core\GeneratedUrl $result */
2019-04-16 05:38:27 +00:00
$result = Url :: fromRoute ( '<front>' , [], $url_options ) -> toString ( TRUE );
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 -> addCacheableDependency ( $result );
2017-03-04 01:20:24 +00:00
$replacements [ $original ] = preg_replace ([ '!^https?://!' , '!/$!' ], '' , $result -> getGeneratedUrl ());
2009-10-02 14:49:10 +00:00
break ;
2009-08-19 20:19:37 +00:00
case 'login-url' :
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\Core\GeneratedUrl $result */
2019-04-16 05:38:27 +00:00
$result = Url :: fromRoute ( 'user.page' , [], $url_options ) -> toString ( TRUE );
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 -> addCacheableDependency ( $result );
$replacements [ $original ] = $result -> getGeneratedUrl ();
2009-08-19 20:19:37 +00:00
break ;
}
}
}
elseif ( $type == 'date' ) {
if ( empty ( $data [ 'date' ])) {
2022-07-04 14:26:28 +00:00
$date = \Drupal :: time () -> getRequestTime ();
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
// We depend on the current request time, so the tokens are not cacheable
// at all.
$bubbleable_metadata -> setCacheMaxAge ( 0 );
2009-08-19 20:19:37 +00:00
}
else {
$date = $data [ 'date' ];
}
foreach ( $tokens as $name => $original ) {
switch ( $name ) {
2009-08-25 10:27:15 +00:00
case 'short' :
2009-08-19 20:19:37 +00:00
case 'medium' :
2009-08-25 10:27:15 +00:00
case 'long' :
2015-09-06 20:50:41 +00:00
$date_format = DateFormat :: load ( $name );
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 -> addCacheableDependency ( $date_format );
2019-01-01 19:15:24 +00:00
$replacements [ $original ] = \Drupal :: service ( 'date.formatter' ) -> format ( $date , $name , '' , NULL , $langcode );
2009-08-19 20:19:37 +00:00
break ;
case 'since' :
2017-03-04 01:20:24 +00:00
$replacements [ $original ] = \Drupal :: service ( 'date.formatter' ) -> formatTimeDiffSince ( $date , [ 'langcode' => $langcode ]);
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 -> setCacheMaxAge ( 0 );
2009-08-19 20:19:37 +00:00
break ;
2010-04-20 09:48:06 +00:00
case 'raw' :
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
$replacements [ $original ] = $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 ) {
2019-01-01 19:15:24 +00:00
$replacements [ $original ] = \Drupal :: service ( 'date.formatter' ) -> format ( $date , 'custom' , $name , NULL , $langcode );
2009-08-19 20:19:37 +00:00
}
}
}
return $replacements ;
}