You are here

public function SchemaMetaTagTag::getElement in Schema.org Metatag 7

Get the HTML tag for this meta tag.

Return value

array A render array for this meta tag.

Overrides DrupalDefaultMetaTag::getElement

File

src/SchemaMetatagTag.php, line 156
Metatag integration for the Schema.org Metatag module.

Class

SchemaMetaTagTag
Text-based meta tag controller.

Code

public function getElement(array $options = array()) {
  $value = $this
    ->getValue($options);
  if (strlen($value) === 0) {
    return array();
  }

  // The stack of elements that will be output.
  $elements = array();

  // Dynamically add each option to this setting.
  $base_element = isset($this->info['element']) ? $this->info['element'] : array();

  // Single item.
  if (empty($this->info['multiple'])) {
    $values = array(
      $value,
    );
  }
  else {
    $values = array_filter(explode(',', $value));
  }

  // Loop over each item.
  if (!empty($values)) {
    foreach ($values as $ctr => $value) {
      $value = trim($value);

      // Some meta tags must be output as secure URLs.
      if (!empty($this->info['secure'])) {
        $value = str_replace('http://', 'https://', $value);
      }

      // Combine the base configuration for this meta tag with the value.
      $id = 'schema_metatag_' . $this->info['name'] . '_' . $ctr;
      $parts = explode('.', $this->info['name']);
      $element = $base_element + array(
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#id' => $id,
        '#attributes' => [
          'schema_metatag' => TRUE,
          'group' => $parts[0],
          'name' => $parts[1],
          'content' => $value,
        ],
        '#weight' => $this->info['weight'],
      );
      $elements[] = array(
        $element,
        $id,
      );
    }
  }
  if (!empty($elements)) {
    return array(
      '#attached' => array(
        'drupal_add_html_head' => $elements,
      ),
    );
  }
}