type .'_node_form' == $form_id) { //this is a node form if (variable_get('ant_'. $form['#node']->type, 0)) { // we will autogenerate the title later, just hide the title field in the meanwhile $form['title']['#value'] = 'ant'; $form['title']['#type'] = 'value'; $form['title']['#required'] = FALSE; $form['#validate']['auto_nodetitle_validate'] = array(); } } } /** * Implementation of hook_nodeapi(). */ function auto_nodetitle_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { if ($op == 'submit' && variable_get('ant_'. $node->type, 0)) { /* * Sets again the node title, as the node should be completed now. */ auto_nodetitle_set_title($node); } } /* * Sets the node title during validation, so that the node preview can display it */ function auto_nodetitle_validate($form_id, $form_values, $form) { $node = (object)$form_values; auto_nodetitle_set_title($node); form_set_value($form['title'], $node->title); } /* * Sets the automatically generated nodetitle for the node */ function auto_nodetitle_set_title(&$node) { $types = node_get_types(); $pattern = variable_get('ant_pattern_'. $node->type, ''); if (trim($pattern)) { $node->changed = time(); $node->title = _auto_nodetitle_patternprocessor($pattern, $node); } else if ($node->nid) { $node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid)); } else { $node->title = t('@type', array('@type' => $types[$node->type]->name)); } // warn, if the generated title is empty if (!trim($node->title) && !variable_get('ant_emptyok_'. $node->type, 0)) { $message = t('Autogenerated title field is blank.'); if (user_access('administer nodes')) { $message .= ' '. t('Perhaps you need to change the configuration settings for this content type.', array('@url' => 'admin/content/types/'. $node->type)); } } } /** * Helper 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. * @return a title string */ function _auto_nodetitle_patternprocessor($output, $node) { if (module_exists('token')) { $output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags(token_replace($output, 'node', $node))); } if (variable_get('ant_php_'. $node->type, 0)) { $output = drupal_eval($output); } return $output; } /** * Helper function for hook_form_alter() renders the settings per node-type. * @TODO: a re-evaluate PHP pattern on edit? option. */ function auto_nodetitle_node_settings_form(&$form) { $form['auto_nodetitle'] = array( '#type' => 'fieldset', '#title' => t('Automatic title generation'), '#weight' => 0, '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['auto_nodetitle']['ant'] = array( '#type' => 'checkbox', '#title' => t('Automatically generate the node title and hide the node title field.'), '#default_value' => variable_get('ant_'. $form['#node_type']->type, 0), ); if (module_exists('token') || user_access('use PHP for title patterns')) { $description = t('Leave blank for using the per default generated title. Otherwise this string will be used as title.'); if (module_exists('token')) { $description .= ' '. t('Use the syntax [token] if you want to insert a replacement pattern.'); } $form['auto_nodetitle']['ant_pattern'] = array( '#type' => 'textarea', '#title' => t('Pattern for the title'), '#description' => $description, '#default_value' => variable_get('ant_pattern_'. $form['#node_type']->type, ''), ); } if (module_exists('token')) { $form['auto_nodetitle']['token_help'] = array( '#title' => t('Replacement patterns'), '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['auto_nodetitle']['token_help']['help'] = array( '#value' => theme('token_help', 'node'), ); } if (user_access('use PHP for title patterns')) { $form['auto_nodetitle']['ant_php'] = array( '#type' => 'checkbox', '#title' => t('Evaluate PHP in pattern.'), '#description' => t('Put PHP code above that prints your string, but make sure you surround code in <?php and ?>'), '#default_value' => variable_get('ant_php_'. $form['#node_type']->type, ''), ); } else { // if user doesn't have PHP permission, their submission will reset PHP evaluation $form['auto_nodetitle']['auto_nodetitle_php'] = array( '#type' => 'value', '#value' => 0, ); } }