You are here

function social_follow_landing_page_preprocess_paragraph 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_landing_page/social_follow_landing_page.module \social_follow_landing_page_preprocess_paragraph()
  2. 10.1.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \social_follow_landing_page_preprocess_paragraph()
  3. 10.2.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \social_follow_landing_page_preprocess_paragraph()

Implements hook_preprocess_HOOK().

File

modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module, line 34
Contains social_follow_landing_page.module.

Code

function social_follow_landing_page_preprocess_paragraph(&$variables) {
  $terms = [];

  /** @var \Drupal\paragraphs\Entity\Paragraph $entity */
  $entity = $variables['elements']['#paragraph'];
  $bundle = $entity
    ->bundle();
  switch ($bundle) {
    case 'tag':
      if ($entity
        ->hasField('field_tag') && !empty($entity->field_tag)) {
        foreach ($entity->field_tag as $term_reference) {

          /** @var \Drupal\taxonomy\TermInterface $term */
          $term = $term_reference->entity;
          if (!empty($term)) {

            // Creates a link to a search page with a tag as a filter parameter.
            $parameter = 'tag';
            $route = 'view.search_content.page_no_value';

            // Override filter parameter id if split option is enabled.
            if (Drupal::getContainer()
              ->get('social_tagging.tag_service')
              ->allowSplit()) {
              $tag_service = Drupal::getContainer()
                ->get('social_tagging.tag_service');
              if (isset($term->parent)) {
                $parent = $term->parent->entity;
                if (!empty($parent)) {
                  $category = $parent
                    ->getName();

                  // Use the name of parent term as id of the filter parameter.
                  $parameter = social_tagging_to_machine_name($category);
                }
                elseif ($tag_service
                  ->useCategoryParent()) {
                  $category = $term
                    ->getName();
                  $parameter = social_tagging_to_machine_name($category);
                }
              }
            }
            $url = Url::fromRoute($route, [
              $parameter . '[]' => $term
                ->id(),
            ]);

            // Prepare additional data for the term variables.
            $terms[] = [
              'url' => $url
                ->toString(),
              '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),
            ];
          }
          $tag_id = $term
            ->id();
          $variables['#cache']['tags'][] = "follow_tag_node:{$tag_id}";
        }

        // Adding data to the terms variable to extend the template.
        $variables['terms'] = $terms;

        // Adding cache tag.
        $variables['#cache']['tags'][] = 'flagging_list';
      }
      break;
  }
}