public function FieldsHelper::createFieldFromProperty in Search API 8
Creates a new field on an index based on a property.
Will find and set a new unique field identifier for the field on the index.
Parameters
\Drupal\search_api\IndexInterface $index: The search index.
\Drupal\Core\TypedData\DataDefinitionInterface $property: The data definition of the property.
string|null $datasourceId: The ID of the index's datasource this property belongs to, or NULL if it is a datasource-independent property.
string $propertyPath: The property's property path within the property structure of the datasource.
string|null $fieldId: (optional) The identifier to use for the field. If not set, a new unique field identifier on the index will be chosen automatically.
string|null $type: (optional) The type to set for the field, or NULL to determine a default type automatically.
Return value
\Drupal\search_api\Item\FieldInterface A new field object for the index, based on the given property.
Throws
\Drupal\search_api\SearchApiException Thrown if no type was given and no default could be determined.
Overrides FieldsHelperInterface::createFieldFromProperty
File
- src/
Utility/ FieldsHelper.php, line 446
Class
- FieldsHelper
- Provides helper methods for dealing with Search API fields and properties.
Namespace
Drupal\search_api\UtilityCode
public function createFieldFromProperty(IndexInterface $index, DataDefinitionInterface $property, $datasourceId, $propertyPath, $fieldId = NULL, $type = NULL) {
$fieldId = $fieldId ?? $this
->getNewFieldId($index, $propertyPath);
if (!isset($type)) {
$typeMapping = $this->dataTypeHelper
->getFieldTypeMapping();
$propertyType = $property
->getDataType();
if (isset($typeMapping[$propertyType])) {
$type = $typeMapping[$propertyType];
}
else {
$propertyName = $property
->getLabel();
throw new SearchApiException("No default data type mapping could be found for property '{$propertyName}' ({$propertyPath}) of type '{$propertyType}'.");
}
}
$fieldInfo = [
'label' => $property
->getLabel(),
'datasource_id' => $datasourceId,
'property_path' => $propertyPath,
'type' => $type,
'data_definition' => $property,
];
if ($property instanceof ConfigurablePropertyInterface) {
$fieldInfo['configuration'] = $property
->defaultConfiguration();
}
return $this
->createField($index, $fieldId, $fieldInfo);
}