Created
April 26, 2012 13:38
-
-
Save mrvdb/2499671 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @file | |
| * Input filter which adds or rewrites a title attribute to links. The title | |
| * is taken form the pagetitle where the link is pointing to. | |
| * | |
| * @author Rolf Meijer for Studio Karu | |
| * @see http://drupal.org/user/358052 | |
| */ | |
| /** | |
| * Implements hook_help(). | |
| */ | |
| function linktitle_help($path, $arg) { | |
| switch ($path) { | |
| case 'admin/help#linktitle': | |
| $output = '<p>' . t('To use the Links Title filter, simply enable the' | |
| . ' Links Title filter for one or more of your' | |
| . ' <a href="@text_formats">text formats</a>.', array( | |
| '@text_formats' => url('admin/config/content/formats'))) . '</p><p>'; | |
| $output .= t('If you also enable other filters that act on links make' | |
| . ' sure that the one that needs to take precedence is the last one' | |
| . ' called in the Filter processing porder.') . '</p>'; | |
| return $output; | |
| } | |
| } | |
| /** | |
| * Implements hook_filter_info(). | |
| */ | |
| function linktitle_filter_info() { | |
| $filters['linktitle'] = array( | |
| 'title' => t('Adds a title attribute to links'), | |
| 'description' => t('Adds a title attribute to links, with it\'s value' | |
| . ' taken from the pagetitle where the link is pointing to.'), | |
| 'process callback' => '_linktitle_filter_process', | |
| 'settings callback' => '_linktitle_filter_settings', | |
| 'default settings' => array( | |
| 'linktitle_display_tip' => 1, | |
| 'linktitle_maxread_bytes' => 2000, | |
| ), | |
| 'tips callback' => '_linktitle_filter_tips', | |
| ); | |
| return $filters; | |
| } | |
| /** | |
| * Settings callback for the linktitle filter. | |
| */ | |
| function _linktitle_filter_settings($form, &$form_state, $filter, $format, $defaults) { | |
| $filter->settings += $defaults; | |
| $settings['linktitle_maxread_bytes'] = array( | |
| '#type' => 'textfield', | |
| '#title' => t('Maximum number of bytes read'), | |
| '#default_value' => $filter->settings['linktitle_maxread_bytes'], | |
| '#maxlength' => 8, | |
| '#description' => t('Number of bytes read from the page that is linked to.' | |
| . ' The title-tag should be in those read bytes,' | |
| . ' if you miss a title from a page,' | |
| . ' it is possible that the title tag is after those read bytes.' | |
| . ' In that case increase this number.'), | |
| ); | |
| return $settings; | |
| } | |
| /** | |
| * Filter process callback for linktitle filter. | |
| * | |
| * @param $text | |
| * Text to be filtered. Regex pattern matches links to be processed. | |
| * | |
| * @return | |
| * Filtered text with processed links including HTML title attributes | |
| */ | |
| function _linktitle_filter_process($text, $filter) { | |
| // Match on the url in the link tag | |
| $pattern = '%<a([^>]*?href="([^"]+?)"[^>]*?)>%i'; | |
| $maxread = $filter->settings['linktitle_maxread_bytes']; | |
| function _process_text($matches) { | |
| if (strpos($matches[1], 'title=') == FALSE) { | |
| $pagetitle = check_plain(get_remotetitle($matches[2], $maxread)); | |
| return substr_replace($matches[0], 'title='.$insert_title, -1, -1); | |
| } else { | |
| return $matches[0]; | |
| } | |
| } | |
| $replacement = preg_replace_callback($pattern,'_process_text', $text); | |
| return $replacement; | |
| } | |
| function get_remotetitle($url, $bytes = 5000) | |
| { | |
| // Init | |
| $dat = ''; $fp = fopen($url,"r"); | |
| if ($fp) { | |
| $dat = fread($fp, $bytes); | |
| fclose($fp); | |
| } | |
| if (preg_match('|<title>[[:space:]]*(.*)[[:space:]]*</title>|Uis', $dat, $match )) { | |
| $dat = $match[1]; | |
| } | |
| else { | |
| $dat = ''; | |
| } | |
| return $dat; | |
| } | |
| /** | |
| * Filter tips callback for linktitle filter. | |
| */ | |
| function _linktitle_filter_tips($filter, $format, $long = FALSE) { | |
| if ($filter->settings['linktitle_display_tip']) { | |
| return t('Adds a title attribute to links found in the content'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment