You are here

public function SolrFieldType::getFieldTypeAsJson in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 src/Entity/SolrFieldType.php \Drupal\search_api_solr\Entity\SolrFieldType::getFieldTypeAsJson()
  2. 4.x src/Entity/SolrFieldType.php \Drupal\search_api_solr\Entity\SolrFieldType::getFieldTypeAsJson()

Gets the Solr Field Type definition as JSON.

The JSON format is used to interact with a managed Solr schema.

Parameters

bool $pretty: Return pretty printed JSON.

Return value

string The Solr Field Type definition as JSON.

Overrides SolrFieldTypeInterface::getFieldTypeAsJson

File

src/Entity/SolrFieldType.php, line 177

Class

SolrFieldType
Defines the SolrFieldType entity.

Namespace

Drupal\search_api_solr\Entity

Code

public function getFieldTypeAsJson(bool $pretty = FALSE) {

  // Unfortunately the JSON encoded field type definition still uses the
  // element names "indexAnalyzer", "queryAnalyzer" and "multiTermAnalyzer"
  // which are deprecated in the XML format. Therefor we need to add some
  // conversion logic.
  $field_type = $this->field_type;
  unset($field_type['analyzers']);
  foreach ($this->field_type['analyzers'] as $analyzer) {
    $type = 'analyzer';
    if (!empty($analyzer['type'])) {
      if ('multiterm' == $analyzer['type']) {
        $type = 'multiTermAnalyzer';
      }
      else {
        $type = $analyzer['type'] . 'Analyzer';
      }
      unset($analyzer['type']);
    }
    $field_type[$type] = $analyzer;
  }
  return $pretty ? json_encode($field_type, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) : Json::encode($field_type);
}