Problem/Motivation

When I select "Automatically generate the label and hide the label field"
and configure token : 'R-[node:nid]_[current-date:html_year' it works fine,

however when I select

"Automatically generate the label if the label field is left empty" with the same token R-[node:nid]_[current-date:html_year'
node:nid won't work and create R-_2021 instead

Is it possible to fix it? I need to migrate nids from the old site and cannot be changed, hence title will stay :R-[old_nid] which is fine, but when I create new content type it won't use node:nid. just already mentioned R-_2021

Steps to reproduce

-already mentioned

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#14 3205865-test-only.patch1021 byteskksandr
#9 3205865-9.patch6.64 KBkksandr
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

coaston created an issue. See original summary.

splash112’s picture

Did you find a solution to this? My entity tokens suddenly stopped working on 1 entity...

splash112’s picture

Noticed I had switched on Token CUSTOM DISPLAY SETTINGS. Switching that off again filled the entity label nicely.
Weird, as the config was exported from staging where it worked perfectly.

coaston’s picture

Hm, not sure i understand, does it mean you find a workaround?

splash112’s picture

Could be a bug, but would need to look into that. Noticed that node tokens for this module worked in 1 content type but not in the other.
Switching off custom display type for tokens at /admin/structure/types/manage/***/display fixed it.

ivnish’s picture

Status: Active » Closed (duplicate)
coaston’s picture

Ivnish - how this can be a duplicate if this is related to d8/9 version and you posted d7 link?
Please reopen it - still waiting for the patch!

coaston’s picture

Status: Closed (duplicate) » Needs work
kksandr’s picture

StatusFileSize
new6.64 KB

Hello, I faced the same problem.
The problem is that the module, if possible, populates the label in the presave hook where the entity ID is not yet available. And after that, the label is no longer considered empty and is not updated.

My solution is to add the ability to force the label to be re-save. To do this, a temporary label value is written to the label for new entities that do not have an identifier, which will also be considered empty for the next save. The new option in the form is called "Generate by resaving".

kksandr’s picture

Status: Needs work » Needs review
marcoliver’s picture

Status: Needs review » Reviewed & tested by the community

I just tested the patch from #9 and it works fine. Seems like a good solution!

Marking as RTBC.

dqd’s picture

Title: [node:nid] - label field is left empty » [node:nid] not added in label in a certain combination of settings
Version: 8.x-3.0-beta4 » 8.x-3.x-dev
Status: Reviewed & tested by the community » Postponed (maintainer needs more info)
/**
 * The temporary label for prevent database errors.
 */

Apart from some minor trouble in the patch which needs to be corrected/clarified, this issue is far behind and we need more reports if this issue is still reproducable at latest dev and latest stable release, which is 2 years younger than this issue here.

Additionally the patch needs to be rerolled then for latest dev if the issue still persist. Than we can think about a backport if required for circumstances where older versions of this project are mandatory.

kksandr’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1021 bytes

Apart from some minor trouble in the patch

The solution you quote is already used in this module. The patch just moves this string constant into a class constant for easy reuse.

This issue is still relevant, I have opened a merge request for the current version of the module.
I also added a test that reproduces the problem.

It would be nice if the module switched to Gitlab CI or fixed Drupal CI for automated testing.
Because the default tests fail via a deprecation error:

---Errors---
You are using the deprecated option "--no-suggest". It has no effect and will break in Composer 3.

dqd’s picture

@kksandr: Thanks for coming back on this. Much appreciated.

It would be nice if the module switched to Gitlab CI or fixed Drupal CI for automated testing.

Agreed. Feel free to open an issue for that. I would embrace that! +1 for the reminder.

The solution you quote is already used in this module.

Minor misunderstanding here: I was referring to minor gramma and typo in code comments as a minor nit pick review.

Thanks for the reroll! +1 For all the hard work in here. Will review the reroll asap.

kksandr’s picture

xavier.masson made their first commit to this issue’s fork.

liam morland made their first commit to this issue’s fork.

benstallings’s picture

Status: Needs review » Needs work

I ran this branch past Claude Code, and it said,

Here's my review of the fix on branch 3205865-id-not-added-2:

Summary

The patch adds an enforce_resave checkbox (visible only in OPTIONAL mode) that's meant to defer label generation until after the entity has been saved and has an ID. While
the problem diagnosis is correct, the fix is incomplete and likely doesn't work.

The Root Problem

When using OPTIONAL mode ("generate label if field is left empty") with a token like [node:nid], the label is generated during hook_entity_presave — before the entity has
been written to the database. At that point the entity has no ID, so [node:nid] resolves to empty, producing R-_2021 instead of R-123_2021.

Issues with This Patch

1. The resave is never triggered (Critical)

The patch makes setLabel() assign TEMP_LABEL when enforce_resave is enabled (AutoEntityLabelManager.php:205-206), but nothing in the insert hook actually triggers a resave
for this case. The insert hook at auto_entitylabel.module:266-268 only triggers a resave when:

$decorated_entity->getNewContentBehavior() === AutoEntityLabelManager::AFTER_SAVE

Since enforce_resave is independent of new_content_behavior (which defaults to BEFORE_SAVE), enabling enforce_resave alone results in entities permanently stuck with the
%AutoEntityLabel% temp label.

2. Redundant mechanism

The module already has new_content_behavior = AFTER_SAVE which solves this exact problem for ENABLED mode by:
1. Setting a UUID-based placeholder during presave
2. Registering a shutdown function to resave after insert
3. Generating the real label on the second presave (entity now has an ID)

The enforce_resave option is a parallel, incomplete copy of this mechanism.

3. The existing AFTER_SAVE doesn't work for OPTIONAL either

The reason AFTER_SAVE doesn't already fix OPTIONAL mode is that the UUID-based placeholder (%AutoEntityLabel: %) set during presave is not recognized by
autoLabelNeeded(). For OPTIONAL, autoLabelNeeded() checks labelIsEmpty(), which only matches empty strings or %AutoEntityLabel% — not the UUID variant. So the insert hook
sees a non-empty label and skips the resave.

4. Test likely doesn't pass

The test at AutoEntityLabelTest::testOptionalOptionWithId sets enforce_resave = TRUE but doesn't set new_content_behavior = AFTER_SAVE. Following the code path:
1. Presave: setLabel() sets TEMP_LABEL (because resavingNeeded() is true)
2. Entity saves, gets an ID
3. Insert hook: autoLabelNeeded() returns true (TEMP_LABEL is "empty"), but getNewContentBehavior() === AFTER_SAVE is false — no resave triggered
4. Entity keeps %AutoEntityLabel% as its title

The assertion assertEquals($title_prefix . $node->id(), $node->getTitle()) should fail.

Suggested Fix

Rather than adding a new config option, make the existing AFTER_SAVE mechanism work for OPTIONAL mode. The key change would be in autoLabelNeeded() — it needs to recognize
the UUID-based placeholder as a label that still needs generation. Something like:

public function autoLabelNeeded() {
$not_applied = empty($this->autoLabelApplied);
$required = $this->hasAutoLabel();
$optional = $this->hasOptionalAutoLabel() && $this->labelIsEmpty();
return $not_applied && ($required || $optional);
}

public function labelIsEmpty() {
$label = (string) $this->entity->label();
return strlen($label) == 0
|| str_starts_with($label, '%AutoEntityLabel');
}

This would make labelIsEmpty() catch both %AutoEntityLabel% and %AutoEntityLabel: %, allowing the insert hook's existing resave logic to work for OPTIONAL mode when
new_content_behavior = AFTER_SAVE. No new config option needed — users just select "Create label after first save" which already exists in the UI.