public function Index::preSave in Search API 8
Acts on an entity before the presave hook is invoked.
Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically. @todo Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.
Parameters
\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.
Throws
\Exception When there is a problem that should prevent saving the entity.
Overrides ConfigEntityBase::preSave
See also
\Drupal\Core\Field\FieldItemListInterface::preSave()
File
- src/
Entity/ Index.php, line 1237
Class
- Index
- Defines the search index configuration entity.
Namespace
Drupal\search_api\EntityCode
public function preSave(EntityStorageInterface $storage) {
// If we are in the process of syncing, or in the process of installing
// configuration from an extension, we shouldn't change any entity
// properties (or other configuration).
if ($this
->isSyncing() || $this
->isInstallingFromExtension()) {
parent::preSave($storage);
return;
}
// Retrieve active config overrides for this index.
$overrides = Utility::getConfigOverrides($this);
// Prevent enabling of indexes when the server is disabled. Take into
// account that both the index's "status" and "server" properties might be
// overridden.
if ($this
->status() && !isset($overrides['status'])) {
// NULL would be a valid override, so we can't use isset() here.
if (!array_key_exists('server', $overrides)) {
if (!$this
->isServerEnabled()) {
$this
->disable();
}
}
else {
$server_id = $overrides['server'];
$server = $server_id !== NULL ? Server::load($server_id) : NULL;
if (!$server || !$server
->status()) {
$this
->disable();
}
}
}
// Merge in default options.
$config = \Drupal::config('search_api.settings');
$this->options += [
'cron_limit' => $config
->get('default_cron_limit'),
'index_directly' => TRUE,
];
// Reset the static cache for getPropertyDefinitions() to make sure we don't
// remove any fields just because of caching problems.
$this->properties = [];
foreach ($this
->getFields() as $field_id => $field) {
// Remove all "locked" and "hidden" flags from all fields of the index. If
// they are still valid, they should be re-added by the processors.
$field
->setIndexedLocked(FALSE);
$field
->setTypeLocked(FALSE);
$field
->setHidden(FALSE);
// Also check whether the underlying property actually (still) exists.
$datasource_id = $field
->getDatasourceId();
$property = NULL;
if ($datasource_id === NULL || $this
->isValidDatasource($datasource_id)) {
$properties = $this
->getPropertyDefinitions($datasource_id);
$property = \Drupal::getContainer()
->get('search_api.fields_helper')
->retrieveNestedProperty($properties, $field
->getPropertyPath());
}
if (!$property) {
$this
->removeField($field_id);
}
}
// Check whether all enabled processors actually still support this index.
// (Since we can't remove processors which are present in overrides anyways,
// we don't need to take overrides into account here.)
foreach ($this
->getProcessors() as $processor_id => $processor) {
if (!$processor
->supportsIndex($this)) {
$this
->removeProcessor($processor_id);
}
}
// Call the preIndexSave() method of all applicable processors.
$processor_overrides = !empty($overrides['processor_settings']) ? $overrides['processor_settings'] : [];
foreach ($this
->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE, $processor_overrides) as $processor) {
$processor
->preIndexSave();
}
// Write the field and plugin settings to the persistent *_settings
// properties.
$this
->writeChangesToSettings();
// Since we change dependency-relevant data in this method, we can only call
// the parent method at the end (or we'd need to re-calculate the
// dependencies).
parent::preSave($storage);
}