addFieldDefinition example

$entity_type_id = $storage_definition->getTargetEntityTypeId();

    // @todo Forward this to all interested handlers, not only storage, once     // iterating handlers is possible: https://www.drupal.org/node/2332857.     $storage = $this->entityTypeManager->getStorage($entity_type_id);

    // Keep the field definition in the deleted fields repository so we can use     // it later during field_purge_batch(), but only if the field has data.     if ($storage_definition instanceof BaseFieldDefinition && $storage instanceof FieldableEntityStorageInterface && $storage->countFieldData($storage_definition, TRUE)) {
      $deleted_storage_definition = clone $storage_definition;
      $deleted_storage_definition->setDeleted(TRUE);
      $this->deletedFieldsRepository->addFieldDefinition($deleted_storage_definition);
      $this->deletedFieldsRepository->addFieldStorageDefinition($deleted_storage_definition);
    }

    if ($storage instanceof FieldStorageDefinitionListenerInterface) {
      $storage->onFieldStorageDefinitionDelete($storage_definition);
    }

    $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition);

    $this->eventDispatcher->dispatch(new FieldStorageDefinitionEvent($storage_definition), FieldStorageDefinitionEvents::DELETE);
    $this->entityFieldManager->clearCachedFieldDefinitions();
  }
// Keep the field definitions in the deleted fields repository so we can use     // them later during field_purge_batch().     /** @var \Drupal\field\FieldConfigInterface $field */
    foreach ($fields as $field) {
      // Only mark a field for purging if there is data. Otherwise, just remove       // it.       $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId());
      if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) {
        $field = clone $field;
        $field->deleted = TRUE;
        $field->fieldStorage = NULL;
        $deleted_fields_repository->addFieldDefinition($field);
      }
    }
  }

  /** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $fields) {
    parent::postDelete($storage$fields);
    // If this is part of a configuration synchronization then the following     // configuration updates are not necessary.
Home | Imprint | This part of the site doesn't use cookies.