<?php

/**
 * @file
 * preprocess-node.php
 *
 * Define all node preprocess HOOKs. Each bundle should provide it's own
 * hook function. e.g. `naomi_2025_preprocess_node__[bundle]`
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;

/**
 * Implements hook_preprocess_node().
 */
function naomi_2025_preprocess_node(&$variables) {
  $elements = $variables['elements'];

  /** @var \Drupal\node\Entity\Node $node */
  $node = $variables['node'];
  $node_bundle = $node->bundle();
  $node_view_mode = $elements['#view_mode'];

  if ($node_view_mode !== 'full') {
    $url = naomi_2025_get_node_url($node);

    $variables['node_url'] = $url;
    $variables['label_as_link'] = [
      '#type' => 'link',
      '#title' => Markup::create('<span>' . trim($node->label()) . '</span>'),
      '#url' => $url,
    ];
  }

  $variables['attributes']['id'] = Html::getClass('node-' . $node_bundle . '-' . $node->id());
  $variables['view_mode'] = $node_view_mode;

  // Remove some attributes.
  unset($variables['attributes']['role']);
  unset($variables['attributes']['about']);

  // Call custom function for the bundle, if it exists.
  $node_bundle_function = '_' . __FUNCTION__ . '__' . $node_bundle;
  if (function_exists($node_bundle_function)) {
    $node_bundle_function($variables);
  }
}

/**
 * Implements hook_preprocess_node() for project.
 */
function _naomi_2025_preprocess_node__project(&$variables) {
  /** @var \Drupal\node\Entity\Node $node */
  $node = $variables['node'];

  $field_project_type = $node?->field_project_type?->getString();

  // Attach the additional project library based on the project type.
  $variables['#attached']['library'][] = "naomi_2025/node.project.full." . Html::getClass($field_project_type);
}

/**
 * Implements hook_preprocess_node() for story.
 */
function _naomi_2025_preprocess_node__story(&$variables) {
  /** @var \Drupal\node\Entity\Node $node */
  $node = $variables['node'];

  $entity_type_manager = \Drupal::entityTypeManager();
  $term_storage = $entity_type_manager->getStorage('taxonomy_term');
  $field_ert_topics = $node?->field_ert_topics?->getString();
  $topic_terms = $term_storage->loadMultiple(explode(',', $field_ert_topics));

  $variables['topic_ids'] = $field_ert_topics;

  /** @var \Drupal\taxonomy\Entity\Term $topic */
  foreach ($topic_terms as $topic) {
    $variables['node_topics'][] = [
      '#type' => 'link',
      '#title' => $topic->getName(),
      '#url' => Url::fromRoute('entity.node.canonical', ['node' => 19], [
        'query' => [
          'topics' => [
            $topic->id() => $topic->id(),
          ],
        ],
      ]),
    ];
  }

  if ($variables['view_mode'] !== 'full') {
    $field_display_title = $node?->field_display_title?->getString();

    if ($field_display_title) {
      $variables['label_as_link'] = [
        '#type' => 'link',
        '#title' => Markup::create('<span>' . trim($field_display_title) . '</span>'),
        '#url' => $variables['node_url'],
      ];
    }

    // In view, create topic buttons.
    if (isset($variables['view'])) {
      /** @var Drupal\views\ViewExecutable $view */
      $view = $variables['view'];

      if (
        $view->id() === 'content_stories' &&
        $view->current_display === 'block_all_filterable'
      ) {
        $variables['node_topics'] = [];

        /** @var \Drupal\taxonomy\Entity\Term $topic */
        foreach ($topic_terms as $topic) {
          $variables['node_topics'][] = [
            '#type' => 'html_tag',
            '#tag' => 'button',
            '#value' => $topic->getName(),
            '#attributes' => [
              'data-topic-id' => $topic->id(),
            ],
          ];
        }
      }
    }
  }
}
