Exemplo n.º 1
0
 /**
  * Creates a search item object by wrapping an existing complex data object.
  *
  * @param \Drupal\search_api\IndexInterface $index
  *   The item's search index.
  * @param \Drupal\Core\TypedData\ComplexDataInterface $original_object
  *   The original object to wrap.
  * @param 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.
  * @param \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 \Drupal\search_api\Item\ItemInterface
  *   A search item with the given values.
  *
  * @throws \InvalidArgumentException
  *   Thrown if both $datasource and $id are NULL.
  */
 public static function createItemFromObject(IndexInterface $index, ComplexDataInterface $original_object, $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 = self::createCombinedId($datasource->getPluginId(), $datasource->getItemId($original_object));
     }
     $item = static::createItem($index, $id, $datasource);
     $item->setOriginalObject($original_object);
     return $item;
 }