2009-08-19 20:19:37 +00:00
< ? php
/**
* @ file
* Builds placeholder replacement tokens system - wide data .
*
* This file handles tokens for the global 'site' token type , as well as
* 'date' and 'file' tokens .
*/
/**
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. " ),
);
$types [ 'file' ] = array (
'name' => t ( " Files " ),
'description' => t ( " Tokens related to uploaded files. " ),
'needs-data' => 'file' ,
);
// 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 " ),
2012-03-24 06:14:35 +00:00
'description' => t ( " A date in a custom format. See !php-date for details. " , array ( '!php-date' => l ( t ( 'the PHP documentation' ), 'http://php.net/manual/function.date.php' ))),
2009-08-19 20:19:37 +00:00
);
$date [ 'since' ] = array (
'name' => t ( " Time-since " ),
2010-04-20 09:48:06 +00:00
'description' => t ( " A date in 'time-since' format. (%date) " , array ( '%date' => format_interval ( 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 )),
);
// File related tokens.
$file [ 'fid' ] = array (
'name' => t ( " File ID " ),
'description' => t ( " The unique ID of the uploaded file. " ),
);
$file [ 'name' ] = array (
'name' => t ( " File name " ),
'description' => t ( " The name of the file on disk. " ),
);
$file [ 'path' ] = array (
'name' => t ( " Path " ),
2010-04-20 09:48:06 +00:00
'description' => t ( " The location of the file relative to Drupal root. " ),
2009-08-19 20:19:37 +00:00
);
$file [ 'mime' ] = array (
'name' => t ( " MIME type " ),
'description' => t ( " The MIME type of the file. " ),
);
$file [ 'size' ] = array (
'name' => t ( " File size " ),
2011-08-26 09:36:50 +00:00
'description' => t ( " The size of the file. " ),
2009-08-19 20:19:37 +00:00
);
2010-04-20 09:48:06 +00:00
$file [ 'url' ] = array (
2009-08-19 20:19:37 +00:00
'name' => t ( " URL " ),
'description' => t ( " The web-accessible URL for the file. " ),
);
$file [ 'timestamp' ] = array (
'name' => t ( " Timestamp " ),
'description' => t ( " The date the file was most recently changed. " ),
'type' => 'date' ,
);
$file [ 'owner' ] = array (
'name' => t ( " Owner " ),
'description' => t ( " The user who originally uploaded the file. " ),
'type' => 'user' ,
);
return array (
'types' => $types ,
'tokens' => array (
'site' => $site ,
'date' => $date ,
'file' => $file ,
),
);
}
/**
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-04-18 07:24:35 +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' ])) {
$url_options [ 'language' ] = language_load ( $options [ 'langcode' ]);
$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' :
2012-07-02 17:20:33 +00:00
$site_name = config ( 'system.site' ) -> get ( 'name' );
2009-08-19 20:19:37 +00:00
$replacements [ $original ] = $sanitize ? check_plain ( $site_name ) : $site_name ;
break ;
case 'slogan' :
2012-07-02 17:20:33 +00:00
$slogan = config ( 'system.site' ) -> get ( 'slogan' );
2012-07-29 13:26:11 +00:00
$replacements [ $original ] = $sanitize ? filter_xss_admin ( $slogan ) : $slogan ;
2009-08-19 20:19:37 +00:00
break ;
case 'mail' :
2012-07-02 17:20:33 +00:00
$replacements [ $original ] = config ( 'system.site' ) -> get ( 'mail' );
2009-08-19 20:19:37 +00:00
break ;
case 'url' :
$replacements [ $original ] = url ( '<front>' , $url_options );
break ;
2009-10-02 14:49:10 +00:00
case 'url-brief' :
2010-10-28 01:33:41 +00:00
$replacements [ $original ] = preg_replace ( array ( '!^https?://!' , '!/$!' ), '' , url ( '<front>' , $url_options ));
2009-10-02 14:49:10 +00:00
break ;
2009-08-19 20:19:37 +00:00
case 'login-url' :
$replacements [ $original ] = url ( 'user' , $url_options );
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' :
2012-08-27 03:25:18 +00:00
$replacements [ $original ] = format_interval (( REQUEST_TIME - $date ), 2 , $langcode );
2009-08-19 20:19:37 +00:00
break ;
2010-04-20 09:48:06 +00:00
case 'raw' :
2010-08-17 21:31:13 +00:00
$replacements [ $original ] = $sanitize ? check_plain ( $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
}
}
}
elseif ( $type == 'file' && ! empty ( $data [ 'file' ])) {
$file = $data [ 'file' ];
foreach ( $tokens as $name => $original ) {
switch ( $name ) {
// Basic keys and values.
case 'fid' :
2013-06-15 08:46:11 +00:00
$replacements [ $original ] = $file -> id ();
2009-08-19 20:19:37 +00:00
break ;
// Essential file data
case 'name' :
2013-06-15 08:46:11 +00:00
$replacements [ $original ] = $sanitize ? check_plain ( $file -> getFilename ()) : $file -> getFilename ();
2009-08-19 20:19:37 +00:00
break ;
case 'path' :
2013-06-15 08:46:11 +00:00
$replacements [ $original ] = $sanitize ? check_plain ( $file -> getFileUri ()) : $file -> getFileUri ();
2009-08-19 20:19:37 +00:00
break ;
case 'mime' :
2013-06-15 08:46:11 +00:00
$replacements [ $original ] = $sanitize ? check_plain ( $file -> getMimeType ()) : $file -> getMimeType ();
2009-08-19 20:19:37 +00:00
break ;
case 'size' :
2013-06-15 08:46:11 +00:00
$replacements [ $original ] = format_size ( $file -> getSize ());
2009-08-19 20:19:37 +00:00
break ;
case 'url' :
2013-06-15 08:46:11 +00:00
$replacements [ $original ] = $sanitize ? check_plain ( file_create_url ( $file -> getFileUri ())) : file_create_url ( $file -> getFileUri ());
2009-08-19 20:19:37 +00:00
break ;
// These tokens are default variations on the chained tokens handled below.
case 'timestamp' :
2013-06-15 08:46:11 +00:00
$replacements [ $original ] = format_date ( $file -> getChangedTime (), 'medium' , '' , NULL , $langcode );
2009-08-19 20:19:37 +00:00
break ;
case 'owner' :
2013-06-15 08:46:11 +00:00
$name = $file -> getOwner () -> label ();
2011-07-03 17:58:46 +00:00
$replacements [ $original ] = $sanitize ? check_plain ( $name ) : $name ;
2009-08-19 20:19:37 +00:00
break ;
}
}
2013-04-18 07:24:35 +00:00
if ( $date_tokens = $token_service -> findWithPrefix ( $tokens , 'timestamp' )) {
2013-06-15 08:46:11 +00:00
$replacements += $token_service -> generate ( 'date' , $date_tokens , array ( 'date' => $file -> getChangedTime ()), $options );
2009-08-19 20:19:37 +00:00
}
2013-06-15 08:46:11 +00:00
if (( $owner_tokens = $token_service -> findWithPrefix ( $tokens , 'owner' )) && $file -> getOwner ()) {
$replacements += $token_service -> generate ( 'user' , $owner_tokens , array ( 'user' => $file -> getOwner ()), $options );
2009-08-19 20:19:37 +00:00
}
}
return $replacements ;
}