You are here

function social_topic_preprocess_node in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  2. 8 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  3. 8.2 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  4. 8.3 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  5. 8.4 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  6. 8.6 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  7. 8.7 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  8. 8.8 modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  9. 10.3.x modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  10. 10.0.x modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  11. 10.1.x modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()
  12. 10.2.x modules/social_features/social_topic/social_topic.module \social_topic_preprocess_node()

Prepares variables for node templates.

Default template: node.html.twig.

Most themes use their own copy of node.html.twig. The default is located inside "/core/modules/node/templates/node.html.twig". Look in there for the full list of variables.

Parameters

array $variables: An associative array containing:

  • elements: An array of elements to display in view mode.
  • node: The node object.
  • view_mode: View mode; e.g., 'full', 'teaser', etc.

File

modules/social_features/social_topic/social_topic.module, line 37
The Social topic module.

Code

function social_topic_preprocess_node(array &$variables) {

  /* @var \Drupal\node\NodeInterface $node */
  $node = $variables['node'];
  if ($node
    ->getType() === 'topic') {
    $topic_type = $node
      ->get('field_topic_type');
    $topic_type_entities = $topic_type
      ->referencedEntities();
    if (count($topic_type_entities) === 1) {
      $curr_langcode = \Drupal::languageManager()
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId();
      foreach ($topic_type_entities as $topic) {
        if ($topic
          ->isTranslatable() && $topic
          ->hasTranslation($curr_langcode)) {
          $topic = $topic
            ->getTranslation($curr_langcode);
        }
        $variables['metadata'] = t('in @topic', [
          '@topic' => $topic
            ->link(),
        ]);

        // Set topic type link.
        $topic_type_url = Url::fromRoute('view.latest_topics.page_latest_topics', [
          'field_topic_type_target_id' => $topic
            ->id(),
        ]);
        $topic_type_link = Link::fromTextAndUrl($topic
          ->label(), $topic_type_url)
          ->toString();
        $variables['topic_type'] = $topic_type_link;
      }
    }
    else {
      $variables['metadata'] = NULL;
      $variables['topic_type'] = NULL;
    }
  }
}