You are here

class EntityHook in Editor Notes 8

Be aware of some entity hooks to perform actions on editor note.

Hierarchy

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\Hook
View 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

Namesort descending Modifiers Type Description Overrides
EntityHook::$editorNoteHelper protected property Editor Note helper service.
EntityHook::$entityTypeManager protected property The entity reference selection handler plugin manager.
EntityHook::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityHook::onEntityDelete public function Delete Editor Note on entity delete.
EntityHook::onEntityInsert public function Save Editor Note on entity insert.
EntityHook::onEntityRevisionDelete public function Delete on Editor Note revisions on entity revision delete.
EntityHook::onFieldDelete public function Delete Editor Note on field delete.
EntityHook::onUserCancel public function Reassign Editor Note on user cancel.
EntityHook::onUserDelete public function Delete Editor Note on user deletion.
EntityHook::__construct public function Constructor.