You are here

social_follow_tag.module in Open Social 10.3.x

File

modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.module
View source
<?php

/**
 * @file
 * Contains social_follow_tag.module.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;

/**
 * Implements hook_theme().
 */
function social_follow_tag_theme() {
  return [
    'social_tagging_nosplit' => [
      'base hook' => 'social_tagging_nosplit',
    ],
    'social_tagging_split' => [
      'base hook' => 'social_tagging_split',
    ],
    'search_follow_tag' => [
      'variables' => [
        'tagstitle' => NULL,
        'tags' => NULL,
      ],
    ],
    'social_tagging_popup' => [
      'variables' => [
        'url' => NULL,
        'name' => NULL,
        'flag' => NULL,
        'followers_count' => NULL,
        'related_entity_count' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_social_follow_taxonomy_terms_list_alter().
 *
 * {@inheritdoc}
 */
function social_follow_tag_social_follow_taxonomy_terms_list_alter(array &$term_ids, $entity) {

  /** @var \Drupal\node\Entity\Node $entity */
  if ($entity instanceof NodeInterface && $entity
    ->hasField('social_tagging')) {
    if (!empty($entity
      ->get('social_tagging')
      ->getValue())) {
      $tags = $entity
        ->get('social_tagging')
        ->getValue();
      foreach ($tags as $tag) {
        $term_ids[] = $tag['target_id'];
      }
    }
  }
  return $term_ids;
}

/**
 * Extends variables for social_tagging_split template.
 *
 * Implements hook_preprocess_HOOK().
 * {@inheritdoc}
 */
function social_follow_tag_preprocess_social_tagging_split(&$variables) {
  if (!isset($variables['taghierarchy'])) {
    return;
  }

  // Get entity type for rendered tags.
  $entity_type = $variables['entity_type'];

  /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
  $term_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');

  // Iterate over an array with parent terms (category).
  foreach ($variables['taghierarchy'] as $parent_tag) {
    if (!isset($parent_tag['tags'])) {
      continue;
    }

    // Iterate over an array with terms in each category.
    foreach ($parent_tag['tags'] as $tag_id => $tag_info) {
      $current_term = $term_storage
        ->load($tag_id);

      // Must be a valid Term.
      if (is_null($current_term)) {
        continue;
      }

      // Get current term parent.
      $parents = $term_storage
        ->loadParents($current_term
        ->id());
      if (!empty($parents)) {
        $wrapped_term = reset($parents);
      }
      else {
        $wrapped_term = $current_term;
      }

      // Adding additional data to the term variable to extend the template.
      $variables['taghierarchy'][$wrapped_term
        ->id()]['tags'][$current_term
        ->id()] = [
        'popup' => [
          '#create_placeholder' => TRUE,
          '#lazy_builder' => [
            'social_follow_tag.lazy_builder:popupLazyBuild',
            [
              $tag_info['url'],
              $current_term
                ->id(),
              'social_tagging',
              $entity_type,
            ],
          ],
        ],
        'name' => $tag_info['name'],
        'follow' => social_follow_taxonomy_term_followed($current_term),
      ];
      $variables['#cache']['tags'][] = "follow_tag_{$entity_type}:{$tag_id}";
    }
  }
  $variables['#cache']['tags'][] = 'flagging_list';
}

/**
 * Extends variables for social_tagging_nosplit template.
 *
 * Implements hook_preprocess_HOOK().
 * {@inheritdoc}
 */
function social_follow_tag_preprocess_social_tagging_nosplit(&$variables) {
  if (!isset($variables['tags'])) {
    return;
  }

  // Get entity type for rendered tags.
  $entity_type = $variables['entity_type'];

  /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
  $term_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  foreach ($variables['tags'] as $tag_name => $tag_info) {

    // Get term id from the transmitted url string.
    $term_id_from_string = explode('/search/content?tag%5B%5D=', $tag_info);
    if ($entity_type === 'group') {
      $term_id_from_string = explode('/search/groups?tag%5B%5D=', $tag_info);
    }
    if (isset($term_id_from_string[1])) {
      $current_term = $term_storage
        ->load($term_id_from_string[1]);
    }

    // Must be a valid Term.
    if (!isset($current_term) || !$current_term instanceof TermInterface) {
      continue;
    }

    // Adding additional data to the term variable to extend the template.
    $variables['tags'][$tag_name] = [
      'popup' => [
        '#create_placeholder' => TRUE,
        '#lazy_builder' => [
          'social_follow_tag.lazy_builder:popupLazyBuild',
          [
            $tag_info,
            $current_term
              ->id(),
            'social_tagging',
            $entity_type,
          ],
        ],
      ],
      'name' => $tag_name,
      'follow' => social_follow_taxonomy_term_followed($current_term),
    ];
    $tag_id = $current_term
      ->id();
    $variables['#cache']['tags'][] = "follow_tag_{$entity_type}:{$tag_id}";
  }
  $variables['#cache']['tags'][] = 'flagging_list';
}

/**
 * Create a notification when tags followed by a user are added to the node.
 *
 * Implements hook_entity_update().
 * {@inheritdoc}
 */
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);
    }
  }
}

Functions

Namesort descending Description
social_follow_tag_entity_update Create a notification when tags followed by a user are added to the node.
social_follow_tag_preprocess_social_tagging_nosplit Extends variables for social_tagging_nosplit template.
social_follow_tag_preprocess_social_tagging_split Extends variables for social_tagging_split template.
social_follow_tag_social_follow_taxonomy_terms_list_alter Implements hook_social_follow_taxonomy_terms_list_alter().
social_follow_tag_theme Implements hook_theme().