You are here

function social_follow_tag_entity_update in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.0.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.module \social_follow_tag_entity_update()
  2. 10.1.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.module \social_follow_tag_entity_update()
  3. 10.2.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.module \social_follow_tag_entity_update()

Create a notification when tags followed by a user are added to the node.

Implements hook_entity_update().

File

modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.module, line 189
Contains social_follow_tag.module.

Code

function social_follow_tag_entity_update(EntityInterface $entity) {
  $taxonomy_ids = [];
  $original_taxonomy_ids = [];

  /** @var \Drupal\social_queue_storage\Entity\QueueStorageEntity $entity */
  if ($entity
    ->getEntityTypeId() === 'node') {

    // Prepare list of tags before update node.
    if (!is_null($entity->original)) {
      $original_entity = $entity->original;
      if ($original_entity
        ->hasField('social_tagging')) {
        if (!empty($original_entity
          ->get('social_tagging')
          ->getValue())) {
          $original_tags = $original_entity
            ->get('social_tagging')
            ->getValue();
          foreach ($original_tags as $original_tag) {
            $original_taxonomy_ids[] = $original_tag['target_id'];
          }
        }
      }
    }

    // Prepare list of tags after update node.
    if ($entity
      ->hasField('social_tagging')) {
      if (!empty($entity
        ->get('social_tagging')
        ->getValue())) {
        $tags = $entity
          ->get('social_tagging')
          ->getValue();
        foreach ($tags as $tag) {

          // Check if new tags have been added after the update.
          if (!in_array($tag['target_id'], $original_taxonomy_ids)) {
            $taxonomy_ids[] = $tag['target_id'];
          }
        }
      }
    }

    // Set the creation time because it is used as notification creation time.
    $entity
      ->setCreatedTime($entity
      ->getChangedTime());

    // Set the owner id because it is used as the id of the notification author.
    $entity
      ->setOwnerId(\Drupal::currentUser()
      ->id());

    // Create activity notification.
    if (!empty($taxonomy_ids)) {
      \Drupal::service('plugin.manager.activity_action.processor')
        ->createInstance('update_entity_action')
        ->create($entity);
    }
  }
}