/** * Form submission handler for adding a new field to the index. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ public function addField(array $form, FormStateInterface $form_state) { $button = $form_state->getTriggeringElement(); if (!$button) { return; } /** @var \Drupal\Core\TypedData\DataDefinitionInterface $property */ $property = $button['#property']; list($datasource_id, $property_path) = Utility::splitCombinedId($button['#name']); $field = Utility::createFieldFromProperty($this->entity, $property, $datasource_id, $property_path, NULL, $button['#data_type']); $field->setLabel($button['#prefixed_label']); $this->entity->addField($field); $args['%label'] = $field->getLabel(); drupal_set_message($this->t('Field %label was added to the index.', $args)); }
/** * Ensures that a field with certain properties is indexed on the index. * * Can be used as a helper method in preIndexSave(). * * @param string|null $datasource_id * The ID of the field's datasource, or NULL for a datasource-independent * field. * @param string $property_path * The field's property path on the datasource. * @param string|null $type * (optional) If set, the field should have this type. * * @return \Drupal\search_api\Item\FieldInterface * A field on the index, possibly newly added, with the specified * properties. * * @throws \Drupal\search_api\SearchApiException * Thrown if there is no property with the specified path, or no type is * given and no default could be determined for the property. */ protected function ensureField($datasource_id, $property_path, $type = NULL) { $field = $this->findField($datasource_id, $property_path, $type); if (!$field) { $property = Utility::retrieveNestedProperty($this->index->getPropertyDefinitions($datasource_id), $property_path); if (!$property) { $args['%property'] = Utility::createCombinedId($datasource_id, $property_path); $args['%processor'] = $this->label(); $message = new FormattableMarkup('Could not find property %property which is required by the %processor processor.', $args); throw new SearchApiException($message); } $field = Utility::createFieldFromProperty($this->index, $property, $datasource_id, $property_path, NULL, $type); } $field->setIndexedLocked(); if (isset($type)) { $field->setTypeLocked(); } $this->index->addField($field); return $field; }