/** * Overrides \Drupal\Core\Entity\Entity::preSave(). * * @throws \Drupal\Core\Field\FieldException * If the field definition is invalid. * @throws \Drupal\Core\Entity\EntityStorageException * In case of failures at the configuration storage level. */ public function preSave(EntityStorageInterface $storage) { $entity_manager = \Drupal::entityManager(); $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); $storage_definition = $this->getFieldStorageDefinition(); // Filter out unknown settings and make sure all settings are present, so // that a complete field definition is passed to the various hooks and // written to config. $default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->getType()); $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings; if ($this->isNew()) { // Notify the entity storage. $entity_manager->onFieldDefinitionCreate($this); } else { // Some updates are always disallowed. if ($this->entity_type != $this->original->entity_type) { throw new FieldException("Cannot change an existing field's entity_type."); } if ($this->bundle != $this->original->bundle && empty($this->bundleRenameAllowed)) { throw new FieldException("Cannot change an existing field's bundle."); } if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) { throw new FieldException("Cannot change an existing field's storage."); } // Notify the entity storage. $entity_manager->onFieldDefinitionUpdate($this, $this->original); } parent::preSave($storage); }
/** * Overrides \Drupal\Core\Entity\Entity::preSave(). * * @throws \Drupal\Core\Field\FieldException * If the field definition is invalid. * @throws \Drupal\Core\Entity\EntityStorageException * In case of failures at the configuration storage level. */ public function preSave(EntityStorageInterface $storage) { $entity_manager = \Drupal::entityManager(); $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); $storage_definition = $this->getFieldStorageDefinition(); if ($this->isNew()) { // Set the default field settings. $this->settings += $field_type_manager->getDefaultFieldSettings($storage_definition->type); // Notify the entity storage. $entity_manager->getStorage($this->entity_type)->onFieldDefinitionCreate($this); } else { // Some updates are always disallowed. if ($this->entity_type != $this->original->entity_type) { throw new FieldException("Cannot change an existing field's entity_type."); } if ($this->bundle != $this->original->bundle && empty($this->bundleRenameAllowed)) { throw new FieldException("Cannot change an existing field's bundle."); } if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) { throw new FieldException("Cannot change an existing field's storage."); } // Set the default field settings. $this->settings += $field_type_manager->getDefaultFieldSettings($storage_definition->type); // Notify the entity storage. $entity_manager->getStorage($this->entity_type)->onFieldDefinitionUpdate($this, $this->original); } parent::preSave($storage); }
/** * {@inheritdoc} * * @throws \Drupal\Core\Field\FieldException * If the bundle is being changed and * BaseFieldOverride::allowBundleRename() has not been called. */ public function preSave(EntityStorageInterface $storage) { // Set the default instance settings. $this->settings += \Drupal::service('plugin.manager.field.field_type')->getDefaultFieldSettings($this->getType()); // Call the parent's presave method to perform validate and calculate // dependencies. parent::preSave($storage); if ($this->isNew()) { // @todo This assumes that the previous definition isn't some // non-config-based override, but that might not be the case: // https://www.drupal.org/node/2321071. $previous_definition = $this->getBaseFieldDefinition(); } else { // Some updates are always disallowed. if ($this->entity_type != $this->original->entity_type) { throw new FieldException(String::format('Cannot change the entity_type of an existing base field bundle override (entity type:@entity_type, bundle:@bundle, field name: @field_name)', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type, '@bundle' => $this->original->bundle))); } if ($this->bundle != $this->original->bundle && empty($this->bundleRenameAllowed)) { throw new FieldException(String::format('Cannot change the bundle of an existing base field bundle override (entity type:@entity_type, bundle:@bundle, field name: @field_name)', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type, '@bundle' => $this->original->bundle))); } $previous_definition = $this->original; } // Notify the entity storage. $this->entityManager()->getStorage($this->getTargetEntityTypeId())->onFieldDefinitionUpdate($this, $previous_definition); }
/** * {@inheritdoc} * * @throws \Drupal\Core\Field\FieldException * If the bundle is being changed and * BaseFieldOverride::allowBundleRename() has not been called. */ public function preSave(EntityStorageInterface $storage) { // Filter out unknown settings and make sure all settings are present, so // that a complete field definition is passed to the various hooks and // written to config. $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); $default_settings = $field_type_manager->getDefaultFieldSettings($this->getType()); $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings; // Call the parent's presave method to perform validate and calculate // dependencies. parent::preSave($storage); if ($this->isNew()) { // @todo This assumes that the previous definition isn't some // non-config-based override, but that might not be the case: // https://www.drupal.org/node/2321071. $previous_definition = $this->getBaseFieldDefinition(); } else { // Some updates are always disallowed. if ($this->entity_type != $this->original->entity_type) { throw new FieldException("Cannot change the entity_type of an existing base field bundle override (entity type:{$this->entity_type}, bundle:{$this->original->bundle}, field name: {$this->field_name})"); } if ($this->bundle != $this->original->bundle && empty($this->bundleRenameAllowed)) { throw new FieldException("Cannot change the bundle of an existing base field bundle override (entity type:{$this->entity_type}, bundle:{$this->original->bundle}, field name: {$this->field_name})"); } $previous_definition = $this->original; } // Notify the entity storage. $this->entityManager()->getStorage($this->getTargetEntityTypeId())->onFieldDefinitionUpdate($this, $previous_definition); }