You are here

public function SocialFollowTagLazyBuilder::lazyBuild 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/src/SocialFollowTagLazyBuilder.php \Drupal\social_follow_tag\SocialFollowTagLazyBuilder::lazyBuild()
  2. 10.1.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php \Drupal\social_follow_tag\SocialFollowTagLazyBuilder::lazyBuild()
  3. 10.2.x modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php \Drupal\social_follow_tag\SocialFollowTagLazyBuilder::lazyBuild()

Returns tags for lazy builder.

File

modules/social_features/social_follow_taxonomy/modules/social_follow_tag/src/SocialFollowTagLazyBuilder.php, line 112

Class

SocialFollowTagLazyBuilder
Provide service for lazy rendering.

Namespace

Drupal\social_follow_tag

Code

public function lazyBuild() {
  $identifiers = [];

  // Get the tag category identifier that is used as a parameter in the URL.
  // It takes on the value of the parent term if the allow_category_split
  // settings is enabled or equal to the default name of the filter (tag).
  if ($this->tagService
    ->allowSplit()) {
    foreach ($this->tagService
      ->getCategories() as $tid => $value) {
      if (!empty($this->tagService
        ->getChildren($tid))) {
        $identifiers[] = social_tagging_to_machine_name($value);
      }
    }
  }
  else {
    $identifiers = [
      'tag',
    ];
  }

  // Get term id from url parameters.
  $term_ids = [];
  foreach ($identifiers as $identifier) {
    if (isset($_GET[$identifier])) {
      $term_ids = array_merge($term_ids, $_GET[$identifier]);
    }
  }
  $tags = [];

  /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
  $term_storage = $this->entityTypeManager
    ->getStorage('taxonomy_term');
  foreach ($term_ids as $term_id) {

    /** @var \Drupal\taxonomy\Entity\Term $term */
    $term = $term_storage
      ->load($term_id);

    // Show only tags followed by user.
    if ($term && social_follow_taxonomy_term_followed($term)) {
      $tags[$term_id] = [
        'name' => $term
          ->getName(),
        'flag' => social_follow_taxonomy_flag_link($term),
        'related_entity_count' => social_follow_taxonomy_related_entity_count($term, 'social_tagging'),
        'followers_count' => social_follow_taxonomy_term_followers_count($term),
      ];
    }
  }
  if (!empty($tags)) {
    $build = [
      '#theme' => 'search_follow_tag',
      '#tagstitle' => $this
        ->t('Tags'),
      '#tags' => $tags,
    ];

    // Generate cache tags.
    foreach ($tags as $tag_id => $tag) {
      $build['#cache']['tags'][] = "follow_tag_node:{$tag_id}";
    }
    return $build;
  }
  return [];
}