You are here

twitter_feed.module in Twitter_Feed 8

Twitter module hooks.

File

twitter_feed.module
View source
<?php

/**
 * @file
 * Twitter module hooks.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_theme().
 */
function twitter_feed_theme($existing, $type, $theme, $path) {
  return [
    'twitter_feed_item' => [
      'template' => 'twitter-feed-item',
      'variables' => [
        'tweet' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_libraries_info().
 */
function twitter_feed_libraries_info() {
  $libraries['timeago'] = [
    'name' => 'TimeAgo',
    'vendor url' => 'http://timeago.yarp.com',
    'download url' => 'https://github.com/rmm5t/jquery-timeago/zipball/v1.5.2',
    'version arguments' => [
      'file' => 'jquery.timeago.js',
      // @version 1.5.2
      'pattern' => '@version\\s+([0-9a-zA-Z\\.-]+)@',
      'lines' => 10,
    ],
    'files' => [
      'js' => [
        'jquery.timeago.js',
      ],
    ],
  ];
  return $libraries;
}

/**
 * Implements hook_library_info_build().
 *
 * We also define Timeago through the core library callbacks.
 */
function twitter_feed_library_info_build() {
  $lib = twitter_feed_libraries_info();
  $libraries = [];
  if (!function_exists('libraries_get_path')) {
    return NULL;
  }
  $library_path = libraries_get_path('timeago');
  if (strpos($library_path, '/') !== 0) {
    $library_path = '/' . $library_path;
  }
  $libraries['timeago'] = [
    'title' => $lib['timeago']['name'],
    'remote' => $lib['timeago']['vendor url'],
    'version' => '1.5.2',
    'license' => [
      'name' => 'MIT',
      'url' => 'https://github.com/rmm5t/jquery-timeago/raw/master/LICENSE.txt',
      'gpl-compatible' => TRUE,
    ],
    'dependencies' => [
      'core/jquery',
    ],
    'js' => [
      $library_path . '/jquery.timeago.js' => [
        'scope' => 'footer',
      ],
    ],
  ];
  $languages = twitter_feed_timeago_languages();
  foreach ($languages as $language_name) {
    $libraries['timeago_' . $language_name] = [
      'title' => $lib['timeago']['name'],
      'remote' => $lib['timeago']['vendor url'],
      'version' => '1.5.2',
      'license' => [
        'name' => 'MIT',
        'url' => 'https://github.com/rmm5t/jquery-timeago/raw/master/LICENSE.txt',
        'gpl-compatible' => TRUE,
      ],
      'dependencies' => [
        'core/jquery',
      ],
      'js' => [
        $library_path . '/locales/jquery.timeago.' . $language_name . '.js' => [
          'scope' => 'footer',
        ],
      ],
    ];
  }
  return $libraries;
}

/**
 * Get timeago languages.
 *
 * @return array
 *   The sorted timeago languages.
 */
function twitter_feed_timeago_languages() {
  if (!function_exists('libraries_get_path')) {
    return [];
  }
  $path = libraries_get_path('timeago');
  $files = \Drupal::service('file_system')
    ->scanDirectory("{$path}/locales", '/timeago\\..{2,14}\\.js$/i', [
    'recurse' => FALSE,
    'key' => 'name',
  ]);
  $timeago_locales = [];
  if ($files) {
    foreach ($files as $name => $file) {
      preg_match('/timeago\\.(.{2,14})$/i', $name, $matches);
      if ($matches[1]) {
        $timeago_locales[$matches[1]] = $matches[1];
      }
    }
  }
  ksort($timeago_locales);
  return $timeago_locales;
}

/**
 * Implements hook_preprocess_twitter_feed_item().
 */
function template_preprocess_twitter_feed_item(&$vars) {
  $tweet = $vars['tweet'];

  // Creates a fake input_format so we can use some internal Drupal filters.
  $fake_filter = new stdClass();
  $fake_filter->settings = [
    'filter_url_length' => 72,
  ];

  // Converts URLs to links and line-breaks to paragraphs.
  // TODO hashtags and usernames.
  $vars['text'] = [
    '#markup' => _filter_autop(_filter_url($tweet->full_text, $fake_filter)),
  ];

  // This is the datestamp that jQuery timeago expects.
  $vars['date']['stamp'] = date('c', strtotime($tweet->created_at));
  $vars['date']['clean'] = \Drupal::service('date.formatter')
    ->format(strtotime($tweet->created_at), 'short');
}

/**
 * Implements hook_help().
 *
 * @inheritdoc
 */
function twitter_feed_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.twitter_feed':
      $text = file_get_contents(dirname(__FILE__) . "/README.md");
      if (!\Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . $text . '</pre>';
      }
      else {

        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        return $filter
          ->process($text, 'en');
      }
  }
  return NULL;
}

Functions

Namesort descending Description
template_preprocess_twitter_feed_item Implements hook_preprocess_twitter_feed_item().
twitter_feed_help Implements hook_help().
twitter_feed_libraries_info Implements hook_libraries_info().
twitter_feed_library_info_build Implements hook_library_info_build().
twitter_feed_theme Implements hook_theme().
twitter_feed_timeago_languages Get timeago languages.