class EntityHook in Editor Notes 8
Be aware of some entity hooks to perform actions on editor note.
Hierarchy
- class \Drupal\editor_note\Hook\EntityHook implements ContainerInjectionInterface
Expanded class hierarchy of EntityHook
1 file declares its use of EntityHook
- editor_note.module in ./
editor_note.module - Contains editor_node.module.
File
- src/
Hook/ EntityHook.php, line 17
Namespace
Drupal\editor_note\HookView source
class EntityHook implements ContainerInjectionInterface {
/**
* Editor Note helper service.
*
* @var \Drupal\editor_note\EditorNoteHelperService
*/
protected $editorNoteHelper;
/**
* The entity reference selection handler plugin manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructor.
*
* @param \Drupal\editor_note\EditorNoteHelperService $editor_note_helper
* Helper service object.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* EntityTypeManager object.
*/
public function __construct(EditorNoteHelperService $editor_note_helper, EntityTypeManagerInterface $entity_type_manager) {
$this->editorNoteHelper = $editor_note_helper;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('editor_note.helper'), $container
->get('entity_type.manager'));
}
/**
* Save Editor Note on entity insert.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity object.
*
* @see editor_note_entity_insert
*/
public function onEntityInsert(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
$field_definitions = $entity
->getFieldDefinitions();
foreach ($field_definitions as $field_name => $field_definition) {
if ($field_definition
->getType() === 'editor_note_item') {
$note_value = $entity
->get($field_name)->value;
if (empty($note_value)) {
return;
}
// Create Editor Note entity.
$this->editorNoteHelper
->createNote($entity, $field_name, $note_value);
}
}
}
/**
* Delete Editor Note on entity delete.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity object.
*
* @see editor_note_entity_delete
*/
public function onEntityDelete(EntityInterface $entity) {
$notes = $this->entityTypeManager
->getStorage('editor_note')
->loadByProperties([
'bundle' => $entity
->bundle(),
'entity_id' => $entity
->id(),
]);
foreach ($notes as $note) {
$note
->delete();
}
}
/**
* Delete Editor Note on user deletion.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity object.
*
* @see editor_note_entity_user_delete
*/
public function onUserDelete(EntityInterface $entity) {
$notes = $this->entityTypeManager
->getStorage('editor_note')
->loadByProperties([
'uid' => $entity
->id(),
]);
foreach ($notes as $note) {
$note
->delete();
}
}
/**
* Reassign Editor Note on user cancel.
*
* @param \Drupal\Core\Session\AccountInterface $account
* User object.
*
* @see editor_note_user_cancel
*/
public function onUserCancel(AccountInterface $account) {
$notes = $this->entityTypeManager
->getStorage('editor_note')
->loadByProperties([
'uid' => $account
->id(),
]);
foreach ($notes as $note) {
// Assign to anonymous user.
$note
->set('uid', 0);
$note
->save();
}
}
/**
* Delete on Editor Note revisions on entity revision delete.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity object.
*
* @see editor_note_entity_revision_delete
*/
public function onEntityRevisionDelete(EntityInterface $entity) {
$entities = $this->entityTypeManager
->getStorage('editor_note')
->loadByProperties([
'bundle' => $entity
->bundle(),
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getOriginalId(),
]);
foreach ($entities as $entity) {
$entity
->delete();
}
}
/**
* Delete Editor Note on field delete.
*
* @param \Drupal\field\Entity\FieldConfig $field
* The field being purged.
*
* @see editor_note_field_purge_field
*/
public function onFieldDelete(FieldConfig $field) {
$entities = $this->entityTypeManager
->getStorage('editor_note')
->loadByProperties([
'bundle' => $field
->getTargetBundle(),
'field_machine_name' => $field
->getName(),
]);
foreach ($entities as $entity) {
$entity
->delete();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityHook:: |
protected | property | Editor Note helper service. | |
EntityHook:: |
protected | property | The entity reference selection handler plugin manager. | |
EntityHook:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
EntityHook:: |
public | function | Delete Editor Note on entity delete. | |
EntityHook:: |
public | function | Save Editor Note on entity insert. | |
EntityHook:: |
public | function | Delete on Editor Note revisions on entity revision delete. | |
EntityHook:: |
public | function | Delete Editor Note on field delete. | |
EntityHook:: |
public | function | Reassign Editor Note on user cancel. | |
EntityHook:: |
public | function | Delete Editor Note on user deletion. | |
EntityHook:: |
public | function | Constructor. |