You are here

function social_follow_taxonomy_preprocess_activity in Open Social 10.3.x

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

Extends variables for activity template.

Implements hook_preprocess_activity().

File

modules/social_features/social_follow_taxonomy/social_follow_taxonomy.module, line 103
Contains social_follow_taxonomy.module.

Code

function social_follow_taxonomy_preprocess_activity(&$variables) {
  $activity = $variables['elements']['#activity'];
  $message_template = [
    'create_discussion',
    'create_event_community',
    'create_topic_community',
    'create_discussion_group',
    'create_event_group',
    'create_idea_group',
    'create_topic_group',
    'update_node_following_tag',
    'create_post_group',
    'update_post_following_tag',
  ];
  $message = Message::load($activity->field_activity_message->target_id);
  $message_template_id = $message
    ->getTemplate()
    ->id();
  if (in_array($message_template_id, $message_template)) {
    $entity = $activity
      ->getRelatedEntity();
    if ($entity
      ->getEntityTypeId() === 'node') {
      $storage = \Drupal::entityTypeManager()
        ->getStorage('flagging');
      if ($entity instanceof NodeInterface) {
        $term_ids = social_follow_taxonomy_terms_list($entity);
        foreach ($term_ids as $term_id) {
          $flag = $storage
            ->loadByProperties([
            'flag_id' => 'follow_term',
            'entity_type' => 'taxonomy_term',
            'entity_id' => $term_id,
            'uid' => \Drupal::currentUser()
              ->id(),
          ]);
          $flag = reset($flag);
          if (!empty($flag)) {

            /** @var \Drupal\taxonomy\TermInterface $term */
            $term = \Drupal::entityTypeManager()
              ->getStorage('taxonomy_term')
              ->load($term_id);
            $variables['content_type'] = $entity->type->entity
              ->label();
            $variables['followed_tags'][$term_id] = [
              'name' => $term
                ->getName(),
              'flag' => social_follow_taxonomy_flag_link($term),
            ];
          }
        }
      }
    }
    if ($entity
      ->getEntityTypeId() === 'post') {
      $storage = \Drupal::entityTypeManager()
        ->getStorage('flagging');
      if ($entity instanceof PostInterface) {
        $term_ids = social_follow_taxonomy_terms_list($entity);
        foreach ($term_ids as $term_id) {
          $flag = $storage
            ->loadByProperties([
            'flag_id' => 'follow_term',
            'entity_type' => 'taxonomy_term',
            'entity_id' => $term_id,
            'uid' => \Drupal::currentUser()
              ->id(),
          ]);
          $flag = reset($flag);
          if (!empty($flag)) {

            /** @var \Drupal\taxonomy\TermInterface $term */
            $term = \Drupal::entityTypeManager()
              ->getStorage('taxonomy_term')
              ->load($term_id);
            $variables['content_type'] = $entity
              ->getEntityType()
              ->getLabel();
            $variables['followed_tags'][$term_id] = [
              'name' => $term
                ->getName(),
              'flag' => social_follow_taxonomy_flag_link($term),
            ];
          }
        }
      }
    }
  }
}