auto_nodetitle.module

Tracking 6.x-1.x branch
  1. drupal
    1. 5 contributions/auto_nodetitle/auto_nodetitle.module
    2. 6 contributions/auto_nodetitle/auto_nodetitle.module
    3. 7 contributions/auto_nodetitle/auto_nodetitle.module

Allows hiding of the node title field and automatic title creation

Constants

NameDescription
AUTO_NODETITLE_DISABLED
AUTO_NODETITLE_ENABLED
AUTO_NODETITLE_OPTIONAL

Functions & methods

NameDescription
auto_nodetitle_evalEvaluates php code and passes $node to it
auto_nodetitle_form_alterImplementation of hook_form_alter().
auto_nodetitle_get_settingGets the auto node title setting associated with the given content type.
auto_nodetitle_is_neededReturns whether the auto nodetitle has to be set.
auto_nodetitle_nodeapiImplementation of hook_nodeapi().
auto_nodetitle_node_form_submitMakes sure the node preview is shown right. It gets the node object, modifies the title, and updates the node in the form_state.
auto_nodetitle_node_operationsImplementation of hook_node_operations().
auto_nodetitle_node_settings_formHelper function for hook_form_alter() renders the settings per node-type.
auto_nodetitle_node_typeImplementation of hook_node_type().
auto_nodetitle_operations_updateCallback function for updating node titles.
auto_nodetitle_permImplementation of hook_perm()
auto_nodetitle_set_titleSets the automatically generated nodetitle for the node
_auto_nodetitle_patternprocessorHelper function to generate the title according to the PHP code. Right now its only a wrapper, but if this is to be expanded, here is the place to be.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows hiding of the node title field and automatic title creation
  5. */
  6. define('AUTO_NODETITLE_DISABLED', 0);
  7. define('AUTO_NODETITLE_ENABLED', 1);
  8. define('AUTO_NODETITLE_OPTIONAL', 2);
  9. /**
  10. * Implementation of hook_perm()
  11. */
  12. function auto_nodetitle_perm() {
  13. return array('use PHP for title patterns');
  14. }
  15. /**
  16. * Implementation of hook_form_alter().
  17. */
  18. function auto_nodetitle_form_alter(&$form, $form_state, $form_id) {
  19. if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
  20. auto_nodetitle_node_settings_form($form);
  21. }
  22. else if (isset($form['#node']) && isset($form['#method']) && $form['#node']->type .'_node_form' == $form_id) {
  23. //this is a node form
  24. if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_ENABLED) {
  25. // we will autogenerate the title later, just hide the title field in the meanwhile
  26. $form['title']['#value'] = 'ant';
  27. $form['title']['#type'] = 'value';
  28. $form['title']['#required'] = FALSE;
  29. $form['#submit'][] = 'auto_nodetitle_node_form_submit';
  30. }
  31. else if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_OPTIONAL) {
  32. // we will make the title optional
  33. $form['title']['#required'] = FALSE;
  34. $form['#submit'][] = 'auto_nodetitle_node_form_submit';
  35. }
  36. }
  37. }
  38. /**
  39. * Makes sure the node preview is shown right.
  40. * It gets the node object, modifies the title, and updates the node in the form_state.
  41. */
  42. function auto_nodetitle_node_form_submit($form, &$form_state) {
  43. // Only do something, if the user clicked the preview button.
  44. if (isset($form_state['clicked_button']['#submit']) && in_array('node_form_build_preview', $form_state['clicked_button']['#submit'])) {
  45. $setting = auto_nodetitle_get_setting($form_state['values']['type']);
  46. if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) {
  47. $node = node_submit($form_state['values']);
  48. auto_nodetitle_set_title($node);
  49. $form_state['values'] = (array)$node;
  50. }
  51. }
  52. }
  53. /**
  54. * Implementation of hook_nodeapi().
  55. */
  56. function auto_nodetitle_nodeapi(&$node, $op) {
  57. if ($op == 'presave' && auto_nodetitle_is_needed($node)) {
  58. auto_nodetitle_set_title($node);
  59. }
  60. }
  61. /**
  62. * Returns whether the auto nodetitle has to be set.
  63. */
  64. function auto_nodetitle_is_needed($node) {
  65. return empty($node->auto_nodetitle_applied) && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title));
  66. }
  67. /**
  68. * Sets the automatically generated nodetitle for the node
  69. */
  70. function auto_nodetitle_set_title(&$node) {
  71. $types = node_get_types();
  72. $pattern = variable_get('ant_pattern_'. $node->type, '');
  73. if (trim($pattern)) {
  74. $node->changed = time();
  75. $node->title = _auto_nodetitle_patternprocessor($pattern, $node);
  76. }
  77. else if ($node->nid) {
  78. $node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid));
  79. }
  80. else {
  81. $node->title = t('@type', array('@type' => $types[$node->type]->name));
  82. }
  83. // Ensure the generated title isn't too long.
  84. $node->title = substr($node->title, 0, 255);
  85. // With that flag we ensure we don't apply the title two times to the same node.
  86. $node->auto_nodetitle_applied = TRUE;
  87. }
  88. /**
  89. * Implementation of hook_node_operations().
  90. */
  91. function auto_nodetitle_node_operations() {
  92. $operations = array(
  93. 'nodetitle_update' => array(
  94. 'label' => t('Update automatic nodetitles'),
  95. 'callback' => 'auto_nodetitle_operations_update',
  96. ),
  97. );
  98. return $operations;
  99. }
  100. /**
  101. * Callback function for updating node titles.
  102. */
  103. function auto_nodetitle_operations_update($nodes) {
  104. foreach ($nodes as $nid) {
  105. $node = node_load($nid);
  106. if ($node && auto_nodetitle_is_needed($node)) {
  107. $previous_title = $node->title;
  108. auto_nodetitle_set_title($node);
  109. // Only save if the title has actually changed.
  110. if ($node->title != $previous_title) {
  111. node_save($node);
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Helper function to generate the title according to the PHP code.
  118. * Right now its only a wrapper, but if this is to be expanded, here is the place to be.
  119. * @return a title string
  120. */
  121. function _auto_nodetitle_patternprocessor($output, $node) {
  122. if (module_exists('token')) {
  123. $output = token_replace($output, 'node', $node);
  124. }
  125. if (variable_get('ant_php_'. $node->type, 0)) {
  126. $output = auto_nodetitle_eval($output, $node);
  127. }
  128. if (variable_get('ant_php_'. $node->type, 0) || module_exists('token')) {
  129. $output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output));
  130. }
  131. return $output;
  132. }
  133. /**
  134. * Helper function for hook_form_alter() renders the settings per node-type.
  135. */
  136. function auto_nodetitle_node_settings_form(&$form) {
  137. $default_value = auto_nodetitle_get_setting($form['#node_type']->type);
  138. $form['auto_nodetitle'] = array(
  139. '#type' => 'fieldset',
  140. '#title' => t('Automatic title generation'),
  141. '#weight' => 0,
  142. '#collapsible' => TRUE,
  143. '#collapsed' => !$default_value,
  144. );
  145. $form['auto_nodetitle']['ant'] = array(
  146. '#type' => 'radios',
  147. '#default_value' => $default_value,
  148. '#options' => array(
  149. t('Disabled'),
  150. t('Automatically generate the title and hide the title field'),
  151. t('Automatically generate the title if the title field is left empty'),
  152. )
  153. );
  154. if (module_exists('token') || user_access('use PHP for title patterns')) {
  155. $description = t('Leave blank for using the per default generated title. Otherwise this string will be used as title.');
  156. if (module_exists('token')) {
  157. $description .= ' '. t('Use the syntax [token] if you want to insert a replacement pattern.');
  158. }
  159. $form['auto_nodetitle']['ant_pattern'] = array(
  160. '#type' => 'textarea',
  161. '#title' => t('Pattern for the title'),
  162. '#description' => $description,
  163. '#default_value' => variable_get('ant_pattern_'. $form['#node_type']->type, ''),
  164. );
  165. // Don't allow editing of the pattern if PHP is used, but the users lacks
  166. // permission for PHP.
  167. if (variable_get('ant_php_' . $form['#node_type']->type, '') && !user_access('use PHP for title patterns')) {
  168. $form['auto_nodetitle']['ant_pattern']['#value'] = $form['auto_nodetitle']['ant_pattern']['#default_value'];
  169. $form['auto_nodetitle']['ant_pattern']['#disabled'] = TRUE;
  170. $form['auto_nodetitle']['ant_pattern']['#description'] = t('You are not allow the configure the pattern for the title, as you lack the %permission permission.', array('%permission' => t('Use PHP for title patterns')));
  171. }
  172. }
  173. if (module_exists('token')) {
  174. $form['auto_nodetitle']['token_help'] = array(
  175. '#title' => t('Replacement patterns'),
  176. '#type' => 'fieldset',
  177. '#collapsible' => TRUE,
  178. '#collapsed' => TRUE,
  179. '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
  180. );
  181. $form['auto_nodetitle']['token_help']['help'] = array(
  182. '#value' => theme('token_help', 'node'),
  183. );
  184. }
  185. if (user_access('use PHP for title patterns')) {
  186. $form['auto_nodetitle']['ant_php'] = array(
  187. '#type' => 'checkbox',
  188. '#title' => t('Evaluate PHP in pattern.'),
  189. '#description' => t('Put PHP code above that returns your string, but make sure you surround code in &lt;?php and ?&gt;. Note that $node is available and can be used by your code.'),
  190. '#default_value' => variable_get('ant_php_'. $form['#node_type']->type, ''),
  191. );
  192. }
  193. else {
  194. $form['auto_nodetitle']['auto_nodetitle_php'] = array(
  195. '#type' => 'value',
  196. '#value' => variable_get('ant_php_'. $form['#node_type']->type, ''),
  197. );
  198. }
  199. }
  200. /**
  201. * Gets the auto node title setting associated with the given content type.
  202. */
  203. function auto_nodetitle_get_setting($type) {
  204. return variable_get('ant_'. $type, AUTO_NODETITLE_DISABLED);
  205. }
  206. /**
  207. * Evaluates php code and passes $node to it
  208. */
  209. function auto_nodetitle_eval($code, $node) {
  210. ob_start();
  211. print eval('?>'. $code);
  212. $output = ob_get_contents();
  213. ob_end_clean();
  214. return $output;
  215. }
  216. /**
  217. * Implementation of hook_node_type().
  218. */
  219. function auto_nodetitle_node_type($op, $info) {
  220. switch ($op) {
  221. case 'delete':
  222. variable_del('ant_'. $info->type);
  223. variable_del('ant_pattern_'. $info->type);
  224. variable_del('ant_php_'. $info->type);
  225. break;
  226. case 'update':
  227. if (!empty($info->old_type) && $info->old_type != $info->type) {
  228. variable_set('ant_'. $info->type, auto_nodetitle_get_setting($info->old_type));
  229. variable_set('ant_pattern_'. $info->type, variable_get('ant_pattern_'. $info->old_type, ''));
  230. variable_set('ant_php_'. $info->type, variable_get('ant_php_'. $info->old_type, ''));
  231. variable_del('ant_'. $info->old_type);
  232. variable_del('ant_pattern_'. $info->old_type);
  233. variable_del('ant_php_'. $info->old_type);
  234. }
  235. break;
  236. }
  237. }