| 1 |
<?php
|
| 2 |
// $Id: ed_readmore.module,v 1.5.8.2.2.11.2.1 2009/04/21 21:01:54 toddnienkerk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Customize the "Read more" link shown in teasers.
|
| 7 |
*
|
| 8 |
* Project homepage:
|
| 9 |
* http://drupal.org/project/ed_readmore
|
| 10 |
*
|
| 11 |
* This provides the customization described at:
|
| 12 |
* http://www.angrydonuts.com/the_nuisance_of_the_read_more_fl
|
| 13 |
*
|
| 14 |
* Conversion from 4.7 to 5.x done by themoebius (zacwitte@gmail.com)
|
| 15 |
* Conversion from 5.x to 6.x done by Exodus Development and tomaszx
|
| 16 |
* (tomaszx provided initial conversion and patches -- thanks!)
|
| 17 |
* 6.x bug fixes, UI/theme improvements, and added functionality by Four Kitchens
|
| 18 |
*
|
| 19 |
* Exodus Development
|
| 20 |
* http://exodusdev.com
|
| 21 |
* exodusdev@gmail.com
|
| 22 |
*
|
| 23 |
* Four Kitchens
|
| 24 |
* http://fourkitchens.com
|
| 25 |
* shout@fourkitchens.com
|
| 26 |
*/
|
| 27 |
|
| 28 |
|
| 29 |
define('ED_READMORE_TEXT_DEFAULT', '<strong>'. t('Read more »') .'</strong>');
|
| 30 |
define('ED_READMORE_TITLE_DEFAULT', t('Read the whole post'));
|
| 31 |
define('ED_READMORE_PLACEMENT_DEFAULT', 'inline');
|
| 32 |
define('ED_READMORE_RSS_DEFAULT', TRUE);
|
| 33 |
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implementation of hook_menu().
|
| 37 |
*/
|
| 38 |
function ed_readmore_menu() {
|
| 39 |
$items['admin/settings/ed_readmore'] = array(
|
| 40 |
'title' => 'Read More link',
|
| 41 |
'description' => 'Configures the <strong>Read More</strong> link that appears in node teasers.',
|
| 42 |
'page callback' => 'drupal_get_form',
|
| 43 |
'page arguments' => array('ed_readmore_admin_settings'),
|
| 44 |
'access arguments' => array('administer site configuration'),
|
| 45 |
'type' => MENU_NORMAL_ITEM,
|
| 46 |
);
|
| 47 |
return $items;
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Displays the settings form.
|
| 53 |
*/
|
| 54 |
function ed_readmore_admin_settings() {
|
| 55 |
$form = array();
|
| 56 |
|
| 57 |
$form['ed_readmore_display'] = array(
|
| 58 |
'#type' => 'fieldset',
|
| 59 |
'#title' => t('Read More link display options'),
|
| 60 |
'#collapsible' => FALSE,
|
| 61 |
);
|
| 62 |
|
| 63 |
$form['ed_readmore_display']['ed_readmore_placement'] = array(
|
| 64 |
'#type' => 'radios',
|
| 65 |
'#title' => t('Link placement'),
|
| 66 |
'#options' => array(
|
| 67 |
'inline' => t('Add the <strong>Read More</strong> link inline. If this fails, add the link after the teaser.'),
|
| 68 |
'after' => t('Add the <strong>Read More</strong> link after the teaser.'),
|
| 69 |
'disable' => t('Do not add a <strong>Read More</strong> link to the teaser.'),
|
| 70 |
),
|
| 71 |
'#default_value' => variable_get('ed_readmore_placement', ED_READMORE_PLACEMENT_DEFAULT),
|
| 72 |
'#description' => t('For more information about how this is done, read this <a href="@link">Angry Donuts post</a>.', array('@link' => url('http://www.angrydonuts.com/the_nuisance_of_the_read_more_fl'))),
|
| 73 |
);
|
| 74 |
|
| 75 |
$form['ed_readmore_display']['ed_readmore_remove'] = array(
|
| 76 |
'#type' => 'checkbox',
|
| 77 |
'#title' => t('Remove <strong>Read More</strong> link from links section'),
|
| 78 |
'#default_value' => variable_get('ed_readmore_remove', TRUE),
|
| 79 |
'#description' => t('Also remove Drupal\'s default <strong>Read More</strong> link from node links.'),
|
| 80 |
);
|
| 81 |
|
| 82 |
$form['ed_readmore_formatting'] = array(
|
| 83 |
'#type' => 'fieldset',
|
| 84 |
'#title' => t('Read More link formatting and attributes'),
|
| 85 |
'#collapsible' => FALSE,
|
| 86 |
);
|
| 87 |
|
| 88 |
$form['ed_readmore_formatting']['ed_readmore_text'] = array(
|
| 89 |
'#type' => 'textfield',
|
| 90 |
'#title' => t('Link text'),
|
| 91 |
'#default_value' => variable_get('ed_readmore_text', ED_READMORE_TEXT_DEFAULT),
|
| 92 |
'#description' => t('Enter the text you wish to display in the <strong>Read More</strong> link. May contain HTML. Special characters should be encoded (like %raquo or %amp). Spaces will be changed to non-breaking spaces (%nbsp).', array('%raquo' => '»', '%amp' => '&', '%nbsp' => ' ')),
|
| 93 |
'#required' => TRUE,
|
| 94 |
);
|
| 95 |
|
| 96 |
$form['ed_readmore_formatting']['ed_readmore_title'] = array(
|
| 97 |
'#type' => 'textfield',
|
| 98 |
'#title' => t('Link title'),
|
| 99 |
'#default_value' => variable_get('ed_readmore_title', ED_READMORE_TITLE_DEFAULT),
|
| 100 |
'#description' => t('Enter the text you wish to be used as the title for the <strong>Read More</strong> link (the value of the %title attribute). It is used for accessibility and search engine optimization purposes and appears as a tooltip in some browsers.', array('%title' => 'title=""')),
|
| 101 |
'#required' => FALSE,
|
| 102 |
);
|
| 103 |
|
| 104 |
$form['ed_readmore_formatting']['ed_readmore_nofollow'] = array(
|
| 105 |
'#type' => 'checkbox',
|
| 106 |
'#title' => t('Make link nofollow'),
|
| 107 |
'#default_value' => variable_get('ed_readmore_nofollow', TRUE),
|
| 108 |
'#description' => t('Adds %nofollow to the link\'s attributes. Often used for search engine optimization purposes.', array('%nofollow' => 'rel="nofollow"')),
|
| 109 |
);
|
| 110 |
|
| 111 |
return system_settings_form($form);
|
| 112 |
}
|
| 113 |
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Implementation of template_preprocess_node().
|
| 117 |
*/
|
| 118 |
function ed_readmore_preprocess_node(&$variables) {
|
| 119 |
$display = variable_get('ed_readmore_placement', ED_READMORE_PLACEMENT_DEFAULT);
|
| 120 |
|
| 121 |
// Don't do anything if placing the link is disabled
|
| 122 |
if ($display != 'disable') {
|
| 123 |
// Check to make sure that:
|
| 124 |
// (1) This is a teaser
|
| 125 |
// (2) There's more to read
|
| 126 |
// (3) The teaser contains fewer characters than the node body (this also ensures the node body isn't empty)
|
| 127 |
if ($variables['teaser'] && $variables['readmore']) {
|
| 128 |
$variables['content'] = ed_readmore_link_place($variables['content'], $variables['node'], $display);
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
/**
|
| 135 |
* Place the "Read more" link in the correct location in the teaser.
|
| 136 |
*
|
| 137 |
* @param $teaser
|
| 138 |
* The teaser to place the "Read more" link into.
|
| 139 |
* @param $link
|
| 140 |
* The node containing information necessary to generate a link.
|
| 141 |
* @param $display
|
| 142 |
* Link display mode: inline, after, or disable.
|
| 143 |
* If 'inline,' try to insert into teaser. If 'added,' just add after the teaser.
|
| 144 |
*/
|
| 145 |
function ed_readmore_link_place($teaser, $node, $display) {
|
| 146 |
if ($display == 'inline') {
|
| 147 |
$block_tags = '(?:p)';
|
| 148 |
// Get last position of the last closing marker
|
| 149 |
// If found, insert the link before the marker
|
| 150 |
if (preg_match('!</?'. $block_tags .'[^>]*>$!i', $teaser, $match, PREG_OFFSET_CAPTURE)) {
|
| 151 |
$teaser = substr_replace($teaser, ' '. ed_readmore_link_render($node, $display), $match[0][1], 0);
|
| 152 |
}
|
| 153 |
else {
|
| 154 |
$display = 'after';
|
| 155 |
$teaser .= ed_readmore_link_render($node, $display); // Not found, so just append it
|
| 156 |
}
|
| 157 |
}
|
| 158 |
else {
|
| 159 |
$teaser .= ed_readmore_link_render($node, $display); // Not inline, so just append it
|
| 160 |
}
|
| 161 |
|
| 162 |
return $teaser;
|
| 163 |
}
|
| 164 |
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Implementation of hook_link_alter().
|
| 168 |
*/
|
| 169 |
function ed_readmore_link_alter(&$links, $node) {
|
| 170 |
$remove_link = variable_get('ed_readmore_remove', TRUE);
|
| 171 |
if ($remove_link) {
|
| 172 |
unset($links['node_read_more']);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
|
| 177 |
/**
|
| 178 |
* Prepares the link for theming and returns a rendered link.
|
| 179 |
*
|
| 180 |
* XSS checking and other safety measures are performed here to prevent
|
| 181 |
* themers from omitting them.
|
| 182 |
*/
|
| 183 |
function ed_readmore_link_render($node, $display = 'inline') {
|
| 184 |
// Filter for cross-site scripting (XSS)
|
| 185 |
// Replace spaces with non-breaking entities to prevent a wrapping in the link
|
| 186 |
$link_text = str_replace(' ', ' ', filter_xss(variable_get('ed_readmore_text', ED_READMORE_TEXT_DEFAULT)));
|
| 187 |
|
| 188 |
$link_destination = 'node/'. $node->nid;
|
| 189 |
|
| 190 |
// Make sure the text is plain
|
| 191 |
// Replace double quotes with single quotes to prevent breaking of the title attribute
|
| 192 |
$link_title = str_replace('"', '\'', check_plain(variable_get('ed_readmore_title', ED_READMORE_TITLE_DEFAULT)));
|
| 193 |
|
| 194 |
// Build link options array
|
| 195 |
$link_options = array(
|
| 196 |
'attributes' => array(
|
| 197 |
'title' => $link_title,
|
| 198 |
),
|
| 199 |
'html' => TRUE,
|
| 200 |
);
|
| 201 |
|
| 202 |
// Add rel="nofollow" to link if the option is enabled
|
| 203 |
if (variable_get('ed_readmore_nofollow', TRUE)) {
|
| 204 |
$link_options['attributes']['rel'] = 'nofollow';
|
| 205 |
}
|
| 206 |
|
| 207 |
// Send prepared data to the theme function
|
| 208 |
return theme('ed_readmore_link', $link_text, $link_destination, $link_options, $display);
|
| 209 |
}
|
| 210 |
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Implementation of hook_theme().
|
| 214 |
*/
|
| 215 |
function ed_readmore_theme($existing, $type, $theme, $path) {
|
| 216 |
return array(
|
| 217 |
'ed_readmore_link' => array(
|
| 218 |
'arguments' => array(
|
| 219 |
'link_text' => NULL,
|
| 220 |
'link_destination' => NULL,
|
| 221 |
'link_options' => NULL,
|
| 222 |
'display' => NULL,
|
| 223 |
),
|
| 224 |
),
|
| 225 |
);
|
| 226 |
}
|
| 227 |
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Theme function that wraps the rendered link.
|
| 231 |
*/
|
| 232 |
function theme_ed_readmore_link($link_text, $link_destination, $link_options, $display) {
|
| 233 |
// Use a <span> (inline) element for links that appear inside the teaser
|
| 234 |
$element = 'span';
|
| 235 |
|
| 236 |
// Use a <div> (block-level) element for links appended after the teaser
|
| 237 |
if ($display == 'after') {
|
| 238 |
$element = 'div';
|
| 239 |
}
|
| 240 |
|
| 241 |
return '<'. $element .' class="read-more">'. l($link_text, $link_destination, $link_options) .'</'. $element .'>';
|
| 242 |
}
|