You are here

public function Item::setField in Search API 8

Sets one of the item's fields.

Parameters

string $field_id: The field's identifier.

\Drupal\search_api\Item\FieldInterface|null $field: (optional) The information and contents of this field. Or NULL to remove the field from the item.

Return value

$this

Throws

\InvalidArgumentException Thrown if a $field is passed but has another field identifier than given as $field_id.

Overrides ItemInterface::setField

File

src/Item/Item.php, line 301

Class

Item
Provides a default implementation for a search item.

Namespace

Drupal\search_api\Item

Code

public function setField($field_id, FieldInterface $field = NULL) {
  if ($field) {
    if ($field
      ->getFieldIdentifier() !== $field_id) {
      throw new \InvalidArgumentException('The field identifier passed must be consistent with the identifier set on the field object.');
    }

    // Make sure that the field has the same index object set as we. This
    // might otherwise cause impossibly hard-to-detect bugs.
    $field
      ->setIndex($this->index);
    $this->fields[$field_id] = $field;
  }
  else {
    unset($this->fields[$field_id]);
  }
  return $this;
}