public function Field::delete

Implements \Drupal\Core\Entity\EntityInterface::delete().

Overrides Entity::delete

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php, line 449
Contains \Drupal\field\Plugin\Core\Entity\Field.

Class

Field
Defines the Field entity.

Namespace

Drupal\field\Plugin\Core\Entity

Code

public function delete() {
  if (!$this->deleted) {
    $module_handler = \Drupal::moduleHandler();
    $instance_controller = \Drupal::entityManager()
      ->getStorageController('field_instance');
    $state = \Drupal::state();

    // Delete all non-deleted instances.
    $instance_ids = array();
    foreach ($this
      ->getBundles() as $entity_type => $bundles) {
      foreach ($bundles as $bundle) {
        $instance_ids[] = "{$entity_type}.{$bundle}.{$this->id}";
      }
    }
    foreach ($instance_controller
      ->load($instance_ids) as $instance) {

      // By default, FieldInstance::delete() will automatically try to delete
      // a field definition when it is deleting the last instance of the
      // field. Since the whole field is being deleted here, pass FALSE as
      // the $field_cleanup parameter to prevent a loop.
      $instance
        ->delete(FALSE);
    }

    // Mark field data for deletion by invoking
    // hook_field_storage_delete_field().
    $module_handler
      ->invoke($this->storage['module'], 'field_storage_delete_field', array(
      $this,
    ));

    // Delete the configuration of this field and save the field configuration
    // in the key_value table so we can use it later during
    // field_purge_batch(). This makes sure a new field can be created
    // immediately with the same name.
    $deleted_fields = $state
      ->get('field.field.deleted') ?: array();
    $config = $this
      ->getExportProperties();
    $config['deleted'] = TRUE;
    $deleted_fields[$this->uuid] = $config;
    $state
      ->set('field.field.deleted', $deleted_fields);
    parent::delete();

    // Clear the cache.
    field_cache_clear();

    // Invoke hook_field_delete_field().
    $module_handler
      ->invokeAll('field_delete_field', array(
      $this,
    ));
  }
}