Hooking into Drupal to react to entity changes
One of the most common integration points is hooking into Drupal to react to the create, read, update, and delete operations of an entity. The entity system also has hooks to provide default values when instantiating a new entity and modifying it before it is saved.
In this recipe, we will create a hook that runs whenever new content is published and send an email to the site’s email address as a notification of the new content.
How to do it…
- First, create a file called
mymodule.module
in yourmodule
file. This is the module extension file that stores hook implementations. - Next, we will implement a hook to listen for new node entities being inserted. Create a function named
mymodule_node_insert
, which is an implementation of thehook_ENTITY_TYPE_insert
hook:<?php
function mymodule_node_insert(\Drupal\node\
NodeInterface $node) {
}
- In our
insert
hook, we will check if the node...