Created
May 25, 2012 10:47
-
-
Save cbeier/2787268 to your computer and use it in GitHub Desktop.
Field Formatter to add an Read More Link in text body
This file contains 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
/** | |
* Implements hook_field_formatter_info(). | |
*/ | |
function hook_field_formatter_info() { | |
return array( | |
// The 'summary or trimmed' field formatter for text_with_summary | |
// fields displays returns the summary element of the field or, if | |
// the summary is empty, the trimmed version of the full element | |
// of the field. | |
// If the Read More link option is used, a link will be placed after | |
// the text. | |
'text_summary_or_trimmed_link' => array( | |
'label' => t('Summary or trimmed with link'), | |
'field types' => array('text_with_summary'), | |
'settings' => array( | |
'trim_length' => 600, | |
'link' => 'content', | |
), | |
), | |
); | |
} | |
/** | |
* Implements hook_field_formatter_settings_form(). | |
*/ | |
function hook_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { | |
$display = $instance['display'][$view_mode]; | |
$settings = $display['settings']; | |
$element = array(); | |
if (strpos($display['type'], '_trimmed_link') !== FALSE) { | |
$element['trim_length'] = array( | |
'#title' => t('Trim length'), | |
'#type' => 'textfield', | |
'#size' => 10, | |
'#default_value' => $settings['trim_length'], | |
'#element_validate' => array('element_validate_integer_positive'), | |
'#required' => TRUE, | |
); | |
$element['read_more'] = array( | |
'#title' => t('Read more link'), | |
'#type' => 'textfield', | |
'#size' => 50, | |
'#default_value' => $settings['read_more'], | |
); | |
} | |
return $element; | |
} | |
/** | |
* Implements hook_field_formatter_settings_summary(). | |
*/ | |
function hook_field_formatter_settings_summary($field, $instance, $view_mode) { | |
$display = $instance['display'][$view_mode]; | |
$settings = $display['settings']; | |
$summary = ''; | |
if (strpos($display['type'], '_trimmed_link') !== FALSE) { | |
$summary = t('Trim length') . ': ' . $settings['trim_length']; | |
} | |
if (!empty($settings['read_more'])) { | |
$summary .= '<br />' . t('Link') . ': ' . $settings['read_more']; | |
} | |
return $summary; | |
} | |
/** | |
* Implements hook_field_formatter_view(). | |
*/ | |
function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { | |
$element = array(); | |
if ($display['type'] == 'text_summary_or_trimmed_link') { | |
foreach ($items as $delta => $item) { | |
if (!empty($item['summary'])) { | |
$output = _text_sanitize($instance, $langcode, $item, 'summary'); | |
} | |
else { | |
$output = _text_sanitize($instance, $langcode, $item, 'value'); | |
$output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']); | |
} | |
// Add the read more link. | |
if (!empty($display['settings']['read_more'])) { | |
$read_more_link = l(t($display['settings']['read_more']), 'node/' . $entity->nid); | |
$output = preg_replace('/<\/p>$/m', ' ' . $read_more_link . '</p>', $output, 1); | |
} | |
$element[$delta] = array('#markup' => $output); | |
} | |
} | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment