<?php

/**
 * @file
 * Contains metatag_open_graph.module.
 */

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements template_preprocess_html().
 */
function metatag_open_graph_preprocess_html(&$variables) {
  if (!metatag_is_current_route_supported()) {
    return;
  }

  // Add XML namespaces if the RDF module is not enabled as it adds these two
  // automatically.
  if (!isset($variables['html_attributes'])) {
    $variables['html_attributes'] = [];
  }
  $namespaces = [];
  if (!\Drupal::moduleHandler()->moduleExists('rdf')) {
    $namespaces = [
      'xmlns:dc' => 'http://purl.org/dc/terms/',
      'xmlns:og' => 'http://ogp.me/ns#',
    ];
  }

  // Namespaces for OpenGraph.
  $namespaces['xmlns:article'] = "http://ogp.me/ns/article#";
  $namespaces['xmlns:book'] = "http://ogp.me/ns/book#";
  $namespaces['xmlns:product'] = "http://ogp.me/ns/product#";
  $namespaces['xmlns:profile'] = "http://ogp.me/ns/profile#";
  $namespaces['xmlns:video'] = "http://ogp.me/ns/video#";

  // Namespaces for Google+.
  if (isset($variables['itemtype'])) {
    $namespaces['itemscope'] = '';
    $namespaces['itemtype'] = "http://schema.org/{$variables['itemtype']}";
  }

  // Append each namespace.
  foreach ($namespaces as $namespace => $uri) {
    $variables['html_attributes'][$namespace] = $uri;
  }
}
