You are here

public static function SolrField::createFromField in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/DataType/SolrField.php \Drupal\search_api_solr\Plugin\DataType\SolrField::createFromField()
  2. 4.x src/Plugin/DataType/SolrField.php \Drupal\search_api_solr\Plugin\DataType\SolrField::createFromField()

Creates an instance wrapping the given Field.

Parameters

\Drupal\search_api\Item\FieldInterface $field: The Field object to wrap.

string $name: The name of the wrapped field.

\Drupal\Core\TypedData\TypedDataInterface $parent: The parent object of the wrapped field, which should be a Solr document.

Return value

static

File

src/Plugin/DataType/SolrField.php, line 43

Class

SolrField
Defines the "Solr field" data type.

Namespace

Drupal\search_api_solr\Plugin\DataType

Code

public static function createFromField(FieldInterface $field, $name, TypedDataInterface $parent) {

  // Get the Solr field definition from the SolrFieldManager.

  /** @var \Drupal\search_api_solr\SolrFieldManagerInterface $field_manager */
  $field_manager = \Drupal::getContainer()
    ->get('solr_field.manager');
  $server_id = $field
    ->getIndex()
    ->getServerInstance()
    ->id();
  $field_id = $field
    ->getPropertyPath();
  $definition = $field_manager
    ->getFieldDefinitions($server_id)[$field_id];
  $instance = new static($definition, $name, $parent);
  $instance
    ->setValue($field
    ->getValues());
  return $instance;
}