Comments

Sinan Erdem created an issue. See original summary.

purushotam.rai’s picture

Title: [node:nid] token is not available after first save » Support [node:nid] token | specially for prefilled option
Category: Bug report » Plan
Related issues: +#2221347: Add hook_entity_postsave hook

As 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

OnkelTem’s picture

Nobody forces us to use preSave only. 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:

/**
 * Implements hook_entity_presave().
 */
function auto_entitylabel_entity_presave(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    $decorator = \Drupal::service('auto_entitylabel.entity_decorator');
    /** @var \Drupal\auto_entitylabel\AutoEntityLabelManager $decorated_entity */
    $decorated_entity = $decorator->decorate($entity);
    if ($decorated_entity->hasLabel() && $decorated_entity->autoLabelNeeded()) {
      $decorated_entity->setLabel();
    }
  }
}

you do:

/**
 * Implements hook_entity_presave().
 */
function auto_entitylabel_entity_presave(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    if (!$entity->isNew()) {
      $decorator = \Drupal::service('auto_entitylabel.entity_decorator');
      /** @var \Drupal\auto_entitylabel\AutoEntityLabelManager $decorated_entity */
      $decorated_entity = $decorator->decorate($entity);
      if ($decorated_entity->hasLabel() && $decorated_entity->autoLabelNeeded()) {
        $decorated_entity->setLabel();
      }
    }
  }
}

/**
 * Implements hook_entity_insert().
 */
function auto_entitylabel_entity_insert(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    $decorator = \Drupal::service('auto_entitylabel.entity_decorator');
    /** @var \Drupal\auto_entitylabel\AutoEntityLabelManager $decorated_entity */
    $decorated_entity = $decorator->decorate($entity);
    if ($decorated_entity->hasLabel() && $decorated_entity->autoLabelNeeded()) {
      $entity->save();
    }
  }
}

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.json and I need a permanent link for that ;-)

OnkelTem’s picture

andileco’s picture

Status: Active » Needs work

I 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.

mwebaze’s picture

Hi @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

durum’s picture

@mwebaze Can you post a patch for that please?

andileco’s picture

Status: Needs work » Needs review
StatusFileSize
new1.43 KB

This is what @mwebaze and I have been using.

robertoperuzzo’s picture

StatusFileSize
new97.7 KB

I 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):

"require": {
        "composer/installers": "^1.0.20",
        "drupal-composer/drupal-scaffold": "^2.0.1",
        "cweagans/composer-patches": "~1.0",
        "drupal/core": "~8.0",
        "drush/drush": "~9.0",
        "drupal/console": "~1.0",
        "drupal/bootstrap": "~3.10",
        "drupal/google_analytics": "~2.2",
        "drupal/metatag": "~1.4",
        "drupal/pathauto": "~1.1",
        "drupal/redirect": "~1.0",
        "drupal/simple_sitemap": "~2.11",
        "drupal/sitemap": "~1.3",
        "drupal/mailsystem": "~4.1",
        "drupal/config_split": "~1.3",
        "drupal/config_installer": "~1.7",
        "drupal/config_ignore": "~2.1",
        "drupal/twig_tweak": "~1.9",
        "drupal/adminimal_admin_toolbar": "~1.5",
        "drupal/adminimal_theme": "~1.3",
        "drupal/auto_entitylabel": "~2.0"
    },
    "require-dev": {
        "behat/mink": "~1.7",
        "behat/mink-goutte-driver": "~1.2",
        "jcalderonzumba/gastonjs": "~1.0.2",
        "jcalderonzumba/mink-phantomjs-driver": "~0.3.1",
        "mikey179/vfsStream": "~1.2",
        "phpunit/phpunit": "~4.8",
        "symfony/css-selector": "~2.8",
        "drupal/devel": "~1.2",
        "drupal/reroute_email": "~1.0",
        "drupal/stage_file_proxy": "1.x-dev",
        "drupal/kint": "~1.2"
    },
andileco’s picture

@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.

durum’s picture

I get the %AutoEntityLabel% even after unchecking the Create new revision. I tried with Administrator and Authenticated user to see if it is related to Revert revisions permissions or alike.

durum’s picture

Namespace was missing. It works now.

andileco’s picture

Ah, 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?

durum’s picture

Oh thank you. I needed the the link for composer.json so I creaed it.

m.lebedev’s picture

$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

robertoperuzzo’s picture

Thank you @durum, your patch #14 works like a charm.

denis.dorosh’s picture

Have 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.

droprocker’s picture

Same problem as #17 dorian76453. Is there any solution yet?

artis’s picture

Status: Needs review » Needs work

I'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?

artis’s picture

So, 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.

m.lebedev’s picture

Status: Needs work » Needs review
StatusFileSize
new1.71 KB
m.lebedev’s picture

Status: Needs review » Needs work

Not yet. I forgot about changing the title of existing entities.

m.lebedev’s picture

Status: Needs work » Needs review
StatusFileSize
new1.73 KB
m.lebedev’s picture

StatusFileSize
new1.73 KB

Changelog 14-24:
- A new entity get a temporary title in presave if it not setted;
- Added save a not revisionable entity.

m.lebedev’s picture

StatusFileSize
new1.16 KB

I 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.

artis’s picture

patch #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.

m.lebedev’s picture

Are you sure? I have successfully applied the patch.

Log:

1000@ee7967a60ce1:/var/www/devel2.loc/www$ ~/.composer/vendor/bin/drush dl auto_entitylabel --select
Choose one of the available releases for auto_entitylabel:
 [0]  :  Cancel                                                   
 [1]  :  8.x-2.x-dev    -  2018-Sep-05  -  Development            
 [2]  :  8.x-2.1-beta1  -  2017-Nov-12  -  Supported, Recommended 
 [3]  :  8.x-1.x-dev    -  2017-Apr-18  -  Supported, Development
1
Install location /var/www/devel2.loc/www//modules/contrib/auto_entitylabel already exists. Do you want to overwrite it? (y/n): y
Project auto_entitylabel (8.x-2.x-dev) downloaded to /var/www/devel2.loc/www//modules/contrib/auto_entitylabel.                                                                                          [success]
1000@ee7967a60ce1:/var/www/devel2.loc/www$ cd /var/www/devel2.loc/www/modules/contrib/auto_entitylabel 
1000@ee7967a60ce1:/var/www/devel2.loc/www/modules/contrib/auto_entitylabel$ wget https://www.drupal.org/files/issues/2018-10-30/auto_entitylabel-support_entity_id-2920695-25_1.patch
--2018-10-31 12:01:43--  https://www.drupal.org/files/issues/2018-10-30/auto_entitylabel-support_entity_id-2920695-25_1.patch
Resolving www.drupal.org (www.drupal.org)... 151.101.113.175
Connecting to www.drupal.org (www.drupal.org)|151.101.113.175|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1183 (1.2K) [text/plain]
Saving to: 'auto_entitylabel-support_entity_id-2920695-25_1.patch'

auto_entitylabel-support_entity_id-2920695-25_1.patc 100%[======================================================================================================================>]   1.16K  --.-KB/s    in 0s      

2018-10-31 12:01:43 (81.5 MB/s) - 'auto_entitylabel-support_entity_id-2920695-25_1.patch' saved [1183/1183]

1000@ee7967a60ce1:/var/www/devel2.loc/www/modules/contrib/auto_entitylabel$ patch -p1 < auto_entitylabel-support_entity_id-2920695-25_1.patch 
patching file auto_entitylabel.module
1000@ee7967a60ce1:/var/www/devel2.loc/www/modules/contrib/auto_entitylabel$ 

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'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?

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.

artis’s picture

Status: Needs review » Reviewed & tested by the community

You 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.

m.lebedev’s picture

I 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.

artis’s picture

I 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.

m.lebedev’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.19 KB

Changelog:
- 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

skaught’s picture

Status: Needs review » Reviewed & tested by the community

have tested. Seems to work as expected.

Anonymous’s picture

This looks good to me as well - thanks to everyone that worked on this already.

Is there anything else we need to get this merged?

ivnish’s picture

Where are maintainers? It needs to be in 8.x-2.1-beta2

  • m.lebedev authored 0cc1fb7 on 8.x-2.x
    Issue #2920695 by m.lebedev, artis, durum, andileco, OnkelTem,...
colan’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

Status: Fixed » Closed (fixed)

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

ofauravi’s picture

New patch version

jayelless’s picture

This issue has been continued in #3362020: Update existing 'node' entity while changing the ID is not supported where an updated patch is available.