That code needs to be changed for the following reasons.
In PHP, $entity->getEntityType() == ('Deadline' || 'Message')
doesn't check that $entity->getEntityType()
returns either 'Deadline'
or 'Message'
. Actually, the code you wrote is equivalent to the following one.
if ($entity->getEntityType() == TRUE) {
createNotification($entity);
}
getEntityType()
doesn't return the ID of the type of the entity, but its definition. The method that returns the ID of the type of the entity is getEntityTypeId()
.
It doesn't make sense to check the value returned from $entity->getEntityTypeId()
in hook_ENTITY_TYPE_create()
because hook_ENTITY_TYPE_create()
is invoked only for the ENTITY_TYPE entities. (You are also supposed to implement hook_ENTITY_TYPE_create()
by replacing ENTITY_TYPE with the entity type, for example node for the Node entity, or user for the User entity.)
EntityInterface
is an interface, not a class; as such, it's never used to call a method, even if it's statically called as in the code you are using.
The correct code is similar to the following one. I assume that ost_hook is the module machine name, notification is entity type, and Notification
is the class implementing the entity you are trying to create.
function ost_hook_entity_create(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'Deadline' || $entity->getEntityTypeId() == 'Message') {
$new_entity = Notification::create([
'title' => $entity->label() . " notification",
'field_content' => $entity->toLink()
]);
$new_entity->save();
}
}
Alternatively, you could use the following code.
function ost_hook_entity_create(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'Deadline' || $entity->getEntityTypeId() == 'Message') {
$new_entity = \Drupal::entityTypeManager()->getStorage('notification')->create([
'title' => $entity->label() . " notification",
'field_content' => $entity->toLink()
]);
$new_entity->save();
}
}
If the file containing the hook is ost.module (as I suspect), the function implementing the hook is ost_entity_create()
, not ost_hook_entity_create()
.
I used hook_entity_create()
because the code needs to be executed for two different entity types. Using hook_ENTITY_TYPE_create()
, I should implement two hooks. (I assume that dealine and message are the entity machine names, in the same way the machine name for the Node entity is node.)
function ost_deadline_create(EntityInterface $entity) {
$new_entity = \Drupal::entityTypeManager()->getStorage('notification')->create([
'title' => $entity->label() . " notification",
'field_content' => $entity->toLink()
]);
$new_entity->save();
}
function ost_message_create(EntityInterface $entity) {
$new_entity = \Drupal::entityTypeManager()->getStorage('notification')->create([
'title' => $entity->label() . " notification",
'field_content' => $entity->toLink()
]);
$new_entity->save();
}
As side note, functions implemented from a module should always be prefixed by the module machine name. A module shouldn't have a createNotification()
function, which should instead be called ost_create_notification()
(assuming that ost is the module machine name).
node
with your own entity.