Generate title only on insertion, not on update

flobruit - August 28, 2007 - 18:39
Project:Automatic Nodetitles
Version:5.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:needs review
Description

I have been in a few situations where I needed node titles generated automatically, but only when the node is created, not when it is edited. I tried to use an if-else structure in the evaluated php code, but I haven't managed yet to pull the node title out of $form_values because it is already temporarily set to 'ant'. From looking at the code it seems that it shouldn't be too hard to add an option to bypass the automatic title generation on node edit for certain content types (that would be another checkbox on the content type settings page), and it would definitely be cleaner doing this in the back-end than in the admin front-end.

I should be able to write this functionality and submit a patch, but before I start coding I was wondering if I could get your opinion/concerns/suggestions about how to go about this.

#1

olio - August 29, 2007 - 08:14

Yeah! Please, please, please try this! I ran exactly into this kind of problem some time ago and I wasn't able find a solution to it. Unfortunately, I posted this issue in the wrong thread (and didn't get a reaction) because I thought it has something to do with another module (AHAH Framework). Here is the link to this issue: http://drupal.org/node/154157 (please tell me if you need more information)
Up to now I was forced to hardcode some node titles inside node template files to hide the famous 'ant' titles - not so elegant...

The Automatic Nodetitle module is great and I can't imagine how to live without it anymore ;o).
So your efforts in finding a solution to this problem would be really, really appreciated.

Thanks!

#2

fago - September 8, 2007 - 13:24

yep, a patch for this would be great. But please write it that way, so that people could also use it the other way round: only automatic set the title on node update, e.g. by using two checkboxes. And if necessary, don't forget an upgrade path so that no settings are lost!

#3

AdAstra - September 26, 2007 - 21:46

subscribing

#4

markfoodyburton - October 1, 2007 - 19:29

I'd like this too - actually, I dont see why this isn't the default...?
Why would you want the title changed on an update - except that it's the only way to get the token stuff working, which is a little awkward anyway?

#5

gmak - October 19, 2007 - 20:23

I'd like to add my voice to the call for auto node title on insertion (and update)

#6

grah - November 17, 2007 - 06:43

subscribing.

#7

fago - December 6, 2007 - 13:33

the optional title generation feature already allows a similar behaviour, for more look at:
http://drupal.org/node/198398

#8

marcoBauli - December 11, 2007 - 12:13

actually looking for the same thing. happy to test any early patch if available.

#9

clivesj - February 18, 2008 - 20:55

Because I needed this very badly I editted the module to suit my needs.
It is a very dirty sollution so it is not fit for a patch or anything.
If ANT detects the title is already set (like when updating a node created before) it will by-pass the title generator.
Of course you will loose token-generated updates of your title, but for me this works better.
Use at own descrition. The parts I changed are commented // *** Clivesj edit
When I have time i will propose a generic sollution.

<?php
function auto_nodetitle_form_alter($form_id, &$form) {
 
  if (isset(
$form['#node_type']) && 'node_type_form' == $form_id) {
   
auto_nodetitle_node_settings_form($form);
  }
  else if (isset(
$form['#node']) && isset($form['#post']) && $form['#node']->type .'_node_form' == $form_id) {
   
//this is a node form   
   
if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_ENABLED) {
     
// we will autogenerate the title later, just hide the title field in the meanwhile

// *** edit Clivesj:
     
if (empty($form[title]['#default_value'])) {
       
$form['title']['#value'] = 'ant';
        } else {
           
$form['title']['#value'] = $form[title]['#default_value'];
            }
// ***  end edit Clivesj
     
$form['title']['#type'] = 'value';
     
$form['title']['#required'] = FALSE;
    }
    else if (
auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_OPTIONAL) {
     
// we will make the title optional
     
$form['title']['#required'] = FALSE;
    }
  }
}

/**
* Implementation of hook_nodeapi().
*/
function auto_nodetitle_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) {
  if (
$op == 'validate') {
    if ((
auto_nodetitle_get_setting($node->type) == AUTO_NODETITLE_ENABLED) || ((auto_nodetitle_get_setting($node->type) == AUTO_NODETITLE_OPTIONAL) && empty($node->title))) {
     
/*
       * Sets the node title.
       * We need to do this on validation because of two points:
       *     It's early engouh to have node previews working and
       *     it's late enough as CCK has already gone through the 'process form values' step
       *
       * As changes to $node are not persistent during validation, we use form_set_value()!
       */
// *** Clivesj edit:
     
if ($node->title == 'ant') {      
       
auto_nodetitle_set_title($node);
      }
// *** end edit Clivesj
     
form_set_value($form['title'], $node->title);
    }
  }
}
?>

#10

cs8c - February 20, 2008 - 06:27

clivesj, Thank you for this dirty fix, it works great so far!

#11

dagmar - December 9, 2008 - 14:34
Status:active» needs review

I have created a patch based on clivesj code that adds another option to auto node titles settings.

It allows to select if preserve or not the original title of the node.

AttachmentSize
auto_nodetitle_preserve_title_d5.patch 4.15 KB

#12

fago - December 30, 2008 - 20:34
Category:feature request» task
Status:needs review» needs work

Hm, I had not planned to add new features to the 5.x branch, but if you roll a 6.x patch too I'm ok with that.

@patch:
* please add your option just as another radio field - as it's just another possible way the module can work. Allowing the combination of the options makes it to difficult to grasp.
* be sure to prefix your variables with ant_ like the other variables.
* I've done some changes in CVS, please reroll your patch and add your new variable to the uninstallation procedure and hook_node_type()

#13

revimage - February 16, 2009 - 06:16

Woah hang on people... can we ask that this feature be OPTIONAL?

For myself actually... i would prefer if it does constantly check for a change on updates... i am using the token [menu-link-title] to have auto nodetitle automatically title the page to whatever is set for the menu name, but MORE importantly... my content type utilizes another module to automatically insert that SAME value into Taxonomy for other uses... so if the title of the page doesnt change, neither will my taxonomy term which could break a big part of how my drupal config is working.

Sincerely hope that makes sense.

Best,
Mike

#14

clivesj - March 16, 2009 - 19:56
Status:needs work» needs review

This patch

  • will add an extra option "preserve title" (radio-option - @revimage: all options remains unchanged)
  • Is patched against 2009-02-10 dev version

I have prepared a D6 version, but will open a seperate issue to provide the patch as soon this one is found OK.
I suggest not to "hide" but "disable" the node-title. This provides the possibility to add some info to the user so they know what behavior to expect.
I have changed an if-loop to a switch-loop for clarity.

AttachmentSize
ant_preserve_option.patch 3.12 KB
 
 

Drupal is a registered trademark of Dries Buytaert.