| 1 |
<?php
|
| 2 |
// $Id: teaserbytype.module,v 1.2 2008/04/13 13:37:13 joachim Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The teaserbytype module allows site admins to set teaser length for each content type.
|
| 7 |
* by joachim (@ drupal.org)
|
| 8 |
* licensed under the GNU General Public License, version 2
|
| 9 |
*
|
| 10 |
* This module adds a teaser length setting to each content type settings page.
|
| 11 |
* This allows admins to choose a teaser length here.
|
| 12 |
* The site-wide setting applies to content types that set the value to 'Default' (which is of
|
| 13 |
* course the default: upon installation, all content types still take the site-wide setting).
|
| 14 |
*
|
| 15 |
* The extra setting is saved as a persistent variable of the form "teaser_length_$type",
|
| 16 |
* but only if it is changed from the default value.
|
| 17 |
* An extra form submit function, teaserbytype_node_type_form_submit,
|
| 18 |
* takes care of deleting the variable if the setting is restored to 'Default'.
|
| 19 |
*
|
| 20 |
* This allows us to merely check for the existence of the variable in hook_nodeapi,
|
| 21 |
* before passing on its value to node_teaser(), the teaser generating function in core.
|
| 22 |
*
|
| 23 |
* Node preview is a special case, as hook_nodeapi won't do the job here.
|
| 24 |
* Instead, another custom submit function alters the teaser in the validation process
|
| 25 |
* of the node preview submission.
|
| 26 |
*/
|
| 27 |
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Implementation of hook_help().
|
| 31 |
*/
|
| 32 |
function teaserbytype_help($section='') {
|
| 33 |
$output = '';
|
| 34 |
|
| 35 |
switch ($section) {
|
| 36 |
// main help page
|
| 37 |
case "admin/help#teaserbytype":
|
| 38 |
$output = '<p>'. t("The teaser by content type module allows an admin to set a different teaser length for each content type. Note that as with the site-wide setting, this only affects new posts or new edits of posts.") .'</p>';
|
| 39 |
$output .= '<p>'. t("Teaser length is set on the individual content type settings pages. Choose 'Default' to use the original site-wide setting for teaser length.") .'</p>';
|
| 40 |
$output .= t('<p>You can</p>
|
| 41 |
<ul>
|
| 42 |
<li>set teaser length for each content type on the <a href="@content-types-page">content types pages</a>.</li>
|
| 43 |
<li>set site-wide teaser length (for content types that do not specify a length) on the <a href="@post-settings-page">post settings page</a>.</li>
|
| 44 |
<li>update teasers of all existing posts with the separate <a href="@retease-module-url">retease module</a>.</li>
|
| 45 |
</ul>',
|
| 46 |
array(
|
| 47 |
'@content-types-page' => url('admin/content/types'),
|
| 48 |
'@post-settings-page' => url('admin/content/node-settings'),
|
| 49 |
'@retease-module-url' => 'http://drupal.org/project/retease',
|
| 50 |
|
| 51 |
)) .'';
|
| 52 |
return $output;
|
| 53 |
// case
|
| 54 |
} // switch
|
| 55 |
} // function teaserbytype_help
|
| 56 |
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Implementations of hook_form_alter() and hook_form_$form-id_alter()
|
| 60 |
* Alterations to various forms.
|
| 61 |
*/
|
| 62 |
|
| 63 |
/**
|
| 64 |
* - 'node_type_form', the form for content type settings
|
| 65 |
* Add a select box for the teaser length, drawing the options from node module,
|
| 66 |
* and add our own submit function to the form: teaserbytype_node_type_form_submit()
|
| 67 |
*/
|
| 68 |
function teaserbytype_form_node_type_form_alter(&$form, &$form_state) {
|
| 69 |
|
| 70 |
// Retrieve the post settings form from node module. This has core's list of teaser lengths
|
| 71 |
// and is also subjected to hook_form_alter as other modules may add other lenths.
|
| 72 |
include_once(drupal_get_path('module', 'node') . '/node.admin.inc');
|
| 73 |
|
| 74 |
$node_configure_form_state = array();
|
| 75 |
$node_configure_form = drupal_retrieve_form('node_configure', $node_configure_form_state);
|
| 76 |
drupal_prepare_form('node_configure', $node_configure_form, $fake_form_state);
|
| 77 |
|
| 78 |
$teaser_length_form = $node_configure_form['teaser_length'];
|
| 79 |
|
| 80 |
// Overwrite some values.
|
| 81 |
$teaser_length_form['#default_value'] = variable_get('teaser_length_'. $form['#node_type']->type, 'default');
|
| 82 |
$teaser_length_form['#description'] .= '<br />'. t("Set to 'Default' to use the value from the <a href=\"@post-settings-page\">post settings page</a>.", array('@post-settings-page' => url('admin/content/node-settings')));
|
| 83 |
|
| 84 |
// Add a 'default' option.
|
| 85 |
$teaser_length_form['#options'] = array_merge(array('default' => t('Default')), $teaser_length_form['#options']);
|
| 86 |
|
| 87 |
$form['workflow']['teaser_length'] = $teaser_length_form;
|
| 88 |
|
| 89 |
// add a submit handler
|
| 90 |
$form['#submit'][] = 'teaserbytype_node_type_form_submit';
|
| 91 |
} // function teaserbytype_form_node_type_form_alter
|
| 92 |
|
| 93 |
/**
|
| 94 |
* - 'node_configure', the regular place to set teaser length.
|
| 95 |
* Just a little reminder of where to go to set per content type.
|
| 96 |
*/
|
| 97 |
function teaserbytype_form_node_configure_alter(&$form, &$form_state) {
|
| 98 |
$form['teaser_length']['#description'] .= '<br />'. t('Override this for individual content types at the <a href="@content-types-page">content types page</a>.', array('@content-types-page' => url('admin/content/types')));
|
| 99 |
} // function teaserbytype_form_node_configure_alter
|
| 100 |
|
| 101 |
/**
|
| 102 |
* - '{type}_node_form', the edit form for a node (of type {type})
|
| 103 |
* If we're editing a node of a type that has a custom length,
|
| 104 |
* then add a custom validation function. This will alter the teaser on node preview.
|
| 105 |
*/
|
| 106 |
function teaserbytype_form_alter(&$form, &$form_state, $form_id) {
|
| 107 |
// node edit form
|
| 108 |
if (isset($form['type'])) {
|
| 109 |
$type = $form['type']['#value'];
|
| 110 |
if ($form_id == $type .'_node_form' && variable_get('teaser_length_'. $type, NULL) !== NULL) {
|
| 111 |
$form['#validate'][] = 'teaserbytype_node_edit_form_submit';
|
| 112 |
}
|
| 113 |
}
|
| 114 |
} // function teaserbytype_form_alter
|
| 115 |
|
| 116 |
|
| 117 |
/**
|
| 118 |
* custom submit function for form node_type_form.
|
| 119 |
* if the teaser length is set to 'default':
|
| 120 |
* - remove this value from the form (so it doesn't get set by node's content_types.inc)
|
| 121 |
* - and delete the persistent variable too
|
| 122 |
* this effectively undoes any customization we've done for the particular content type
|
| 123 |
*/
|
| 124 |
function teaserbytype_node_type_form_submit($form, &$form_state) {
|
| 125 |
if ($form_state['values']['teaser_length'] == 'default') {
|
| 126 |
unset($form_state['values']['teaser_length']);
|
| 127 |
|
| 128 |
$varname = 'teaser_length_'. $form_state['values']['type'];
|
| 129 |
global $conf;
|
| 130 |
if (isset($conf[$varname])) {
|
| 131 |
variable_del($varname);
|
| 132 |
}
|
| 133 |
}
|
| 134 |
// else case is handled in content_types.inc: the variable gets saved there
|
| 135 |
} // function teaserbytype_node_type_form_submit
|
| 136 |
|
| 137 |
|
| 138 |
/**
|
| 139 |
* custom submit function for node edit forms.
|
| 140 |
* creates the correct length teaser on previews of nodes.
|
| 141 |
*/
|
| 142 |
function teaserbytype_node_edit_form_submit($form, &$form_state) {
|
| 143 |
if ($form_state['clicked_button']['#value'] == 'Preview') {
|
| 144 |
$size = variable_get('teaser_length_'. $form_state['values']['type'], NULL);
|
| 145 |
if (isset($size)) {
|
| 146 |
$form_state['values']['teaser'] = isset($form_state['values']['body']) ? node_teaser($form_state['values']['body'], isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL, $size) : '';
|
| 147 |
}
|
| 148 |
}
|
| 149 |
} //teaserbytype_node_edit_form_submit
|
| 150 |
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Implementation of hook_nodeapi().
|
| 154 |
* If this node's type has an override teaser length variable, set the teaser here
|
| 155 |
*/
|
| 156 |
function teaserbytype_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 157 |
switch ($op) {
|
| 158 |
case 'presave':
|
| 159 |
// works for save -- not for preview
|
| 160 |
$size = variable_get('teaser_length_'. $node->type, NULL);
|
| 161 |
if (isset($size)) {
|
| 162 |
$node->teaser = isset($node->body) ? node_teaser($node->body, isset($node->format) ? $node->format : NULL, $size) : '';
|
| 163 |
}
|
| 164 |
break;
|
| 165 |
// case 'presave'
|
| 166 |
} // switch
|
| 167 |
} // function teaserbytype_nodeapi
|