EclipseGc

Token Plugins

Jul 9th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\user\Plugins\core\tokens;
  4.  
  5. use Drupal\Component\Plugin\PluginAbstract;
  6.  
  7. class UserTokens extends PluginAbstract {
  8.   public function __construct(array $data) {
  9.     $this->account = $data['user'];
  10.     $this->url_options = array('absolute' => TRUE);
  11.     $this->language_code = $this->getLangCode($data);
  12.     $this->sanitize = !empty($options['sanitize']);
  13.   }
  14.  
  15.   protected function getLangCode(array $data) {
  16.     if (isset($options['language'])) {
  17.       $this->url_options['language'] = $options['language'];
  18.       $this->language_code = $options['language']->language;
  19.     }
  20.     else {
  21.       $this->language_code = NULL;
  22.     }
  23.   }
  24.  
  25.   public function uidToken() {
  26.     return !empty($this->account->uid) ? $this->account->uid : t('not yet assigned');
  27.   }
  28.  
  29.   public function nameToken() {
  30.     $name = format_username($this->account);
  31.     return $this->sanitize ? check_plain($name) : $name;
  32.   }
  33.  
  34.   public function mailToken() {
  35.     return $this->sanitize ? check_plain($this->account->mail) : $this->account->mail;
  36.   }
  37.  
  38.   public function urlToken() {
  39.     return !empty($this->account->uid) ? url("user/$this->account->uid", $this->url_options) : t('not yet assigned');
  40.   }
  41.  
  42.   public function editUrlToken() {
  43.     return !empty($this->account->uid) ? url("user/$this->account->uid/edit", $this->url_options) : t('not yet assigned');
  44.   }
  45.  
  46.   public function lastLoginToken() {
  47.     return !empty($this->account->login) ? format_date($this->account->login, 'medium', '', NULL, $this->language_code) : t('never');
  48.   }
  49.  
  50.   public function createdToken() {
  51.     return !empty($this->account->created) ? format_date($this->account->created, 'medium', '', NULL, $this->language_code) : t('not yet created');
  52.   }
  53.  
  54.   public function __call($string, $arguments) {
  55.     // This portion of the method should likely exist on an abstract for tokens
  56.     $method = $string . 'Token';
  57.     if (method_exists($this, $method)) {
  58.       return call_user_method_array($method, $this, $arguments);
  59.     }
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment