Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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).
Comment | File | Size | Author |
---|---|---|---|
#33 | auto_entitylabel_hacked.zip | 17.08 KB | GAMe |
#28 | Untitled-1.png | 48.54 KB | rclemings |
#14 | auto_entitylabel-entity-id-support-1445124-14.patch | 2.68 KB | msmithcti |
#12 | auto_entitylabel-entity-id-support-1445124-12.patch | 2.32 KB | msmithcti |
#4 | 1445124.patch | 1.34 KB | NancyDru |
Comments
Comment #1
bforchhammer CreditAttribution: bforchhammer commentedRelated discussion from automatic nodetitles module: #194197: Use (T)NID as node title
Comment #2
NancyDruSee also #1865432: Empty node title on initial creation.
Comment #3
bforchhammer CreditAttribution: bforchhammer commentedSorry, but changing the priority won't make this happen any sooner; providing a patch would :-)
Comment #4
NancyDruOkay, try this one.
Comment #5
bforchhammer CreditAttribution: bforchhammer commentedThanks for the patch, that's a good start :-)
We should use
hook_entity_insert()
to make this also work for entity types other than node.$node->title
will only work for nodes, but this should be supported for all entity types. I think it's should be something likeWhy is the
drupal_goto()
here?Comment #6
NancyDruThe 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.
Comment #7
bforchhammer CreditAttribution: bforchhammer commentedWell, 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 ofnode_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...This sounds unrelated to this module; I think this should be handled/fixed by the pathauto module.
Comment #8
NancyDruActually, 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.
Comment #9
matsbla CreditAttribution: matsbla commentedOne 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
Comment #10
NancyDru@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).
Comment #11
westie CreditAttribution: westie commentedOn 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
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.
Comment #12
msmithcti CreditAttribution: msmithcti commentedThanks 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()
.Comment #13
bforchhammer CreditAttribution: bforchhammer commented@splatio: Thanks for the updated patch!
auto_entitylabel_is_needed
does not work here, because it returns FALSE ifauto_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 fromauto_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).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.
More readable:
list($id, , ) = entity_extract_ids($type, $entity);
AFAIK the use of global variables such as $_SESSION is discouraged in Drupal; we should be able to use
drupal_static()
instead.Missing space before *.
I'm wondering whether this still works reliably when title replacement is used... did you test that by any chance?
Comment #14
msmithcti CreditAttribution: msmithcti commentedThanks 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.
Comment #15
bforchhammer CreditAttribution: bforchhammer commentedThanks! Looks good from a quick look... I'll try to test this myself next week, and possibly commit afterwards.
Comment #16
presleyd CreditAttribution: presleyd commentedTested tonight with a node that had a nid in it's title and it worked perfectly.
Comment #17
westie CreditAttribution: westie commentedLooks good to me.
Comment #18
bforchhammer CreditAttribution: bforchhammer commentedI 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()
inauto_entitylabel_entity_insert()
, because the title is generated without ids fromauto_entitylabel_entity_presave
already anyway, and we should avoid callingauto_entitylabel_set_title()
repeatedly as far as possible.Thanks everyone!
Comment #19
DerTobi75 CreditAttribution: DerTobi75 commentedThis 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!
Comment #20
bforchhammer CreditAttribution: bforchhammer commentedOn 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 tonode/%
, but comments do not go tocomment/%
!)Comment #21
DerTobi75 CreditAttribution: DerTobi75 commentedYes, 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
Comment #22
bforchhammer CreditAttribution: bforchhammer commentedWhat pattern are you using for pathauto?
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.
Comment #23
DerTobi75 CreditAttribution: DerTobi75 commentedFor 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
Comment #24
BWPanda CreditAttribution: BWPanda commentedSetting back to fixed. Thanks for this commit!
Comment #26
CodeWarrior-1 CreditAttribution: CodeWarrior-1 commentedI'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?
Comment #27
bforchhammer CreditAttribution: bforchhammer commentedPatch is already committed. Use the development version :)
Comment #28
rclemings CreditAttribution: rclemings commentedThis 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.
Comment #29
bforchhammer CreditAttribution: bforchhammer commentedNot sure whether this worked before this patch either; let's open a new issue, please :)
Comment #30
bforchhammer CreditAttribution: bforchhammer commentedSee #2005528: Preview mode does not correctly display titles
Comment #31
ikeigenwijs CreditAttribution: ikeigenwijs commentedI 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.
Comment #32
nithinkolekar CreditAttribution: nithinkolekar commentedsame 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.
Comment #33
GAMe CreditAttribution: GAMe commentedI 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 ;-)
Comment #34
mtoscano CreditAttribution: mtoscano commentedHi,
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.
Comment #35
mtoscano CreditAttribution: mtoscano commentedSorry, 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.
Comment #37
Michael-IDA CreditAttribution: Michael-IDA as a volunteer commentedI 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).
There's always a better way, but this worked, without errors, on a fairly busy site. Translate to D7 as needed.
Best
Comment #39
asiby CreditAttribution: asiby commentedGuys. 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.