degov-8.x-2.0/modules/degov_tweets/degov_tweets.module

modules/degov_tweets/degov_tweets.module
<?php

/**
 * @file
 * Contains all hooks and custom functions for the tweets module.
 */

/**
 * Implements hook_theme().
 */
function degov_tweets_theme($existing, $type, $theme, $path) {
  return [
    'degov_tweets' => [
      'variables' => [
        'tweets' => NULL,
      ],
    ],
  ];
}

/**
 * Implements template_preprocess_HOOK().
 */
function template_preprocess_degov_tweets(&$variables) {
  $variables['attributes']['class'][] = 'tweets';
  $tweets = $variables['tweets'];
  $items = [];
  if (!empty($tweets)) {
    // Processs each tweet for rendering.
    foreach ($tweets as $tweet) {
      $settings = [
        '#type' => 'processed_text',
        '#text' => degov_tweets_parse_tweet($tweet->text),
        '#format' => 'rich_text',
        '#filter_types_to_skip' => [],
      ];
      $text = \Drupal::service('renderer')->renderPlain($settings);
      $items[] = [
        'id' => $tweet->id,
        'text' => $text,
        'time' => $tweet->created_at,
        'time_ago' => \Drupal::service('date.formatter')
          ->formatInterval(REQUEST_TIME - strtotime($tweet->created_at)),
        'username' => $tweet->user->name,
        'screen_name' => $tweet->user->screen_name,
        'user_url' => $tweet->user->url,
        'avatar' => $tweet->user->profile_image_url_https,
      ];
    }
  }
  $variables['items'] = $items;
  // Attach slider library.
  $variables['#attached']['library'][] = 'degov_common/slick';
}

/**
 * Replace hashtags, user mentions and links with appropriate twitter links.
 *
 * @param string $text
 *   Contains text to be replaced.
 *
 * @return string
 *   Return the processed text string.
 */
function degov_tweets_parse_tweet($text) {
  // Links replacement.
  $text = preg_replace('@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@', '<a target="_blank" href="$1">$1</a>', $text);
  // User replacement.
  $text = preg_replace('/@(\w+)/', '<a target="_blank" href="https://twitter.com/$1">@$1</a>', $text);
  // Hashtag replacement.
  $text = preg_replace('/\s+#(\w+)/', ' <a target="_blank" href="https://twitter.com/hashtag/$1">#$1</a>', $text);

  return $text;
}

/**
 * Implements hook_locale_translation_projects_alter().
 */
function degov_tweets_locale_translation_projects_alter(&$projects) {
  $projects['degov_tweets'] = [
    'info' => [
      'interface translation project' => 'degov_tweets',
      'interface translation server pattern' => drupal_get_path('module', 'degov_tweets').'/translations/%language.po',
    ],
  ];
}

function degov_tweets_preprocess_block(&$variables) {
  if (isset($variables['elements']['#base_plugin_id']) && $variables['elements']['#base_plugin_id'] == 'degov_twitter_block') {
    $variables['#attached']['library'][] = 'degov_common/slick';
    $markup = render($variables['content']);

    if (!empty($markup)) {
      $variables['#attached']['drupalSettings']['degov_social_media_settings']['code']['tweetslider'] = $markup;
      $variables['attributes']['class'][] = 'js-social-media-wrapper';
      $variables['attributes']['data-social-media-source'] = 'twitter';
      $variables['attributes']['data-social-media-entity'] = 'tweetslider';
      $variables['social_media_disabled'] = TRUE;
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc