ckeditor_mentions-8.x-2.x-dev/modules/ckeditor_mentions_entity/ckeditor_mentions_entity.module

modules/ckeditor_mentions_entity/ckeditor_mentions_entity.module
<?php

/**
 * @file
 * Primary module hooks for Ckeditor Mentions Entity module.
 */

use Drupal\ckeditor_mentions_entity\Entity\Mention;
use Drupal\ckeditor_mentions_entity\MentionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\paragraphs\ParagraphInterface;

/**
 * Wrapper function collecting mentions from entities.
 */
function _ckeditor_mentions_entity_collect_mentions(ContentEntityInterface $entity) {
  /** @var \Drupal\ckeditor_mentions\MentionEventDispatcher $service */
  $service = \Drupal::service('ckeditor_mentions.mention_event_dispatcher');
  $mentions = [];

  foreach ($service->getMentionsFromEntity($entity) as $mention) {
    /** @var \DOMElement $anchor */
    $anchor = $mention['anchor'];
    /** @var \Drupal\Core\Entity\ContentEntityInterface $target */
    $target = $mention['entity'];

    if ($target instanceof ParagraphInterface) {
      $target = $target->getParentEntity();
    }

    if ($uuid = $anchor->getAttribute('data-mention-uuid')) {
      $mentions[$uuid] = $target;
    }
  }

  return $mentions;
}

/**
 * Implements hook_entity_delete().
 */
function ckeditor_mentions_entity_entity_delete(EntityInterface $entity) {
  if (($entity instanceof ContentEntityInterface) && !($entity instanceof MentionInterface)) {
    $storage = \Drupal::entityTypeManager()->getStorage('mention');
    $query = $storage->getQuery('OR')->accessCheck(FALSE);
    $type = $entity->getEntityTypeId();

    $condition_parent = $query->andConditionGroup()
      ->condition('parent.target_id', $entity->id())
      ->condition('parent.target_type', $type);

    $condition_target = $query->andConditionGroup()
      ->condition('target.target_id', $entity->id())
      ->condition('target.target_type', $type);

    if ($type == 'user') {
      $query->condition('uid', $entity->id());
    }

    $ids = $query->condition($condition_parent)
      ->condition($condition_target)
      ->execute();
    $mentions = $storage->loadMultiple($ids);

    $storage->delete($mentions);
  }
}

/**
 * Implements hook_entity_insert().
 */
function ckeditor_mentions_entity_entity_insert(EntityInterface $entity) {
  if (($entity instanceof ContentEntityInterface) && !($entity instanceof MentionInterface)) {
    if (!$entity->isDefaultRevision()) {
      return;
    }

    $account = \Drupal::currentUser();
    $mentions = _ckeditor_mentions_entity_collect_mentions($entity);

    foreach ($mentions as $uuid => $target_entity) {
      $mention_entity = Mention::create(['uuid' => $uuid]);
      $mention_entity->set('langcode', $entity->language()->getId())
        ->setOwnerId($account->id())
        ->setParentEntity($entity)
        ->setTargetEntity($target_entity)
        ->save();
    }
  }
}

/**
 * Implements hook_entity_update().
 */
function ckeditor_mentions_entity_entity_update(EntityInterface $entity) {
  if (($entity instanceof ContentEntityInterface) && !($entity instanceof MentionInterface)) {
    if (!$entity->isDefaultRevision()) {
      return;
    }

    /** @var \Drupal\Core\Entity\EntityRepositoryInterface $repository */
    $repository = \Drupal::service('entity.repository');
    $account = \Drupal::currentUser();

    $mentions_current = _ckeditor_mentions_entity_collect_mentions($entity);
    $mentions_original = _ckeditor_mentions_entity_collect_mentions($entity->original);

    $mentions_added = array_diff_key($mentions_current, $mentions_original);
    $mentions_removed = array_diff_key($mentions_original, $mentions_current);

    foreach ($mentions_added as $uuid => $target_entity) {
      $mention_entity = Mention::create(['uuid' => $uuid]);
      $mention_entity->set('langcode', $entity->language()->getId())
        ->setOwnerId($account->id())
        ->setParentEntity($entity)
        ->setTargetEntity($target_entity)
        ->save();
    }

    foreach ($mentions_removed as $uuid => $target_entity) {
      $mention_entity = $repository->loadEntityByUuid('mention', $uuid);

      if (isset($mention_entity)) {
        $mention_entity->delete();
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc