/** * {@inheritdoc} */ public function addField(FieldInterface $field) { $field_id = $field->getFieldIdentifier(); if (Utility::isFieldIdReserved($field_id)) { $args['%field_id'] = $field_id; throw new SearchApiException(new FormattableMarkup('%field_id is a reserved value and cannot be used as the machine name of a normal field.', $args)); } $old_field = $this->getField($field_id); if ($old_field && $old_field != $field) { $args['%field_id'] = $field_id; throw new SearchApiException(new FormattableMarkup('Cannot add field with machine name %field_id: machine name is already taken.', $args)); } $this->fieldInstances[$field_id] = $field; return $this; }
/** * {@inheritdoc} */ 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; }