Closed (fixed)
Project:
Automatic Entity Label
Version:
8.x-2.x-dev
Component:
Code
Priority:
Normal
Category:
Plan
Assigned:
Unassigned
Reporter:
Created:
3 Nov 2017 at 06:59 UTC
Updated:
23 May 2023 at 23:37 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
purushotam.rai commentedAs of now, this seems bit difficult, as there is no direct way of resolving this because there is no way we can get nid in hook_pre_save, so we need to find some alternative way regarding this
Comment #3
OnkelTem commentedNobody forces us to use
preSaveonly. It's obvious that with the current database stuff you can set ID only after inserting the record. I'm not trying to explain you guys trivial things - you already know them. Anticipating your arguments I'm not providing patch as it's not going to be the part of the release anyway.Basically, instead of this:
you do:
and it would work. For obvious reasons.
UPDATE. I think I will provide patch finally as I'm going to use it in my
composer.jsonand I need a permanent link for that ;-)Comment #4
OnkelTem commentedComment #5
andileco commentedI applied the patch. I'm using the "Automatically generate the label and hide the label field" option for my node title.
When I create a new node, the title gets saved appropriately (the "Content type [title] has been created" message after saving is correct), but I get "%AutoEntityLabel%" as the H1 element on the node itself. I cleared caches and ran cron, but the "%AutoEntityLabel%" as the H1 on the node remained there until I edited the page and re-saved it.
Comment #6
mwebaze commentedHi @OnkelTem,
The entity->save in the hook_entity_insert throws an exception. As a work around I have had to call $entity->setNewRevision(FALSE) just before the save. This works if the entity supports revisions
Comment #7
durum commented@mwebaze Can you post a patch for that please?
Comment #8
andileco commentedThis is what @mwebaze and I have been using.
Comment #9
robertoperuzzoI installed the patch #8, but when I create a new content I get
%AutoEntityLabel%as entity label.These are installe module (snippet from composer.json):
Comment #10
andileco commented@robertoperuzzo, that's what I got with patch 4. Can you check that the line "$entity->setNewRevision(FALSE);" is present in your code? Also, please let us know if you have "create new revision" checked for your content type.
Comment #11
durum commentedI get the
%AutoEntityLabel%even after unchecking theCreate new revision. I tried with Administrator and Authenticated user to see if it is related toRevert revisionspermissions or alike.Comment #12
durum commentedNamespace was missing. It works now.
Comment #13
andileco commentedAh, thanks for figuring that out. I suspected it, but I created the patch outside a Drupal environment, and my IDE wasn't being helpful. Want me to write the new patch, or will you?
Comment #14
durum commentedOh thank you. I needed the the link for composer.json so I creaed it.
Comment #15
m.lebedev commented$node = \Drupal\node\Entity\Node::create(['type' => 'id']);
$node->save();
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null
Comment #16
robertoperuzzoThank you @durum, your patch #14 works like a charm.
Comment #17
denis.dorosh commentedHave same error like #15
(Automatically generate the label if the label field is left empty)
any idea?
for option (Automatically generate the label and hide the label field) - working good.
Comment #18
droprocker commentedSame problem as #17 dorian76453. Is there any solution yet?
Comment #19
artis commentedI'm getting the same error on both (Automatically generate the label and hide the label field) & (Automatically generate the label if the label field is left empty). Anybody found an answer?
Comment #20
artis commentedSo, the Title field has to be visible on "Manage Form Display" for "Automatically generate the label and hide the label field" to work. But (Automatically generate the label if the label field is left empty) still errors.
Comment #21
m.lebedev commentedComment #22
m.lebedev commentedNot yet. I forgot about changing the title of existing entities.
Comment #23
m.lebedev commentedComment #24
m.lebedev commentedChangelog 14-24:
- A new entity get a temporary title in presave if it not setted;
- Added save a not revisionable entity.
Comment #25
m.lebedev commentedI think that it is not necessary to check the existence of the label value. In any case, the new entity will be saved again. For example, when programmatically creating an entity and set a title, the title will be overwritten.
This is my final patch.
Comment #26
artis commentedpatch #25 does not apply to dev-2.x or 2.1-beta1.I remade the patch against dev-2.x but it resulted in the issue with %AutoEntityLabel% that was previously reported. So instead this new patch is based on patch #24 but correctly applies to dev-2.x.UPDATE: IGNORE THIS COMMENT AND PATCH. It was a mistake.
Comment #27
m.lebedev commentedAre you sure? I have successfully applied the patch.
Log:
I redid the patch number 24 because it is incompatible with entities that do not have the getTitle () method. In addition, I have already said that such the checking is no sense.
I checked your problem and it does not reproduce for me. I created a new type of node, enabled the option (automatically generate the label and hide the label field) or (automatically generate the label if the label field is left empty), setted the pattern for the label and it works. I tried to hide the title and it did not cause an error.
Comment #28
artis commentedYou were right about the patch applying correctly. I figured out my issue. I was using composer-patches to apply the patch and I just manually incremented the number on the link to '25' in my composer.json file. But in fact, your link ends with 25_1 so it was getting the wrong patch.
Additionally, your patch on #25 does it fact solve all of the issues reported here.
Thanks and sorry for the misunderstanding. I'm marking this RTBC.
Comment #29
m.lebedev commentedI have a case when I selected the option "Automatically generate the label if the label field is left empty" and setted the value "[node:content-type:name] #[node:nid]" in the field "the pattern for the label". After creating new node the title have the wrong value "BundleName #".
The token [node:nid] is not setted because the condition "if the label field is left empty" is FALSE and after that a second save of the node would not happen.
Comment #30
artis commentedI noticed that too, m.lebedev. It is unique to [node:nid] and the condition "if the label field is left empty".
The only way I can think to make this work is to save 'auto_entitylabel_empty' or something like that into the label field if it was left empty. Then test for that string and the "is left empty" condition to know if the auto label should be re-generated on the hook_entity_insert call or not.
This will add some complexity and could potentially experience issues if the temporary string was ever used by someone as an actual label value (unlikely), but the other choice is to document this situation and say it doesn't work.
Comment #31
m.lebedev commentedChangelog:
- New entity resaving only if the title has changed;
- Fixed a wrong checking on revisionable of the entity.
Helpful links:
Automatic Entity Label doesn't work with taxonomy terms
Automatic Entity Label not working with ECK entities
Broken when provider !=entityTypeId
Comment #32
skaughthave tested. Seems to work as expected.
Comment #33
Anonymous (not verified) commentedThis looks good to me as well - thanks to everyone that worked on this already.
Is there anything else we need to get this merged?
Comment #34
ivnishWhere are maintainers? It needs to be in 8.x-2.1-beta2
Comment #36
colanThanks!
Comment #38
ofauravi commentedNew patch version
Comment #39
jayelless commentedThis issue has been continued in #3362020: Update existing 'node' entity while changing the ID is not supported where an updated patch is available.