link_iframe_formatter.module in Link iframe formatter 7
Same filename and directory in other branches
Defines a formatter that renders a link like an iframe
File
link_iframe_formatter.moduleView source
<?php
/**
* @file
* Defines a formatter that renders a link like an iframe
*/
/**
* Implements hook_menu().
*/
function link_iframe_formatter_menu() {
$items = array();
$items['admin/config/content/link_iframe_formatter'] = array(
'title' => 'Link iframe blacklist',
'description' => 'Config blacklist for link iframe formatter',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'link_iframe_formatter_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_field_formatter_info().
*/
function link_iframe_formatter_field_formatter_info() {
return array(
'link_iframe_formatter_iframe' => array(
'label' => t('Iframe'),
'field types' => array(
'link_field',
),
'settings' => array(
'width' => '640',
'height' => '480',
'style' => '',
'class' => '',
'original' => FALSE,
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function link_iframe_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$settings = $instance['display'][$view_mode]['settings'];
$element = array();
$element['width'] = array(
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $settings['width'],
'#required' => TRUE,
);
$element['height'] = array(
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $settings['height'],
'#required' => TRUE,
);
$element['style'] = array(
'#title' => t('Style'),
'#type' => 'textfield',
'#default_value' => $settings['style'],
'#required' => FALSE,
);
$element['class'] = array(
'#title' => t('Class'),
'#type' => 'textfield',
'#default_value' => $settings['class'],
'#required' => FALSE,
);
$element['original'] = array(
'#title' => t('Show original link'),
'#type' => 'radios',
'#options' => array(
TRUE => t('On'),
FALSE => t('Off'),
),
'#default_value' => $settings['original'],
'#required' => FALSE,
);
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function link_iframe_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
$settings = $instance['display'][$view_mode]['settings'];
$summary = t('Width: @width, Height: @height, Class: @class, Original link is @original and Style is @style', array(
'@width' => $settings['width'],
'@height' => $settings['height'],
'@style' => empty($settings['style']) ? t('Off') : t('On'),
'@class' => empty($settings['class']) ? 'None' : $settings['class'],
'@original' => $settings['original'] ? t('On') : t('Off'),
));
return $summary;
}
/**
* Page callback: Link image formatter settings form
*
* @see link_iframe_formatter_menu()
*/
function link_iframe_formatter_settings($form, &$form_state) {
// no need to check URL validity because invalid URL is not going to pass "link" field check anyway.
$form['link_iframe_formatter_blacklist'] = array(
'#type' => 'textarea',
'#title' => t('Link iframe blacklist'),
'#default_value' => variable_get('link_iframe_formatter_blacklist', implode("\n", array(
//'# below is a domain black list',
'*http://www.nytimes.com',
'*http://google.com',
))),
'#description' => t('URLs in blacklist will display as links instead of iframe. One URL per line. Use "*" at the beginning of the line as a domain name. Use "#" at the beginning of line for comments.'),
);
$return = system_settings_form($form);
$return['#submit'][] = '_link_iframe_formatter_clear_cache';
return $return;
}
function _link_iframe_formatter_clear_cache() {
cache_clear_all('link_iframe_formatter_blacklist_table', 'cache');
}
/**
* verify url is in blacklist or not
* @param $url: the link url
* @return bool true if $url is in the blacklist
*/
function link_iframe_formatter_check_blacklist($url) {
// retrieve cache
$table =& drupal_static(__FUNCTION__);
if (!isset($table)) {
if ($cache = cache_get('link_iframe_formatter_blacklist_table')) {
$table = $cache->data;
}
else {
// set the cache tables.
$domains = array();
$urls = array();
$list = preg_split('/$\\R?^/m', variable_get('link_iframe_formatter_blacklist', ''));
foreach ($list as $line) {
if ($line[0] == '#') {
// skip comments
continue;
}
else {
if ($line[0] == '*') {
// strip off '_', which is invalid char after parse_url().
$domain = trim(parse_url(substr($line, 1), PHP_URL_HOST), "_\n\t\r\0\v");
$domains[$domain] = $domain;
}
else {
// we don't do validation because it's not necessary.
// if a blacklist link is not valid URL, it's still not getting displayed.
$link = link_cleanup_url($line);
$urls[$link] = $link;
}
}
}
$table = array(
'domains' => $domains,
'urls' => $urls,
);
cache_set('link_iframe_formatter_blacklist_table', $table, 'cache');
}
}
// fast check if url is in domain
$domain = parse_url($url, PHP_URL_HOST);
if (array_key_exists($domain, $table['domains'])) {
return TRUE;
}
$url = link_cleanup_url($url);
if (array_key_exists($url, $table['urls'])) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_formatter_view().
*/
function link_iframe_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$elements = array();
if ($display['type'] == 'link_iframe_formatter_iframe') {
foreach ($items as $delta => $item) {
$url = $item['url'];
if (link_iframe_formatter_check_blacklist($url)) {
// if it's in blacklist, show link only.
$elements[$delta] = array(
'#markup' => theme('link_iframe_formatter_blacklist_notice', array(
'element' => $item,
)),
);
}
else {
//not in blacklist, show url in iframe
$link_options = $item;
$settings = $display['settings'];
unset($link_options['title']);
unset($link_options['url']);
// TODO: if some settings are not set, do set them.
$iframe_element = array(
'#type' => 'html_tag',
'#tag' => 'iframe',
'#attributes' => array(
'name' => check_plain($item['title']),
'src' => url($item['url'], $link_options),
'height' => $settings['height'],
'width' => $settings['width'],
'style' => $settings['style'],
'id' => drupal_html_id('link-iframe-' . $instance['bundle'] . '-' . $instance['field_name'] . '-' . $delta),
'class' => drupal_html_class($settings['class']),
),
'#value' => '',
);
// add original link if set to On.
if ($settings['original']) {
$elements[$delta] = array(
'original' => array(
'#markup' => theme('link_iframe_formatter_original', array(
'element' => $item,
)),
),
'iframe' => $iframe_element,
);
}
else {
$elements[$delta] = $iframe_element;
}
}
}
}
return $elements;
}
/**
* Implements hook_theme().
*/
function link_iframe_formatter_theme() {
return array(
'link_iframe_formatter_original' => array(
'variables' => array(
'element' => NULL,
),
),
'link_iframe_formatter_blacklist_notice' => array(
'variables' => array(
'element' => NULL,
),
),
);
}
function theme_link_iframe_formatter_blacklist_notice($variables) {
$link_options = $variables['element'];
$link = l($link_options['title'], $link_options['url'], $link_options);
return '<div class="link-iframe-formatter-blacklist-notice">Please click the original link at: ' . $link . '<div>';
}
function theme_link_iframe_formatter_original($variables) {
$link_options = $variables['element'];
$link = l($link_options['title'], $link_options['url'], $link_options);
return '<div class="link-iframe-formatter-original">You may view the original link at: ' . $link . '<div>';
}
Functions
Name | Description |
---|---|
link_iframe_formatter_check_blacklist | verify url is in blacklist or not |
link_iframe_formatter_field_formatter_info | Implements hook_field_formatter_info(). |
link_iframe_formatter_field_formatter_settings_form | Implements hook_field_formatter_settings_form(). |
link_iframe_formatter_field_formatter_settings_summary | Implements hook_field_formatter_settings_summary(). |
link_iframe_formatter_field_formatter_view | Implements hook_field_formatter_view(). |
link_iframe_formatter_menu | Implements hook_menu(). |
link_iframe_formatter_settings | Page callback: Link image formatter settings form |
link_iframe_formatter_theme | Implements hook_theme(). |
theme_link_iframe_formatter_blacklist_notice | |
theme_link_iframe_formatter_original | |
_link_iframe_formatter_clear_cache |