EntityHook.php in Editor Notes 8
File
src/Hook/EntityHook.php
View source
<?php
namespace Drupal\editor_note\Hook;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\editor_note\EditorNoteHelperService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\field\Entity\FieldConfig;
class EntityHook implements ContainerInjectionInterface {
protected $editorNoteHelper;
protected $entityTypeManager;
public function __construct(EditorNoteHelperService $editor_note_helper, EntityTypeManagerInterface $entity_type_manager) {
$this->editorNoteHelper = $editor_note_helper;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('editor_note.helper'), $container
->get('entity_type.manager'));
}
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;
}
$this->editorNoteHelper
->createNote($entity, $field_name, $note_value);
}
}
}
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();
}
}
public function onUserDelete(EntityInterface $entity) {
$notes = $this->entityTypeManager
->getStorage('editor_note')
->loadByProperties([
'uid' => $entity
->id(),
]);
foreach ($notes as $note) {
$note
->delete();
}
}
public function onUserCancel(AccountInterface $account) {
$notes = $this->entityTypeManager
->getStorage('editor_note')
->loadByProperties([
'uid' => $account
->id(),
]);
foreach ($notes as $note) {
$note
->set('uid', 0);
$note
->save();
}
}
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();
}
}
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();
}
}
}
Classes
Name |
Description |
EntityHook |
Be aware of some entity hooks to perform actions on editor note. |