You are here

public function FieldsHelper::createItemFromObject in Search API 8

Creates a search item object by wrapping an existing complex data object.

Parameters

\Drupal\search_api\IndexInterface $index: The item's search index.

\Drupal\Core\TypedData\ComplexDataInterface $originalObject: The original object to wrap.

string $id: (optional) The item's (combined) ID. If not set, it will be determined with the \Drupal\search_api\Datasource\DatasourceInterface::getItemId() method of $datasource. In this case, $datasource must not be NULL.

\Drupal\search_api\Datasource\DatasourceInterface|null $datasource: (optional) The datasource of the item. If not set, it will be determined from the ID and loaded from the index if needed.

Return value

\Drupal\search_api\Item\ItemInterface A search item with the given values.

Throws

\InvalidArgumentException Thrown if both $datasource and $id are NULL.

Overrides FieldsHelperInterface::createItemFromObject

File

src/Utility/FieldsHelper.php, line 415

Class

FieldsHelper
Provides helper methods for dealing with Search API fields and properties.

Namespace

Drupal\search_api\Utility

Code

public function createItemFromObject(IndexInterface $index, ComplexDataInterface $originalObject, $id = NULL, DatasourceInterface $datasource = NULL) {
  if (!isset($id)) {
    if (!isset($datasource)) {
      throw new \InvalidArgumentException('Need either an item ID or the datasource to create a search item from an object.');
    }
    $id = Utility::createCombinedId($datasource
      ->getPluginId(), $datasource
      ->getItemId($originalObject));
  }
  $item = $this
    ->createItem($index, $id, $datasource);
  $item
    ->setOriginalObject($originalObject);
  return $item;
}