<?php

/**
 * @file
 * preprocess-field.php
 *
 * Define all field preprocess HOOKs.
 */

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

/**
 * Implements hook_preprocess_field().
 */
function naomi_2025_preprocess_field(&$variables) {
}

/**
 * Implements hook_preprocess_field() for advanced_header_field.
 */
function naomi_2025_preprocess_field__advanced_header_field(&$variables) {
  foreach ($variables['items'] as &$item) {
    $heading = $item['content']['#heading_text'] ?? $item['content']['#value'];

    $output = '';
    foreach (explode('|', $heading) as $text) {
      $output .= Markup::create('<span>' . Html::escape($text) . '</span>');
    }

    $item['content']['#value'] = $output;
  }
}

/**
 * Implements hook_preprocess_field() for address.
 */
function naomi_2025_preprocess_field__address(&$variables) {
  // We always want to remove United States.
  if ($variables['items'][0]['content']['country']['#value'] === 'United States') {
    $variables['items'][0]['content']['country']['#value'] = '';
  }
}

/**
 * Implements hook_preprocess_field() for field_social_links.
 */
function naomi_2025_preprocess_field__field_social_links(&$variables) {
  $element = $variables['element'];

  $object = $element['#object'];
  $object_label = $object->label();

  foreach ($variables['items'] as &$item) {
    $item_content_title = &$item['content']['#title'];

    $url = $item['content']['#url'];

    $url->setOptions([
      'attributes' => [
        'aria-label' => 'Go to ' . $object_label . '\'s ' . $item_content_title . ' page',
      ],
    ]);

    $item_content_title = [
      '#theme' => 'social_icon',
      '#social_name' => Html::getClass($item_content_title),
    ];
  }
}
