As we've classed entity types now there is no need to pass $type separately, so let's remove it.

Talking of

core/modules/entity/entity.api.php:function hook_entity_presave($entity, $type) {
core/modules/entity/entity.api.php:function hook_entity_insert($entity, $type) {
core/modules/entity/entity.api.php:function hook_entity_update($entity, $type) {
core/modules/entity/entity.api.php:function hook_entity_predelete($entity, $type) {
core/modules/entity/entity.api.php:function hook_entity_delete($entity, $type) {
core/modules/entity/entity.api.php:function hook_entity_view($entity, $type, $view_mode, $langcode) {
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fago’s picture

Issue tags: +Entity system, +sprint

added tags

aspilicious’s picture

larowlan’s picture

Status: Active » Needs review
FileSize
6.31 KB
aspilicious’s picture

Status: Needs review » Needs work

Thnx for starting this but this needs extra work

+++ b/core/modules/entity/entity.api.phpundefined
@@ -242,10 +240,8 @@ function hook_entity_load($entities, $type) {
  * @param $entity
  *   The entity object.
- * @param $type
- *   The type of entity being saved (i.e. node, user, comment).
  */
-function hook_entity_presave($entity, $type) {
+function hook_entity_presave(EntityInterface $entity) {

In .api.php files we need to declare te full path. EntiInterface should be Drupal\entity\EntityInterface. And we need to define the type after @param. SO that should be: @param Drupal\entity\EntityInterface $entity

Second of all you should grep for "Implement_hook_entity_*" in drupal 8. Replace * with one of the hooks.
You'll find one instance for all in a test file. There you need to add the typehint.

In those classes you can add "use Drupal\entity\EntityInterface" on top of the file and than you can use EntityInterface to typehint.

I hope this makes a bit sense... ;)

14 days to next Drupal core point release.

fgm’s picture

Status: Needs work » Needs review
FileSize
9.04 KB

Rerolled accordingly.

aspilicious’s picture

Status: Needs review » Needs work
+++ b/core/modules/entity/tests/modules/entity_crud_hook_test/entity_crud_hook_test.moduleundefined
@@ -5,10 +5,12 @@
+function entity_crud_hook_test_entity_presave(EntityInterface $entity, $type) {

We need to delete the $type if I'm correct :)

Same for the other functions

14 days to next Drupal core point release.

fgm’s picture

Status: Needs work » Needs review
FileSize
9.93 KB

Previous patch was still missing a few changes.

fgm’s picture

@aspilicious: indeed, just rerolled for that reason :-)

aspilicious’s picture

Status: Needs review » Needs work
+++ b/core/modules/entity/entity.api.phpundefined
@@ -327,17 +317,15 @@ function hook_entity_predelete($entity, $type) {
-  $info = entity_get_info($type);
-  list($id) = entity_extract_ids($type, $entity);
+  $info = entity_get_info($entity->entityType());
+  list($id) = entity_extract_ids($entity);
   db_delete('example_entity')

entity_extract id's is wrong is having incorrect paramaters. But we don't need that here anymore we just can call $entity->id() (or something like that, just check the entity.php class)

+++ b/core/modules/entity/entity.api.phpundefined
@@ -228,42 +228,36 @@ function hook_entity_info_alter(&$entity_info) {
+  list($id) = entity_extract_ids($entity);

same

+++ b/core/modules/entity/entity.api.phpundefined
@@ -274,20 +268,18 @@ function hook_entity_insert($entity, $type) {
+  list($id) = entity_extract_ids($entity);

Same

+++ b/core/modules/entity/entity.api.phpundefined
@@ -297,17 +289,15 @@ function hook_entity_update($entity, $type) {
+  list($id) = entity_extract_ids($entity);

Same

14 days to next Drupal core point release.

aspilicious’s picture

And the info stuff looks useless too...

fgm’s picture

Status: Needs work » Needs review
FileSize
9.38 KB

Not exactly the same issue, but close enough to include it.

aspilicious’s picture

+++ b/core/modules/entity/entity.api.phpundefined
@@ -440,12 +419,10 @@ function hook_entity_view_alter(&$build, $type) {
+function hook_entity_prepare_view($entities) {
   // Load a specific node into the user object for later theming.
-  if ($type == 'user') {
+  if ($entity->entityType() == 'user') {

mmm we have an entity array and we are fetching the type from it... We have to check if it's not empty and use the first entity than... (or just use the first entity as it just an api doc)

14 days to next Drupal core point release.

fgm’s picture

Good catch. This should be better.

I also added the full signature for Drupal\entity\EntityFieldQuery on hook_entity_query_alter() while changing that file.

aspilicious’s picture

Status: Needs review » Needs work
+++ b/core/modules/entity/entity.api.phpundefined
@@ -440,12 +419,10 @@ function hook_entity_view_alter(&$build, $type) {
+  if (!empty($entities) && reset($entities)->type() == 'user') {

entityType

14 days to next Drupal core point release.

fgm’s picture

Status: Needs work » Needs review
FileSize
9.71 KB

Can't even guess why I didn't change that one.

fago’s picture

Status: Needs review » Needs work
+++ b/core/modules/entity/entity.api.php
@@ -228,43 +228,35 @@ function hook_entity_info_alter(&$entity_info) {
- * @param $type
- *   The type of entities being loaded (i.e. node, user, comment).
  */
-function hook_entity_load($entities, $type) {

I don't think we want to remove it from the multiple hooks, as it's still useful there in that you don't have to look it up from your passed $entities.

+++ b/core/modules/entity/entity.api.php
@@ -421,7 +400,7 @@ function hook_entity_view($entity, $type, $view_mode, $langcode) {
-function hook_entity_view_alter(&$build, $type) {
+function hook_entity_view_alter(&$build) {

Is that information available in $build? Maybe, let's just pass $entity instead.

+++ b/core/modules/entity/entity.api.php
@@ -440,12 +419,10 @@ function hook_entity_view_alter(&$build, $type) {
-  if ($type == 'user') {
+  if (!empty($entities) && reset($entities)->entityType() == 'user') {

Imo, that's a change for the worse. Again, the multiple-issue.

aspilicious’s picture

Yeah I agree with the above actually...

Berdir’s picture

The view alter stuff looks like a duplicate of #1618140: hook_entity_view_alter() misses an $entity parameter, maybe just leave this out in this issue?

fgm’s picture

Status: Needs work » Needs review
FileSize
13.82 KB

Rerolled included #1618140: hook_entity_view_alter() misses an $entity parameter and the changes from #16. Tests for comment, entity, node, taxonomy, and user pass locally.

Status: Needs review » Needs work
Issue tags: -Entity system, -sprint

The last submitted patch, 0001-Issue-1618136-remove-type-and-entity_extract_ids-fro.patch, failed testing.

fgm’s picture

Status: Needs work » Needs review
Issue tags: +Entity system, +sprint

#19: 0001-Issue-1618136-remove-type-and-entity_extract_ids-fro.patch queued for re-testing.

(these 2 errors just do not make sense: they happen outside the test)

fago’s picture

Status: Needs review » Needs work
+++ b/core/modules/comment/comment.module
@@ -1044,7 +1043,7 @@ function comment_build_content(Comment $comment, Node $node, $view_mode = 'full'
-  module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
+  module_invoke_all('entity_view', 'comment', $view_mode, $langcode);

Ouch.

Also, aspilicious needs to get commit credits for this one as well as he wrote the code taken over from #1618140: hook_entity_view_alter() misses an $entity parameter.

fgm’s picture

Status: Needs work » Needs review
FileSize
13.88 KB

Ouch as you say. Rerolled and credit added.

aspilicious’s picture

Status: Needs review » Reviewed & tested by the community

Is rtbc IMO.

fgm’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
13.88 KB

Rerolled after yesternight's commits, just in case they broke something. Setting to CNR just for bot to check.

Damien Tournoud’s picture

I think this is conceptually blocked by #1551140: Remove stub entities, replace entity_create_stub_entity(). During the delete operation, we currently pass a stub of the entity to field hooks. That's not going to work anymore if the stub entities are not classed themselves.

fgm’s picture

@DamZ: just discussed this with Berdir, and he's saying that since #1551140: Remove stub entities, replace entity_create_stub_entity() only affects the field hook, and this only affects the entity CRUD hooks, it shouldn't be a blocker.

Damien Tournoud’s picture

Status: Needs review » Needs work

Talking with @berdir, we are not actually blocked here, because we load the entity everywhere, including in delete.

Looking at the patch, it seems to be missing updates to the documentation:

-  drupal_alter(array('taxonomy_term_view', 'entity_view'), $build, $type);
+  drupal_alter(array('taxonomy_term_view', 'entity_view'), $build, $term);

We modify the entity-type specific hook signature without updating its API documentation (true for all entity types in core).

fgm’s picture

Status: Needs work » Needs review
FileSize
17.78 KB

Rerolled accordingly.

fgm’s picture

Status: Needs review » Reviewed & tested by the community

resetting to rtbc as per #24.

fago’s picture

Status: Reviewed & tested by the community » Needs work

oh, still the additions need review before it can go to RTBC again. So here it is:

+++ b/core/modules/comment/comment.api.php
@@ -91,11 +93,13 @@ function hook_comment_view(Drupal\comment\Comment $comment, $view_mode, $langcod
+ * @param Drupal\entity\EntityInterface $entity
+ *   The entity object being rendered. Actually always a Comment.

We do type-hinting on the Comment class in that case, see the other comment specific hooks. The same issue applies to the other entity types as well.

fgm’s picture

Status: Needs work » Needs review
FileSize
17.62 KB

And here they are.

fago’s picture

Status: Needs review » Needs work
+++ b/core/modules/entity/entity.api.php
@@ -226,45 +226,39 @@ function hook_entity_info_alter(&$entity_info) {
- * @param $type
+  * @param string $entity_type

Somehow a whitespace made it in here?

fgm’s picture

Status: Needs work » Needs review
FileSize
17.62 KB

Cannot guess how this even happens.

fago’s picture

Status: Needs review » Needs work

That happens .. ;)

+++ b/core/modules/user/user.api.php
@@ -359,11 +361,13 @@ function hook_user_view($account, $view_mode, $langcode) {
-function hook_user_view_alter(&$build) {
+function hook_user_view_alter(&$build, Drupal\user\User $user) {

When looking at it for once another I've found another remark, this should be $account as it is for all the user hooks for consistency.

fgm’s picture

Status: Needs work » Needs review
FileSize
17.64 KB

Someday we'll get rid of global $user

fago’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, looks good now! :-)

As mentioned in #22 aspilicious needs to get commit credits for this one as well as he wrote the code taken over from #1618140: hook_entity_view_alter() misses an $entity parameter.

Also, this will need a change notice.

fgm’s picture

Prospective change notice created at http://drupal.org/node/1637540

webchick’s picture

So we had a discussion about this:

-function hook_entity_presave($entity, $type) {
+function hook_entity_presave(Drupal\entity\EntityInterface $entity) {

That looks bloody awful from a "newish to Drupal 8" perspective, but OTOH this is the current standard, so it's correct. The reason we do that is so you can copy/paste these implementations into your module without having to also figure out where the class comes from and toss the namespace at the top of your file. However, not the fault of this patch, and I'm actually reasonably sure I was one of the people advocating for this originally so.. ;P

Committed and pushed to 8.x. Thanks! :)

webchick’s picture

Also reviewed the change notice and it looks great. :)

webchick’s picture

Status: Reviewed & tested by the community » Fixed
sun’s picture

Status: Fixed » Needs work
+++ b/core/modules/node/node.module
@@ -1159,7 +1159,7 @@ function node_view(Node $node, $view_mode = 'full', $langcode = NULL) {
   $type = 'node';
-  drupal_alter(array('node_view', 'entity_view'), $build, $type);
+  drupal_alter(array('node_view', 'entity_view'), $build, $node);

Stray $type variable, which should be removed.

aspilicious’s picture

Ow we forgot to remove

$type = 'node';

fgm’s picture

Status: Needs work » Needs review
FileSize
757 bytes

Yup.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Simple enough, RTBC unless the testbot disagrees, and I don't think he will ;)

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks for the catch. :)

Status: Fixed » Closed (fixed)

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

fago’s picture

Issue tags: -sprint

Removing sprint tag.