You are here

public function SolrFieldType::getFieldTypeAsXml in Search API Multilingual Solr Search 8

Gets the Solr Field Type definition as XML fragment.

The XML format is used as part of a classic Solr schema.

Parameters

bool $add_commment: Wether to add a comment to the XML or not to explain the purpose of this Solr Field Type.

Return value

string The Solr Field Type definition as XML.

Overrides SolrFieldTypeInterface::getFieldTypeAsXml

File

src/Entity/SolrFieldType.php, line 200

Class

SolrFieldType
Defines the SolrFieldType entity.

Namespace

Drupal\search_api_solr_multilingual\Entity

Code

public function getFieldTypeAsXml($add_commment = TRUE) {
  $root = new \SimpleXMLElement('<fieldType/>');
  $f = function (\SimpleXMLElement $element, array $attributes) use (&$f) {
    foreach ($attributes as $key => $value) {
      if (is_scalar($value)) {
        $element
          ->addAttribute($key, $value);
      }
      elseif (is_array($value)) {
        if (array_key_exists(0, $value)) {
          $key = rtrim($key, 's');
          foreach ($value as $inner_attributes) {
            $child = $element
              ->addChild($key);
            $f($child, $inner_attributes);
          }
        }
        else {
          $child = $element
            ->addChild($key);
          $f($child, $value);
        }
      }
    }
  };
  $f($root, $this->field_type);
  $comment = '';
  if ($add_commment) {
    $comment = "<!--\n  " . $this
      ->label() . "\n  " . ($this
      ->isManagedSchema() ? " for managed schema\n  " : '') . $this
      ->getMinimumSolrVersion() . "\n-->\n";
  }

  // Create formatted string.
  $dom = dom_import_simplexml($root)->ownerDocument;
  $dom->formatOutput = TRUE;
  $formatted_xml_string = $dom
    ->saveXML();

  // Remove the XML declaration before returning the XML fragment.
  return $comment . preg_replace('/<\\?.*?\\?>\\s*\\n?/', '', $formatted_xml_string);
}