You are here

function sharerich_tokens in Sharerich 8

Same name and namespace in other branches
  1. 7.3 includes/sharerich.tokens.inc \sharerich_tokens()
  2. 7 includes/sharerich.tokens.inc \sharerich_tokens()
  3. 7.2 includes/sharerich.tokens.inc \sharerich_tokens()

Implements hook_tokens().

@todo Support Taxonomy and User tokens.

File

./sharerich.tokens.inc, line 56
Tokens for Sharerich.

Code

function sharerich_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if ($type == 'sharerich') {
    foreach ($tokens as $name => $original) {
      $value = '';
      switch ($name) {
        case 'fb_app_id':
          $value = Html::escape(\Drupal::config('sharerich.settings')
            ->get('facebook_app_id'));
          break;
        case 'fb_site_url':
          $value = Html::escape(\Drupal::config('sharerich.settings')
            ->get('facebook_site_url'));
          break;
        case 'youtube_username':
          $value = Html::escape(\Drupal::config('sharerich.settings')
            ->get('youtube_username'));
          break;
        case 'github_username':
          $value = Html::escape(\Drupal::config('sharerich.settings')
            ->get('github_username'));
          break;
        case 'instagram_username':
          $value = Html::escape(\Drupal::config('sharerich.settings')
            ->get('instagram_username'));
          break;
        case 'twitter_user':
          $value = Html::escape(\Drupal::config('sharerich.settings')
            ->get('twitter_user'));
          break;
        case 'url':

          // Token fallback.
          $tokens = [
            '[node:url]',
            '[term:url]',
            '[current-page:url]',
            '[site:url]',
          ];
          foreach ($tokens as $token) {
            $value = \Drupal::token()
              ->replace($token, $data, [
              'clear' => TRUE,
            ]);
            if (!empty($value)) {
              $value = rawurlencode($value);
              break;
            }
          }
          break;
        case 'title':

          // Token fallback.
          $tokens = [
            '[node:title]',
            '[term:name]',
            '[current-page:title]',
            '[site:name]',
          ];
          foreach ($tokens as $token) {
            $value = \Drupal::token()
              ->replace($token, $data, [
              'clear' => TRUE,
            ]);
            if (!empty($value)) {
              $value = rawurlencode(PlainTextOutput::renderFromHtml(htmlspecialchars_decode($value)));
              break;
            }
          }
          break;
        case 'summary':
        case 'description':

          // Token fallback.
          $tokens = [
            '[node:summary]',
            '[term:description]',
            '[current-page:title]',
            '[site:slogan]',
          ];
          foreach ($tokens as $token) {
            $value = \Drupal::token()
              ->replace($token, $data, [
              'clear' => TRUE,
            ]);
            if (!empty($value)) {
              $value = rawurlencode(PlainTextOutput::renderFromHtml(htmlspecialchars_decode($value)));
              break;
            }
          }
          break;
      }
      $replacements[$original] = $value;
    }
    return $replacements;
  }
}