<?php

/**
 * @file
 * Contains wxt_ext_metatag.module.
 */

use Drupal\wxt_ext_metatag\Plugin\metatag\Tag\AdobeAnalyticsAttributeBase;

/**
 * Implements hook_metatags_attachments_alter().
 */
function wxt_ext_metatag_metatags_attachments_alter(array &$metatag_attachments) {
  foreach ($metatag_attachments['#attached']['html_head'] as &$tag) {
    switch ($tag[1]) {
      case 'dcterms_issued':
        $tag[0]['#attributes']['title'] = 'W3CDTF';
        break;

      case 'dcterms_modified':
        $tag[0]['#attributes']['title'] = 'W3CDTF';
        break;

      case 'dcterms_subject':
        $tag[0]['#attributes']['title'] = 'gccore';
        break;

      case 'dcterms_language':
        $tag[0]['#attributes']['title'] = 'ISO639-2/T';

        // Need language code in ISO 639-2 format.
        $content = $tag[0]['#attributes']['content'];

        if ($content == 'en') {
          $tag[0]['#attributes']['content'] = 'eng';
        }
        elseif ($content == 'fr') {
          $tag[0]['#attributes']['content'] = 'fra';
        }
        break;
    }
  }
}

/**
 * Implements hook_metatags_alter().
 */
function wxt_ext_metatag_metatags_alter(array &$metatags, array &$context): void {
  // Update the metadata values for all that use AdobeAnalyticsAttributeBase.
  AdobeAnalyticsAttributeBase::alterMetatags($metatags, $context);
}
