You are here

function context_metadata_html_head_alter in Context Metadata 7

Implements hook_html_head_alter()

Parameters

unknown_type $elements:

File

./context_metadata.module, line 75

Code

function context_metadata_html_head_alter(&$elements) {
  unset($elements['system_meta_generator']);
  $metadata = drupal_static('metadata_array');
  if (isset($metadata['metadata_description'])) {
    $value = token_replace($metadata['metadata_description']);
    if (!empty($value)) {
      $elements['metatag_description'] = array(
        '#theme' => 'metatag',
        '#tag' => 'meta',
        '#value' => $value,
        '#type' => 'html_tag',
        '#id' => 'metatag_description',
        '#name' => 'description',
      );
    }
  }
  if (isset($metadata['metadata_keywords'])) {
    $value = token_replace($metadata['metadata_keywords']);
    if (!empty($value)) {
      $elements['metadata_keywords'] = array(
        '#theme' => 'metatag',
        '#tag' => 'meta',
        '#value' => $value,
        '#type' => 'html_tag',
        '#id' => 'metatag_keywords',
        '#name' => 'keywords',
      );
    }
  }
  if (isset($metadata['metadata_robots'])) {
    $value = token_replace($metadata['metadata_robots']);
    if (!empty($value)) {
      $elements['metadata_robots'] = array(
        '#theme' => 'metatag',
        '#tag' => 'meta',
        '#value' => $value,
        '#type' => 'html_tag',
        '#id' => 'metadata_robots',
        '#name' => 'robots',
      );
    }
  }
  if (isset($metadata['metadata_canonical'])) {
    $value = token_replace($metadata['metadata_canonical']);
    if (!empty($value)) {
      $elements['metatag_canonical'] = array(
        '#theme' => 'metatag',
        '#tag' => 'meta',
        '#value' => $value,
        '#type' => 'html_tag',
        '#id' => 'metatag_canonical',
        '#name' => 'canonical',
      );
      foreach ($elements as $key => $element) {
        if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
          $elements[$key]['#attributes']['href'] = $value;
        }
        if ($key == 'metatag_canonical') {
          $elements[$key]['#value'] = $value;
        }
      }
    }
  }
}