1

Using Drupal 8 and hooks, I am able to send an email to the current user, thanks to How to send mail programmatically in Drupal 8 and BCC For drupal mail function.

So, I define my message in hook_entity_update() and send it in hook_mail().

I am trying to send emails in the following case.

  1. user U1 consult a node created by user U2 (the aim is to permit to U1 to give his impression to U2 thanks to a select field). When U1 gave his impression, content is registered.
  2. I need to send a email to U2 (the node author), but since he isn't the current user, I cannot use \Drupal::currentUser().

In hook_entity_update(), how can I identify the author of the current entity, knowing that "author" is not provided by Drupal\Core\Entity\EntityInterface?

Is there a specific service?

2

2 Answers 2

4

Afaik, there's no requirement that a Content Entity must store the id of the user who created/edited it. However, many entity types do use a common method to store the owner information - Drupal core's user module provides an EntityOwnerTrait that many entity types use. (At least Node, Media, etc, all use this trait.)

For example, if you're looking for the owner of an entity that uses this trait, you can use the getOwner() method like:

$owner_user_object = $entity->getOwner();

It's unclear if user U2, in your example, would be the owner of the entity or just the author of a particular revision. If your entity is storing revision information, and the entity extends EditorialContentEntityBase (Note that they probably do. Node, Media, etc, all extend this base class) then it's also possible to load the author of a particular revision.

First, load the revision. See @baikho's comment pointing to Preferred way of loading a specific entity revision.

$entity_revision = \Drupal::entityTypeManager()
  ->getStorage('MY_ENTITY_EYPE')
  ->loadRevision($MY_REVISION_ID);

Once you'd loaded the revision, you can get the author of that revision with the getRevisionUser() method like:

$revision_user_object = $entity_revision->getRevisionUser()
2

EntityInterface doesn't have any method for getting the user who created an entity because not all the entities have the concept of owner. For example, the User entity is one of those entities.

The entities for which is defined an owner implement EntityOwnerInterface.

An owner is someone who has primary control over an entity, similar to owners in Unix file system access. This may or may not be the entity's original author. The owner may also have less permissions than other users, such as administrators.

If you are implementing hook_entity_update() for a generic entity and you want to check if it has an owner, you should use code similar to the following one.

use \Drupal\Core\Entity\EntityInterface;
use \Drupal\user\EntityOwnerInterface;

function hook_entity_update(EntityInterface $entity) {
  if ($entity instanceof EntityOwnerInterface) {
    // The entity has an owner (a.k.a. an author).
    $author_id = $entity->getOwnerId();
    $autor = $entity->getOwner();
  }
}

If the entity implements RevisionLogInterface (which means it supports revisions, and it usually extends RevisionableContentEntityBase), then it also defines a revision author, returned from getRevisionUser().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.