Currently it's not possible to use entity id tokens such as [node:nid] for the title during the creation of new entities, as it the id won't be available until after it has been saved to the database.

A simple solution to this problem could be to just save the entity twice... not sure if that has any unwanted consequences. (I think the Commerce AutoSKU uses this approach).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bforchhammer’s picture

Related discussion from automatic nodetitles module: #194197: Use (T)NID as node title

NancyDru’s picture

Priority: Normal » Major
bforchhammer’s picture

Priority: Major » Normal

Sorry, but changing the priority won't make this happen any sooner; providing a patch would :-)

NancyDru’s picture

Status: Active » Needs review
FileSize
1.34 KB

Okay, try this one.

bforchhammer’s picture

Status: Needs review » Needs work

Thanks for the patch, that's a good start :-)

+++ b/auto_entitylabel.moduleundefined
@@ -113,6 +113,44 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
 /**
+ * Implements hook_node_insert().
+ */
+function auto_entitylabel_node_insert($node) {

We should use hook_entity_insert() to make this also work for entity types other than node.

+++ b/auto_entitylabel.moduleundefined
@@ -113,6 +113,44 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+        $old_title = $node->title;

$node->title will only work for nodes, but this should be supported for all entity types. I think it's should be something like

$settings = _auto_entitylabel_get_settings($entity, $type);
$old_label = $entity->{$settings['title']};
+++ b/auto_entitylabel.moduleundefined
@@ -113,6 +113,44 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+          drupal_goto("node/$nid");

Why is the drupal_goto() here?

NancyDru’s picture

The drupal_goto() is to prevent a 404 when the URL alias changes because the title changed.

I used $node->title because there is a node_save() right after it, as in the node_operations code.

bforchhammer’s picture

I used $node->title because there is a node_save() right after it, as in the node_operations code.

Well, as I said, we should get this to work for any entity type... The entity api module provides an entity_save() method, which we can use instead of node_save(). However, I just removed the dependency on the entity module in a different issue, so we either need to add it back, or maybe just copy&paste some functionality from there. Not sure what's best at the moment...

The drupal_goto() is to prevent a 404 when the URL alias changes because the title changed.

This sounds unrelated to this module; I think this should be handled/fixed by the pathauto module.

NancyDru’s picture

Actually, IMHO the 404 issue is caused by core. Node_save() specifically does a drupal_goto("node/$node->nid"). Somewhere in core (I think), it gets translated to 'node/alias' and this is the root of the problem.

matsbla’s picture

One solution can be to use the module "Serial Field"
http://drupal.org/project/serial

Then add this patch:
http://drupal.org/node/615410#comment-5530710

Then you can even make id numbers for each content type, e.g. video 1, video 2, photo 1, video 3

NancyDru’s picture

@matsbla: No, that doesn't address this issue at all. As it turns out, I am using Serial in the same content type (and even in the title).

westie’s picture

On creating new content Drupal sets a message displaying the title which still reflects the missing nid for example:

Content type title () has been created.

To rectify this add

 auto_entitylabel_set_title($node, 'node');

into the hook_node_insert(). Which sorts now adds the nid in the message like the example below:

Content type title (13) has been created.

msmithcti’s picture

Status: Needs work » Needs review
FileSize
2.32 KB

Thanks for starting this patch off @NancyDru, here's the patch again with the suggestion from the reviews above. The biggest changes I've made is that this now supports all entities, rather than just node. I've also reintroduced the dependency on the entity module for the function entity_save().

bforchhammer’s picture

Status: Needs review » Needs work

@splatio: Thanks for the updated patch!

+++ b/auto_entitylabel.module
@@ -113,6 +113,54 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+  ////@TODO: auto_entitylabel_is_needed does not seem to work here, so save everything.

auto_entitylabel_is_needed does not work here, because it returns FALSE if auto_entitylabel_set_title() has already run. We still need the check, so we don't accidentally change titles on entities without an automatic pattern...

I guess the best solution would be if we could detect that a pattern contains something like an ID and only run our code in auto_entitylabel_entity_presave if it doesn't.

I also just realized that auto_entitylabel_entity_presave duplicates code from auto_entitylabel_is_needed; I'm going to clean that up in a separate issue. (EDIT: see #1918924: Cleanup duplicate code in auto_entitylabel_entity_presave).

+++ b/auto_entitylabel.module
@@ -113,6 +113,54 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+  // Set the auto entity label here so the temporary label (without any ids) is
+  // displayed for feedback to the user.

Where would this be displayed? For the end-user there shouldn't be an intermediate step, right? If this is only for debugging purposes let's remove it.

+++ b/auto_entitylabel.module
@@ -113,6 +113,54 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+  $ids = entity_extract_ids($type, $entity);

More readable: list($id, , ) = entity_extract_ids($type, $entity);

+++ b/auto_entitylabel.module
@@ -113,6 +113,54 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+  $_SESSION['auto_entitylabel_entity_list'][$type][] = $ids[0];

AFAIK the use of global variables such as $_SESSION is discouraged in Drupal; we should be able to use drupal_static() instead.

+++ b/auto_entitylabel.module
@@ -113,6 +113,54 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+* Implements hook_exit().
+*
+* @see auto_entitylabel_entity_insert()
+*/

Missing space before *.

+++ b/auto_entitylabel.module
@@ -113,6 +113,54 @@ function auto_entitylabel_form_alter(&$form, &$form_state, $form_id) {
+          // Store the old label.
+          $old_label = $entity->$settings['title'];
...
+          // Save it only if the title has changed.
+          if ($entity->$settings['title'] != $old_label) {

I'm wondering whether this still works reliably when title replacement is used... did you test that by any chance?

msmithcti’s picture

Status: Needs work » Needs review
FileSize
2.68 KB

Thanks for the review!

I discovered after posting the patch that the auto_entitylabel_is_needed check is needed. I fixed that by adding an optional "reset" argument to auto_entitylabel_is_needed(), the downside with that approach is that the title will be reset every time an auto labelled entity is inserted [edit: I was thinking update, it's only inserted once so this should be fine].

The call to auto_entitylabel_set_title() is purely so in the "[bundle] [title] has been created." message shown after saving a node shows the title.

I've just done a quick test with the title module and that seems to work just fine.

Here's an updated patch.

bforchhammer’s picture

Thanks! Looks good from a quick look... I'll try to test this myself next week, and possibly commit afterwards.

presleyd’s picture

Tested tonight with a node that had a nid in it's title and it worked perfectly.

westie’s picture

Looks good to me.

bforchhammer’s picture

Status: Needs review » Fixed

I just tested this and it looks good! :-)

Committed and pushed #14, with one small adjustment: I've removed the call to auto_entitylabel_set_title() in auto_entitylabel_entity_insert(), because the title is generated without ids from auto_entitylabel_entity_presave already anyway, and we should avoid calling auto_entitylabel_set_title() repeatedly as far as possible.

Thanks everyone!

DerTobi75’s picture

Status: Fixed » Needs work

This works, but has one little bug.

After saving the node, the node cannot be found at first, because [node:id] is not added to the path, that opens the new created node!

bforchhammer’s picture

After saving the node, the node cannot be found at first, because [node:id] is not added to the path, that opens the new created node!

On a clean install without additional modules this should not be happening... I'm assuming you're using something like pathauto, right? Could you share you configuration (token patterns etc.) so we can reproduce this properly?

Also, I now realize that this problem was probably the reason for the drupal_goto() call in the original patch; I'm afraid that's still not a viable solution, because there is no unified destination to redirect to (nodes go to node/%, but comments do not go to comment/%!)

DerTobi75’s picture

Yes, I am using Path Auto!
For the title I just used: tf[node:nid]

I found another bug, when using Entity Reference Module and use the Autocomplete Field, the title is not created when saving the Node for the first time. When updating the node, the Title also get updated!

Regards,

Tobi

bforchhammer’s picture

Yes, I am using Path Auto!
For the title I just used: tf[node:nid]

What pattern are you using for pathauto?

I found another bug, when using Entity Reference Module and use the Autocomplete Field, the title is not created when saving the Node for the first time. When updating the node, the Title also get updated!

Please create a new issue and try to be a bit more specific about what you're doing and what's not working the way you expect.

DerTobi75’s picture

For the title created by Automatic Entity Label, I just used: tf[node:nid]
The Pattern for Path Auto is: media/[node:title]

And after saving the node Drupal was looking just for: media/tf
Without the NID. BUT the title is saved correctly, seems, somethings broken with the redirect?!

Regards,

Tobi

BWPanda’s picture

Status: Needs work » Fixed

Setting back to fixed. Thanks for this commit!

Status: Fixed » Closed (fixed)

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

CodeWarrior-1’s picture

Status: Closed (fixed) » Active

I've downloaded the last patch in the thread and attempted to apply it, the patch fails. Can some one advise me if this patch is current and if not how I might modify it for the latest version?

bforchhammer’s picture

Status: Active » Closed (fixed)

Patch is already committed. Use the development version :)

rclemings’s picture

Status: Closed (fixed) » Active
FileSize
48.54 KB

This might be a bug report but it seems likely to be related to this issue so I will post it here first.

The patch in dev doesn't appear to work when the entity is previewed rather than saved. Instead it displays "%AutoEntityLabel%" for the title.

Steps to reproduce on clean install of Drupal 7:

1. Install and enable auto_entitylabel, entity, token
2. Create a new content type with text field "field1"
3. Set "node auto label" to "Automatically generate the label and hide the label field" and in "Pattern for the title" put: [node:field_field1]
4. Create new content of that content type and preview. See attached png.

bforchhammer’s picture

Not sure whether this worked before this patch either; let's open a new issue, please :)

bforchhammer’s picture

Status: Active » Closed (fixed)
ikeigenwijs’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

I got the same problem of #23

But with the serial field.
The title gets saved correctly at one go, no rules.
Path auto is enabled and used following
[node:content-type:name]/[node:title]
the node title there is not yet complete, the serial field is missing, but when looking at content listing, the title is complete and the node is created ok.
When disabling path auto and node/xxx is being used, redirect is ok.
Looking all over the place for the source of this problem.

nithinkolekar’s picture

same as #23 with setup

pathauto
Default path pattern (applies to all content types with blank patterns below)
content/[node:title]

auto entity label
Automatically generate the label and hide the label field
SMS-[node:nid] [node:created:short]

When new node is saved its title is correctly set with SMS-1255 05/27/2015 - 18:31 but redirecting to 404 with path content/sms-05272015-1831 (notice nid token is not replaced in path) instead of content/sms-1255-05272015-1831 which is created and exist after node save.

GAMe’s picture

FileSize
17.08 KB

I too am suffering this issue the same as #23 but I hacked an older version of the module some time ago and cant for the life of me remember where I read the post nor can I find any comments I made on it but after installing the latest version I experienced the site breaking with issue #23 but after reverting the module back to my older hacked version and all is fine. I will dig around a bit more and try to compare the 2 versions to see what I changed however the file is dated 26/09/14 so it was some time ago and I imagine there have been many more changes since then.

I have uploaded the hacked version of my module in case anyone else wants to try it or look at it.

My lesson here is to document any hacks I do in a separate text file within the module directory in the future ;-)

mtoscano’s picture

Hi,
on my site users are able to create and save nodes but not to publish them and [node:nid] wasn't working on node saved, it was only when node where published. The hacked version in comment #33 worked for me: now nid are included in new nodes even if they are just saved and not published.

mtoscano’s picture

Sorry, actually my last comment is not true, the problem is different and a bit strange. I have the option "Automatically generate the label if the label field is left empty". With the last version or last dev, if I choose just [node:nid] it works, but if I add some text before or after it it doesn't and only that text is inserted in the node title. Then, if I edit the node, empty the title field and save, then it works.

  • bforchhammer committed afa4ff2 on 8.x-1.x authored by splatio
    Issue #1445124 by splatio, NancyDru | bforchhammer: Added support for...
Michael-IDA’s picture

I needed this for a non-Title field, which while this module implies it does it, it doesn't. Here's my code from D5 that auto incremented a Title field (w/ nodetitles module).

  $output = "Quote XXX";
  if (arg(0) == 'node' && arg(1) == 'add'){
    $result= db_query('
    select max( s.id ) 
    from sequences as s
    where s.name = "node_nid"
    ');
    if(mysql_num_rows($result)){
      $sid = mysql_fetch_array($result,MYSQL_NUM);
      $s_id = $sid[array_rand($sid)];
    }
    $s_id =$s_id +1;
    $output = "Quote $s_id";
  } elseif (arg(0) == 'node' && is_numeric(arg(1))) {
    $nodeid = arg(1);
    $output = "Quote $nodeid";
  } else {
    # just fooked...
    $output = "Quote nnn";
  }
  print $output;

There's always a better way, but this worked, without errors, on a fairly busy site. Translate to D7 as needed.

Best

  • bforchhammer committed afa4ff2 on 8.x-2.x authored by splatio
    Issue #1445124 by splatio, NancyDru | bforchhammer: Added support for...
asiby’s picture

Guys. This is still not working (from the dev version). Clean install and the [node:nid] token is not being replaced when creating a new node. Am I missing something. Somehow, the if (auto_entitylabel_is_needed($entity, $type, TRUE)) statement is being evaluated as false when creating a new node.