Problem/Motivation

In Drupal entity terminology, 'create' means an entity is created as a PHP object in memory, but is not saved to the database. However, most people not familiar with this might assume that docs that say "Acts on an entity after it is created but before hooks are invoked." mean that the entity has been saved to the database.

Proposed resolution

Add proposed text: See the @link entity_crud Entity CRUD topic @endlink for more information. to documentation
header on line 345 of /core/lib/Drupal/Core/Entity/EntityInterface.php.

Add additional documentation to explain that after postCreate there are several pre* hooks that could be fired before an Entity is passed to the save hook.

Remaining tasks

Awaiting RTBC

User interface changes

No user interface changes.

API changes

No API changes.

Data model changes

No data model changes.

API page: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Entity.php...

In Drupal entity terminology, 'create' means an entity is created as a PHP object in memory, but is not saved to the database. However, most people not familiar with this might assume that docs that say "Acts on an entity after it is created but before hooks are invoked." mean that the entity has been saved to the database.

We do explain in the general entity hook topic what we mean by 'create' but it might be an idea to mention it briefly here too.

Comments

joachim created an issue. See original summary.

cilefen’s picture

Version: 8.0.x-dev » 8.1.x-dev
jhodgdon’s picture

You don't think that the fact that the method is called postCreate() and not postSave() would imply it runs after the create() method and not the save() method? I think it's pretty clear from the method name... and the create() method is clearly documented to say it doesn't save the entity.

joachim’s picture

One of my co-maintainer thought that postCreate() acted after something was saved, so I think it's a mistake other people might make too.

jhodgdon’s picture

OK, fair enough! I would support making it clear that it runs after the create() method and making an @see link to that method. We should check the other pre* and post* methods and do the same for them.

joachim’s picture

Yes, or add something like "See the @topic page for details on when this is called in the entity CRUD workflow".

I don't remember which topic it is, but there's a page in the API docs that has a really clear step-by-step list of all hook and method executions.

chgasparoto’s picture

The topic link is this: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/group/entity_crud/8.

Is there a right way to add this big URL in docs?

jhodgdon’s picture

Yes. To link to a topic, you just need its ID, which in this case is "entity_crud" (if you look at the source code for this file, you can see that where it says @defgroup entity_crud -- and it's also near the end of the URL where it says group/entity_crud.

With the ID in hand, you can make a link in one of these three ways:

@see entity_crud
@ingroup entity_crud
See the @link entity_crud Entity CRUD topic @endlink for more information.

Method 1 -- adds it to the See Also section of the documentation page.
Method 2 -- adds it to the Related Topics section of the documentation page. And also makes the function/class/method that you are documenting show up on the Entity Crud topic page. (For this issue, I would not recommend doing this, because I don't think these pre* and post* methods are important enough to list on that topic page.)
Method 3 -- adds a link in the text of the documentation.

Also note the order of tags in doc blocks:
https://www.drupal.org/node/1354#order
All of the @tags are also documented on that page.

Thanks!

pepegarciag’s picture

@jhodgdon The best way to comment this function (postCreate()) could be something like this?

See the @link entity_crud Entity CRUD topic @endlink for more information.

chgasparoto’s picture

Status: Active » Needs review
StatusFileSize
new631 bytes

@jhodgdon I got it now, thanks.

As @pepegarciag suggested above, I also think this is the best approach.

Please review the patch.

P.s.: this patch shouldn't go to the branch 8.2.x-dev?

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the patch! This is a valid way to make a link to the Entity CRUD topic.

But the patch doesn't really solve the issue here completely. See comments #5 and #6 to understand what all needs to be done.

jmk61’s picture

@DrupalConNOLA and a group will be looking into this documentation issue.

kyleleber’s picture

Issue summary: View changes
jmk61’s picture

What I believe this issue/request is that not only do we need to have the "See the @link entity_crud Entity CRUD topic @endlink for more information." Element in the documentation, but also explain that after postCreate there are several pre* hooks that could be fired before an Entity is passed to the save hook.

kyleleber’s picture

Issue summary: View changes
kyleleber’s picture

Issue summary: View changes
jmk61’s picture

soulsesa’s picture

Status: Needs work » Needs review
kyleleber’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 17: drupal-docs_for_postCreate-2705921-16.patch, failed testing.

jmk61’s picture

I don't understand why this patch would fail a SimpleTest. It was purely a comment change.

vegantriathlete’s picture

Not sure why it failed. I do think that the patch also still needs the @see link to the create() method as discussed in comment 5.

vegantriathlete’s picture

Issue summary: View changes
vegantriathlete’s picture

Issue summary: View changes
vegantriathlete’s picture

So, to be explicit, I think we still need the

@see \Drupal\Core\Entity\EntityInterface::create()

reference

jmk61’s picture

Marc:

Thanks for the info. I will create a new interdiff and patch.

Thanks
Joey

jmk61’s picture

@vegantriathlete I've created a new patch and interdiff with the explicit @see

Thanks
Joey

snehi’s picture

Status: Needs work » Needs review

@jmk61 thanks for contributing.
Interdiff here mean that the difference between your patch and the latest patch just uploaded in the above comment.

Anyway Thanks. After uploading your patch please change status to need review :)
Making this for you.

vegantriathlete’s picture

Issue summary: View changes
jhodgdon’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Entity/EntityInterface.php
@@ -339,7 +339,12 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE);
   /**
-   * Acts on an entity after it is created but before hooks are invoked.
+   * Acts on an entity after create(), but before the entity is saved
+   * and hooks are invoked. postCreate() does not save the entity to db.

OK...

So first off, we always want a one-line description at the start of a function doc block. This needs to be split.

Then don't every use abbreviations like "db" in documentation.

And then... I don't think this description is right in the first place. Please go back to the issue summary to see what needs to be done.

vinay15’s picture

Assigned: Unassigned » vinay15
Status: Needs work » Needs review
StatusFileSize
new977 bytes
new1.06 KB

Tried to update the patch.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks! But again, PLEASE read the Proposed Resolution section of the issue summary.

This patch doesn't cover all of it:

Add additional documentation to explain that after postCreate there are several pre* hooks that could be fired before an Entity is passed to the save hook.

Also:

+++ b/core/lib/Drupal/Core/Entity/EntityInterface.php
@@ -339,10 +339,16 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE);
+   *
+   * See the @link entity_crud Entity CRUD topic @endlink for more information.
+   *
+   * @see \Drupal\Core\Entity\EntityInterface::create()

@see links go at the end of the doc block (as this one is).

However, text lines that say "See ...." need to go before the @param documentation.

So this line needs to move up.

vinay15’s picture

Status: Needs work » Needs review
StatusFileSize
new1.01 KB
new916 bytes

Updated the patch as per suggestions in #32.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

OK, that seems to be good enough, and cover the summary now. Plus, it's concise, always good. Thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 1683b18 and pushed to 8.1.x and 8.2.x. Thanks!

  • alexpott committed c962bd0 on 8.2.x
    Issue #2705921 by jmk61, Vinay15, chgasparoto, jhodgdon, vegantriathlete...

  • alexpott committed 1683b18 on 8.1.x
    Issue #2705921 by jmk61, Vinay15, chgasparoto, jhodgdon, vegantriathlete...

Status: Fixed » Closed (fixed)

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