You are here

public static function XmlSitemapWriter::formatXmlElements in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 src/XmlSitemapWriter.php \Drupal\xmlsitemap\XmlSitemapWriter::formatXmlElements()

Copy of Drupal 7's format_xml_elements() function.

The extra whitespace has been removed.

Parameters

array $array: An array where each item represents an element and is either a:

  • (key => value) pair (<key>value</key>)
  • Associative array with fields:

In both cases, 'value' can be a simple string, or it can be another array with the same format as $array itself for nesting.

Return value

string The XML output.

1 call to XmlSitemapWriter::formatXmlElements()
XmlSitemapWriter::writeElement in src/XmlSitemapWriter.php
Writes full element tag including support for nested elements.

File

src/XmlSitemapWriter.php, line 286

Class

XmlSitemapWriter
Extended class for writing XML sitemap files.

Namespace

Drupal\xmlsitemap

Code

public static function formatXmlElements(array $array) {
  $output = '';
  foreach ($array as $key => $value) {
    if (is_numeric($key)) {
      if ($value['key']) {
        $output .= '<' . $value['key'];
        if (isset($value['attributes'])) {
          if (is_array($value['attributes'])) {
            $value['attributes'] = new Attribute($value['attributes']);
          }
          $output .= static::toString($value['attributes']);
        }
        if (isset($value['value']) && $value['value'] != '') {
          $output .= '>' . (is_array($value['value']) ? static::formatXmlElements($value['value']) : Html::escape(static::toString($value['value']))) . '</' . $value['key'] . '>';
        }
        else {
          $output .= ' />';
        }
      }
    }
    else {
      $output .= '<' . $key . '>' . (is_array($value) ? static::formatXmlElements($value) : Html::escape(static::toString($value))) . "</{$key}>";
    }
  }
  return $output;
}