Download & Extend

Generate title only on insertion, not on update

Project:Automatic Nodetitles
Version:7.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

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.

Comments

#1

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

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

subscribing

#4

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

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

#6

subscribing.

#7

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

#8

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

#9

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

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

#11

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

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

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

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

#15

Version:5.x-1.x-dev» 6.x-1.2

Patch failed against version 6.x-1.2.
Perhaps it can get rolled into the latest DEV version?
Perhaps a fourth option should be offered for "hide"?
Switch loop did improve clarity.

% patch -b < ant_preserve_option.patch
patching file auto_nodetitle.module
Hunk #2 FAILED at 28.
Hunk #3 succeeded at 88 with fuzz 1 (offset 10 lines).
Hunk #4 succeeded at 167 (offset -2 lines).
1 out of 4 hunks FAILED -- saving rejects to file auto_nodetitle.module.rej

#16

Hello,
The patch was for D5.
Since I don't need this feature anymore, I have abandoned efforts for this patch.
Greetz

#17

Version:6.x-1.2» 6.x-1.x-dev
Status:needs review» active

Ok, lets move this to "active" again. I will to try to make a patch for 6.x this week.

#18

I need this feature badly too.

If I get this right, the way we are trying to work around this is to have an extra option in the node edit form that will bypass the title update if a title is already set(?), something like say... a 'Don't update title' checkbox? If so, can we please also have a default setting for this? I mean an option in the content type creation page.

@Dagmar: did you make any progress on the patch? I don't mean to bother you, I simply need a status update and if you have a draft patch perhaps I can help test it.

Thanks in advance.

#19

subscribing - need that one too.

#20

+1

#21

Any news? This is a major issue for many of us yet seems to be getting no attention

#22

Subscribing.

#23

subscribe

#24

OK folks, I´m not using this option myself, but I changed the module (D6) to accomodate this change.
With ANT enabled you will have an extra option:

  • Enable ANT but preserve title once set.
  • So after node creation ANT will not modify the title anymore.

    Also when creating an ANT-enabled node, the title-field will not be hidden. It will show a description of how the title generation will occur.

    Please test this version on your test-site and please provide feedback. If it´s OK I will make a patch.

    Greetz

    AttachmentSize
    auto_nodetitle.zip 2.74 KB

    #25

    I have similar problem. When creating a new node, the title is being generated well, but when I try to edit this node, title becomes empty. The title is supposed to be in following form: '[parent-title] by [field_developer-formatted]' where [parent-title] is token returned by Node Relativity module.

    My solution is to use code in "Automatic title generation" settings:

    <?php
    if (is_null($node->nid)){                           //node is new
    print '[parent-title] by [field_developer-formatted]';
    }else{                                                
    //node is not new

    $node = node_load($node->nid);
    $dev = t($node->field_developer[0]['value']);
    $pnid = $node->parent_node;
    $pnode = node_load($pnid);

    print
    $pnode->title .' by ' .$dev ;
    }
    ?>

    #26

    Andrey, I think yours is a different thing.
    This issue is about a feature to preserve a title once it is generated. The need for preserving the title is NOT -as in your case- the generated title disappears, but because it it re-generated when the node is edited.
    The re-generation is normal system operation by design.
    What we are trying to do now is to create an option to choose whether you want to title to be regenerated or not.

    #27

    Yep, I got. It's different a bit, yes, but anyway return result we need.

    #28

    I've tried the code from comment #24 but it simply doesn't work (I've ported the changes to the current -dev release). The title is ever updated.

    #29

    Forget # 24, it has a bug in it.
    I have corrected it and tested it on my test environment.
    The patch will add a 3th option to ANT: Generate a title, but don't update it once it is generated.
    It will also give a description on the node entry form, so you know what is happening with the title.

    I have included some screenshots so you can see how it works:

    • fig.1 shows the node type creation. Here you can choose the new option (preserve title). The title is composed of the authorsname and the node creation date. (token module)
    • fig.2 shows the node entry form. The title field tells you that the title will be generated once.
    • fig.3 shows the view of the created node. The authors name (admin) is in the title.
    • fig.4 Now I edit the node and change the author to user "Martijn".
    • fig.5 The author is changed, but the node still shows the original title.

    Notes:

    1. It doesn't look like the module owner is planning expand the ANT with features like this. The module-status is set to "maintenance only". I will make a patch for D7 as well. Maybe this will be an incentive to allow feature requests.
    2. I have tested the patch to see wether the 2 other options (ENABLE, OPTIONAL) remains intact. It works OK, BUT, it seems that date- related tokens are not updated (when options is set to ENABLE or OPTIONAL). This happens both with the patched and non-patched version. I have to checkup on this and will open an issue if neccesary.
    AttachmentSize
    ant_prsrv_title.patch 4.46 KB
    1.ant_presrv_nodetype.png 3.97 KB
    2.ant_presrv_create_a_node.png 4.45 KB
    3.ant_presrv_viewnode.png 4.21 KB
    4.ant_presrv_edit_existing.png 5.7 KB
    5.ant_presrv_title_preserved.png 3.64 KB

    #30

    A minor typo that I saw in the screenshots and verified that exits in the patch's code:

    ...Title for this node will be set automatically bij ANT module...

    should be:

    ...Title for this node will be set automatically by ANT module...

    I have a suggestion. It would be really useful if there was a per-node option to allow changing of the title in cases where required, perhaps only by certain roles. What I am talking about is:

    1. Change the setting's (radio button) text from:

    Always automatically generate the title but preserve the first generated title

    to:

    Always automatically generate the title and lock it so it cannot be changed on edit

    2. Add a 'Unlock automatically generated node titles' permission.

    3. Add a 'Unlock title' checkbox under node's locked titles that will *only* show for roles/users with the permission.

    This way, certain roles would be able to temporary unlock the title, edit it and once the node is saved the new title would still have a locked state.

    #31

    I 'll correct the typo, thanks.
    I think it is a good suggest to be able to unlock a locked title. But in order not to complex the code to much it may be easier to add a disengage button for ANT alltogether. Indeed ruled by permission. So those with permission can always overide a title regardless of the node-option.
    But we we will have to discuss this with fago since he has no intention to allow new features to ANT

    #32

    Fair enough, but even if there's no intent to add new features, still a solution here as a patch would help a lot of people get things done. Thanx for considering my suggestion and for your time on this Clivesj.

    #33

    Ok, now the patch looks correct.

    Does the intent to not add new features include the generation of the title only when the node is created (this topic) ?

    #34

    @FiNeX: As Clivesj already said in #29 (his note #1) the module's development status is set to 'Maintenance fixes only'. You can see this for yourself in the project's page. I guess this means that there will be no new features whatsoever ...unless of course fago doesn't intent to be that strict about it ;)

    #35

    @Klonos: sorry, I didn't read well the notes :-) Thanks :-)

    #36

    No problem mate ;)

    #37

    @clivesj
    just want to say thanks for your great patch!!

    #38

    what would be solution for D7?

    #39

    Version:6.x-1.x-dev» 7.x-1.x-dev

    new features should go in 7.x first.

    >Maintenance fixes only
    That means I'm not going to actively work on new features. But if the community comes up with new stuff that is wanted and fits in the module, like this one - I'm fine with that!

    #40

    I have a patch for D7, but I'm trying to figure-out how to file a patch utilizing the new GIT system

    #41

    Proper patches with GIT

    This article should be everywhere

    http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/

    Christophler

    #42

    Thanks Christopher,
    That was really usefull.
    I have created a patch for D7 http://drupal.org/node/1225384 and will come-up with a backport for D6 shortly.

    #43

    There is a problem in 6.x patch.
    When using "image import", the module generates EMPTY TITLE. Title is correctly generated only if the node is created thru node/add/image.
    Is there a quick way to fix this? When patch was not applied title was generated correctly.
    Please help.

    #44

    Hi Lex, are you using patch #29? If so, what is shown in the title field when editing the node?

    #45

    Patch from #42 had some problems: http://drupal.org/node/1225384#comment-4927390

    The link includes what I did to fix it.