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
];
$site [ 'url' ] = [
2009-08-19 20:19:37 +00:00
'name' => t ( " URL " ),
'description' => t ( " The URL of the site's front page. " ),
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.
2017-03-04 01:20:24 +00:00
$date [ 'short' ] = [
2009-08-25 10:27:15 +00:00
'name' => t ( " Short format " ),
2019-01-01 19:15:24 +00:00
'description' => t ( " A 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 " ),
2019-01-01 19:15:24 +00:00
'description' => t ( " A 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 " ),
2019-01-01 19:15:24 +00:00
'description' => t ( " A 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 " ),
2020-10-16 08:11:59 +00:00
'description' => t ( 'A 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 " ),
2019-01-01 19:15:24 +00:00
'description' => t ( " A 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 " ),
2017-03-04 01:20:24 +00:00
'description' => t ( " A date in UNIX timestamp format (%date) " , [ '%date' => REQUEST_TIME ]),
];
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 ;
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' ])) {
$date = REQUEST_TIME ;
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 ;
}