You are here

tweet_feed.module in Tweet Feed 4.x

Contains tweet_feed.module.

File

tweet_feed.module
View source
<?php

/**
 * @file
 * Contains tweet_feed.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function tweet_feed_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the tweet_feed module.
    case 'help.page.tweet_feed':
      $output = '<h4>' . t('Overview') . '</h4>';
      $output .= '<p>' . t('The Tweet Feed module is an advanced importing, displaying and data association module that allows you to pull in tweets by search, user, or list. The parameters of what is pulled in falls under the guidelines of ') . '<a href="https://developer.twitter.com/en/docs/rate-limits">' . t('Twitter\'s REST API') . '.</a></p>';
      $output .= '<p>' . t('Tweets can be displayed as nodes or in views as well as displayed by hash tag or user mention. All hash tags and user mentions are stored as references im the tweet nodes to their corresponding taxonomy term. This gives you great power in terms of displaying tweets with specific content in specific places by leveraging the power of contextual filters and taxonomies.</p>');
      return $output;
      break;
    default:
      break;
  }
}

/**
 * Implements hook_theme().
 */
function tweet_feed_theme() {
  return [
    'tweet_feed' => [
      'render element' => 'children',
    ],
  ];
}

/**
 * Format Tweet Output to HTML
 *
 * Makes links, hash tags, and usernames clickable.
 */
function tweet_feed_format_output($tweet, $new_window = FALSE, $hash_taxonomy = FALSE, $hashtags = array()) {

  /* based on our preference, assign all links to new windows or to the same window */
  $target = $new_window == 1 ? '_blank' : '_self';

  // Look for links and make them clickable
  $tweet = preg_replace('/(((f|ht){1}tp:\\/\\/)[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]+)/i', '<a target="' . $target . '" href="\\1">\\1</a>', $tweet);
  $tweet = preg_replace('/(((f|ht){1}tps:\\/\\/)[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]+)/i', '<a target="' . $target . '" href="\\1">\\1</a>', $tweet);
  $tweet = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]+)/i', '\\1<a target="' . $target . '" href="http:\\/\\/\\2">\\2</a>', $tweet);
  $tweet = preg_replace('/([_\\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\\.)+[a-z]{2,3})/i', '<a href="mailto:\\1">\\1</a>', $tweet);

  // Look for twitter handles and make them clickable
  // Modified so that slashes in the twitter handle are counted
  $pattern = '/@([A-Za-z0-9_]{1,15})(?![.A-Za-z])/';
  $replace = '<a target="' . $target . '" href="http://twitter.com/' . strtolower('\\1') . '">@\\1</a>';
  $tweet = preg_replace($pattern, $replace, $tweet);

  // Look for twitter hash tags and make them clickable
  // Modified so that slashes in the twitter handle are counted
  // Modified to link to taxonomy term if that checkbox is selected for this feed.
  $tweet = preg_replace('/(^|\\s)#(\\w*+)/u', '\\1<a target="' . $target . '" href="http://twitter.com/search?q=%23\\2">#\\2</a>', $tweet);
  return $tweet;
}

/**
 * Filter iconv from text.
 */
function tweet_feed_filter_iconv_text($text, $replace = '--') {

  // The tweet author goes into the title field
  // Filter it cleanly since it is going into the title field. If we cannot use iconv,
  // then use something more primitive, but effective
  // @see https://www.drupal.org/node/1910376
  // @see http://webcollab.sourceforge.net/unicode.html
  // Reject overly long 2 byte sequences, as well as characters above U+10000
  // and replace with --.
  $altered = preg_replace('/[\\x00-\\x08\\x10\\x0B\\x0C\\x0E-\\x19\\x7F]' . '|[\\x00-\\x7F][\\x80-\\xBF]+' . '|([\\xC0\\xC1]|[\\xF0-\\xFF])[\\x80-\\xBF]*' . '|[\\xC2-\\xDF]((?![\\x80-\\xBF])|[\\x80-\\xBF]{2,})' . '|[\\xE0-\\xEF](([\\x80-\\xBF](?![\\x80-\\xBF]))|(?![\\x80-\\xBF]{2})|[\\x80-\\xBF]{3,})/S', '--', $text);

  // Reject overly long 3 byte sequences and UTF-16 surrogates and replace
  // with --.
  return preg_replace('/\\xE0[\\x80-\\x9F][\\x80-\\xBF]' . '|\\xED[\\xA0-\\xBF][\\x80-\\xBF]/S', $replace, $altered);
}

/**
 * Filter smart quotes to ASCII equivalent.
 *
 * @param string $text
 *   Input text to filter.
 *
 * @return string $text
 *   Filtered text.
 */
function tweet_feed_filter_smart_quotes($text) {

  // Convert varieties of smart quotes to ACSII equivalent.
  $search = array(
    chr(0xe2) . chr(0x80) . chr(0x98),
    chr(0xe2) . chr(0x80) . chr(0x99),
    chr(0xe2) . chr(0x80) . chr(0x9c),
    chr(0xe2) . chr(0x80) . chr(0x9d),
    chr(0xe2) . chr(0x80) . chr(0x93),
    chr(0xe2) . chr(0x80) . chr(0x94),
  );
  $ascii_replace = array(
    "'",
    "'",
    '"',
    '"',
    '-',
    '&mdash;',
  );
  return str_replace($search, $ascii_replace, $text);
}

Functions

Namesort descending Description
tweet_feed_filter_iconv_text Filter iconv from text.
tweet_feed_filter_smart_quotes Filter smart quotes to ASCII equivalent.
tweet_feed_format_output Format Tweet Output to HTML
tweet_feed_help Implements hook_help().
tweet_feed_theme Implements hook_theme().