You are here

function social_follow_tag_preprocess_social_tagging_split 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_preprocess_social_tagging_split()
  2. 10.1.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.module \social_follow_tag_preprocess_social_tagging_split()
  3. 10.2.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/social_follow_tag.module \social_follow_tag_preprocess_social_tagging_split()

Extends variables for social_tagging_split template.

Implements hook_preprocess_HOOK().

File

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

Code

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';
}