You are here

public function SolrFieldType::setFieldTypeAsJson 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::setFieldTypeAsJson()
  2. 4.x src/Entity/SolrFieldType.php \Drupal\search_api_solr\Entity\SolrFieldType::setFieldTypeAsJson()

Sets the Solr Field Type definition as JSON.

Decodes the Solr Field Type definition encoded as JSON and stores an nested associative array internally. This method in useful to import a field type from an existing Solr server.

Parameters

string $field_type: The Solr Field Type definition as JSON.

Return value

$this

Overrides SolrFieldTypeInterface::setFieldTypeAsJson

File

src/Entity/SolrFieldType.php, line 206

Class

SolrFieldType
Defines the SolrFieldType entity.

Namespace

Drupal\search_api_solr\Entity

Code

public function setFieldTypeAsJson($field_type) {
  $field_type = $this->field_type = Json::decode($field_type);

  // Unfortunately the JSON encoded field type definition still uses the
  // element names "indexAnalyzer", "queryAnalyzer" and "multiTermAnalyzer"
  // which are deprecated in the XML format. Therefore we need to add some
  // conversion logic.
  foreach ([
    'index' => 'indexAnalyzer',
    'query' => 'queryAnalyzer',
    'multiterm' => 'multiTermAnalyzer',
    'analyzer' => 'analyzer',
  ] as $type => $analyzer) {
    if (!empty($field_type[$analyzer])) {
      unset($this->field_type[$analyzer]);
      if ($type != $analyzer) {
        $field_type[$analyzer]['type'] = $type;
      }
      $this->field_type['analyzers'][] = $field_type[$analyzer];
    }
  }
  return $this;
}