diff --git a/core/modules/field/lib/Drupal/field/FieldInstanceInterface.php b/core/modules/field/lib/Drupal/field/FieldInstanceInterface.php new file mode 100644 index 0000000..38762a7 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/FieldInstanceInterface.php @@ -0,0 +1,17 @@ +uuid; + $this->field = field_info_field($values['field_name']); + if ($this->field) { + $values['field_uuid'] = $this->field->uuid; } else { throw new FieldException(format_string('Attempt to create an instance of unknown, disabled, or deleted field @name', array('@name' => $values['field_name']))); @@ -251,9 +259,9 @@ public function __construct(array $values, $entity_type = 'field_instance') { // @todo Revisit that in favor of a getField() method. // See http://drupal.org/node/1967106. elseif (isset($values['field_uuid']) && !isset($values['field_name'])) { - $field = current(field_read_fields(array('uuid' => $values['field_uuid']), array('include_inactive' => TRUE, 'include_deleted' => TRUE))); - if ($field) { - $values['field_name'] = $field->id; + $this->field = current(field_read_fields(array('uuid' => $values['field_uuid']), array('include_inactive' => TRUE, 'include_deleted' => TRUE))); + if ($this->field) { + $values['field_name'] = $this->field->id; } else { throw new FieldException(format_string('Attempt to create an instance of unknown field @uuid', array('@uuid' => $values['field_uuid']))); @@ -318,17 +326,14 @@ public function getExportProperties() { public function save() { $module_handler = \Drupal::moduleHandler(); $entity_manager = \Drupal::service('plugin.manager.entity'); - $field_controller = $entity_manager->getStorageController('field_entity'); $instance_controller = $entity_manager->getStorageController($this->entityType); - $field = current($field_controller->load(array($this->field_name))); - if ($this->isNew()) { - if (empty($field)) { + if (empty($this->field)) { throw new FieldException(format_string("Attempt to save an instance of a field @field_id that doesn't exist or is currently inactive.", array('@field_name' => $this->field_name))); } // Check that the field can be attached to this entity type. - if (!empty($field->entity_types) && !in_array($this->entity_type, $field->entity_types)) { + if (!empty($this->field->entity_types) && !in_array($this->entity_type, $this->field->entity_types)) { throw new FieldException(format_string('Attempt to create an instance of field @field_name on forbidden entity type @entity_type.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type))); } @@ -341,7 +346,7 @@ public function save() { } // Set the field UUID. - $this->field_uuid = $field->uuid; + $this->field_uuid = $this->field->uuid; $hook = 'field_create_instance'; $hook_args = array($this); @@ -367,7 +372,7 @@ public function save() { $hook_args = array($this, $original); } - $field_type_info = field_info_field_types($field->type); + $field_type_info = field_info_field_types($this->field->type); // Set the default instance settings. $this->settings += $field_type_info['instance_settings']; @@ -429,16 +434,15 @@ public function delete($field_cleanup = TRUE) { // Mark instance data for deletion by invoking // hook_field_storage_delete_instance(). - $field = field_info_field($this->field_name); - $module_handler->invoke($field->storage['module'], 'field_storage_delete_instance', array($this)); + $module_handler->invoke($this->field->storage['module'], 'field_storage_delete_instance', array($this)); // Let modules react to the deletion of the instance with // hook_field_delete_instance(). $module_handler->invokeAll('field_delete_instance', array($this)); // Delete the field itself if we just deleted its last instance. - if ($field_cleanup && count($field->getBundles()) == 0) { - $field->delete(); + if ($field_cleanup && count($this->field->getBundles()) == 0) { + $this->field->delete(); } } } @@ -524,5 +528,11 @@ public function offsetUnset($offset) { unset($this->{$offset}); } -} + /** + * {@inheritdoc} + */ + public function getField() { + return $this->field; + } +}