PHP4: auto_nodetitle just returns "ant" for title when creating or updating CCK nodes

rizzo - February 22, 2009 - 01:15
Project:Automatic Nodetitles
Version:6.x-1.x-dev
Component:User interface
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Issue tags:auto_nodetitle
Description

Using drupal 6.9 with CCK 6.x-2.1, Token 6.x-1.11 and Date 6.x-2.0-rc6.

Selecting "Automatically generate the title and hide the title field". Then I tried a variety of patterns for the title, using the tokens I wanted. Then I tried a PHP code snippet. Then I just tried "helloworld". Every time it just used "ant" as the title. I don't see any documentation other than a PHP example in the README.txt.

The tokens I'm using are from custom CCK fields. However, since it used "ant" with a static string, I don't think that's the problem.

#1

rizzo - February 22, 2009 - 01:18

fwiw, here is some relevant info. PHP is old, I know, but I didn't see any warning from auto_nodetitle about it like other modules do.

MySQL 4.1.20
PHP 4.3.9

#2

rizzo - February 22, 2009 - 01:21

Here's the different titles I tried to use, first regular tokens:

[field_gametime-view] - [field_opponent-title-raw]

Then a PHP snippet:

<?php
  $token
= '[field_gametime-view]';
  if ([
field_homegame_flag-raw] == 'Home') {
   
$token .= ' vs. ';
  } else {
   
$token .= ' at ';
  }
 
$token .= '[field_opponent-title-raw]';

  return
$token;
?>

The tokens were provided in the list of "Replacement patterns" in the Automatic title generation box of the content type edit form.

#3

rizzo - February 22, 2009 - 01:48

One other thing to note, I have disabled FCKeditor for the pattern field, so it's getting entered in plain text.

I'm not sure if there's some documentation for this module, all I have is the README.txt. I see references in the release notes about docs being updated.

#4

rizzo - February 22, 2009 - 02:45

There is nothing written to the error log when either creating or updating a node. I've disabled and uninstalled and reinstalled the module as well as changed the weight in the system table from 5 to 10 based on nadavoid's suggestion in IRC. Nothing changes in the end.

fwiw, I also have the pathauto module installed. That module works perfectly fine.

#5

rizzo - February 22, 2009 - 02:50

Here's something new. When I go under the Content administration screen and batch "Update automatic nodetitles", it works. This may have been what nadavoid asked me to do earlier, I thought he meant to re-edit an existing node and save it.

So perhaps the patch he suggested is one that will work for me. Was that in Issue 336477? Was that fix not in 6.x-1.1?

#6

asak - March 19, 2009 - 09:11

subscribing.

#7

TomSherlock - March 20, 2009 - 19:47

I would like to add what i have noticed with regards to auto_nodetitle when updating a content item or CCK node.

First i have noticed that with version 6.x-1.1 when updating the properties of a content item, the title (node->title) is changed to 'ant'. I found this frustrating, so i tried to discover the point at which this occurs. I have seen 'ant' appear about three times in the auto_nodetitle module. However it appears to me that 'ant' is first introduced to the node in the function auto_nodetitle_form_alter.

It seems that this function is executed under three circumstances for an existing content item:

  1. When setting the content item to be edited.
  2. When previewing an edited content item.
  3. When saving an edited content item.

I don't understanding why this function is executed for all three circumstances. When i look at api.drupal.org, the documentation for hook_form_alter explains that the function Perform[s] alterations before a form is rendered. It would seem to me that an alternation is actually only being performed when the edited content item is being saved. Perhaps you could make an argument that the content item has been temporarily changed under the preview status; but i don't see any alteration being made when simply presenting the content item for editing.

Or does changing the status of the content item from viewable (read-only) to editable (read & write) constitute an alteration of a form?

As you may have guessed I am a noob to Drupal and thus may not have a grasp of the entire Drupal picture here. Please edify me if this is the case.

In any case, since the this function (auto_nodetitle_form_alter) appears to be the first in which 'ant' is introduced into the node as a placeholder value perhaps it would also be a good idea to first check for the existing value of the node's title (node->title)? I have noticed during update of an existing content item with a title the following fields are populated:

  1. $form['#node']->title
  2. $form['title']['#default_value']

At this point node has a title and form's title has a default value. Again it is not clear to me why both fields are necessary. Would the first field be blank (empty, null) and the second field populated when the content is first being created? Sorry i didn't yet check obvious scenario.

I came up with my own hack to check for the title when editing an existing content item. Perhaps i placed the check in the wrong function or am checking the wrong fields. But i think i got the basic idea right. Hopefully this code will be of some use:

/**
* Implementation of hook_form_alter().
*/
function auto_nodetitle_form_alter(&$form, $form_state, $form_id) {

  if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
    auto_nodetitle_node_settings_form($form);
  }
  else if (isset($form['#node']) && isset($form['#method']) && $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


       
        //HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK
        //  Thomas J. Sherlock tjs2009MAR20  (Happy Northern Spring!)
        // Check for the current title
    if(isset($form['#node']->title)){
    $form['title']['#value'] = $form['#node']->title;
    }elseif(isset($form['title']['#default_value'])){
    $form['title']['#value'] = $form['title']['#default_value'];
    }else{
      $form['title']['#value'] = 'ant';
    }
        //HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK-HACK


     
      $form['title']['#type'] = 'value';
      $form['title']['#required'] = FALSE;
      $form['#submit'][] = 'auto_nodetitle_node_form_submit';
   
    }
    else if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_OPTIONAL) {
      // we will make the title optional
      $form['title']['#required'] = FALSE;
      $form['#submit'][] = 'auto_nodetitle_node_form_submit';     
    }
  }
}

#8

rizzo - April 21, 2009 - 03:02

Thanks for the reply Tom. Unfortunately it doesn't seem to help my problem. My CCK nodes are getting set to "ant" right at creation. Right now the only way to get the correctly auto nodetitle is through the batch update. I desperately need a fix for this.

#9

rizzo - April 21, 2009 - 03:33

One interesting note, when I "Preview" a new CCK node, the auto-generated title is perfect. But when I save, it goes to "ant". I'm looking at code now.

#10

rizzo - April 21, 2009 - 03:45

I may have found it. It seems that the piece of code I wanted to execute was only getting executed when Preview mode. I don't know why you would call auto_nodetitle_set_title() only for Preview and not for Save. Unless perhaps the author believes that the Save case is handled elsewhere (but for me it wasn't).

Loosening up the outermost "if" control in auto_nodetitle_node_form_submit() gets the title generated on new node creation as well as node edits.

<?php
/**
* Makes sure the node preview is shown right.
* It gets the node object, modifies the title, and updates the node in the form_state.
*/
function auto_nodetitle_node_form_submit($form, &$form_state) {
 
// Only do something, if the user clicked the preview button.

  // XXX
  //if (isset($form_state['clicked_button']['#submit']) && in_array('node_form_build_preview', $form_state['clicked_button']['#submit'])) {

 
if (isset($form_state['clicked_button']['#submit'])) {
   
$setting = auto_nodetitle_get_setting($form_state['values']['type']);
    if (
$setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) {
     
$node = node_submit($form_state['values']);
       
watchdog('auto_nodetitle_node_form_submit','Calling auto_nodetitle_set_title()');
     
auto_nodetitle_set_title($node);
     
$form_state['values'] = (array)$node;
    }
  }
}
?>

I hope I'm not creating other problems. Perhaps fago or someone else more familiar can verify?

#11

fago - April 21, 2009 - 08:54
Title:auto_nodetitle just returns "ant" for title when creating or updating CCK nodes» PHP4: auto_nodetitle just returns "ant" for title when creating or updating CCK nodes
Version:6.x-1.1» 6.x-1.x-dev
Category:support request» bug report
Status:active» fixed

The reason the code above is restricted to preview is, that ANT is automatically activated when the node is saved, due to:

<?php
/**
* Implementation of hook_nodeapi().
*/
function auto_nodetitle_nodeapi(&$node, $op) {
  if (
$op == 'presave' && auto_nodetitle_is_needed($node)) {
   
auto_nodetitle_set_title($node);
  }
}
?>

Unfortunately until now the & before $node was missing - which probably created problems for php4 users. I suppose you were using PHP4? If so, try the next generated dev-snapshot.

#12

rizzo - April 21, 2009 - 13:12

Ah yeah, I'm on php 4. I didn't think to mention that initially.

$ php --version
PHP 4.3.9 (cgi) (built: Jul 16 2008 16:44:47)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

We're looking at upgrading to php 5 and mysql 5 sometime this year. So if I just add the "&" in front of $node in the function definition, that's the only change needed?

#13

rizzo - April 22, 2009 - 01:49

Confirming that your fix works for me. I just made the one change, I didn't download a new snapshot.

Thanks!

#14

TomSherlock - April 25, 2009 - 04:47

Hi, Rizzo. Sorry my hack was unable to help you with your problem. But i'm glad fago's suggestion worked.

I'll try the same to see if it addresses my issue.

#15

System Message - May 9, 2009 - 04:50
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.