Unverified Commit 46424a89 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2599228 by tstoeckler, Taran2L, poornachandran, catch, vacho, johan.s,...

Issue #2599228 by tstoeckler, Taran2L, poornachandran, catch, vacho, johan.s, Berdir, hchonov, osman, tim.plunkett, GoZ, Voidtek, neetu morwani, psf_, alexpott, dxvargas, nkoporec, plach, andy@andyhawks.com, joey-santiago: Programmatically created translatable content type returns SQL error on content creation
parent cbef6823
Loading
Loading
Loading
Loading
+52 −6
Changes for core/modules/content_translation/content_translation.module: 52 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\language\ContentLanguageSettingsInterface;

/**
 * Implements hook_help().
@@ -172,27 +173,72 @@ function content_translation_entity_type_alter(array &$entity_types) {
/**
 * Implements hook_ENTITY_TYPE_insert().
 *
 * Clear the bundle information cache so that the bundle's translatability will
 * be set properly.
 * Installs Content Translation's field storage definitions for the target
 * entity type, if required.
 *
 * Also clears the bundle information cache so that the bundle's translatability
 * will be set properly.
 *
 * @see content_translation_entity_bundle_info_alter()
 * @see \Drupal\content_translation\ContentTranslationManager::isEnabled()
 */
function content_translation_language_content_settings_insert(EntityInterface $entity) {
function content_translation_language_content_settings_insert(ContentLanguageSettingsInterface $settings) {
  if ($settings->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
    _content_translation_install_field_storage_definitions($settings->getTargetEntityTypeId());
  }

  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
}

/**
 * Implements hook_ENTITY_TYPE_update().
 *
 * Clear the bundle information cache so that the bundle's translatability will
 * be changed properly.
 * Installs Content Translation's field storage definitions for the target
 * entity type, if required.
 *
 * Also clears the bundle information cache so that the bundle's translatability
 * will be changed properly.
 *
 * @see content_translation_entity_bundle_info_alter()
 * @see \Drupal\content_translation\ContentTranslationManager::isEnabled()
 */
function content_translation_language_content_settings_update(EntityInterface $entity) {
function content_translation_language_content_settings_update(ContentLanguageSettingsInterface $settings) {
  $original_settings = $settings->original;
  if ($settings->getThirdPartySetting('content_translation', 'enabled', FALSE)
    && !$original_settings->getThirdPartySetting('content_translation', 'enabled', FALSE)
  ) {
    _content_translation_install_field_storage_definitions($settings->getTargetEntityTypeId());
  }
  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
}

/**
 * Installs Content Translation's fields for a given entity type.
 *
 * @param string $entity_type_id
 *   The entity type ID.
 *
 * @todo Generalize this code in https://www.drupal.org/node/2346013.
 */
function _content_translation_install_field_storage_definitions($entity_type_id) {
  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
  $field_manager = \Drupal::service('entity_field.manager');
  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */
  $schema_repository = \Drupal::service('entity.last_installed_schema.repository');
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  $field_manager->useCaches(FALSE);
  $storage_definitions = $field_manager->getFieldStorageDefinitions($entity_type_id);
  $field_manager->useCaches(TRUE);
  $installed_storage_definitions = $schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
  foreach (array_diff_key($storage_definitions, $installed_storage_definitions) as $storage_definition) {
    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition */
    if ($storage_definition->getProvider() == 'content_translation') {
      $definition_update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type_id, 'content_translation', $storage_definition);
    }
  }
}

/**
 * Implements hook_entity_bundle_info_alter().
 */
+2 −3
Changes for core/modules/content_translation/content_translation.services.yml: 2 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -29,10 +29,9 @@ services:

  content_translation.manager:
    class: Drupal\content_translation\ContentTranslationManager
    arguments: ['@entity_type.manager', '@content_translation.updates_manager', '@entity_type.bundle.info']
    arguments: ['@entity_type.manager', '@entity_type.bundle.info']

  content_translation.updates_manager:
    class: Drupal\content_translation\ContentTranslationUpdatesManager
    arguments: ['@entity_type.manager', '@entity.definition_update_manager', '@entity_field.manager', '@entity.last_installed_schema.repository']
    tags:
      - { name: event_subscriber }
    deprecated: The "%service_id%" service is deprecated. Definitions are updated automatically now so no replacement is needed. See https://www.drupal.org/node/2973222.
+0 −5
Changes for core/modules/content_translation/migrations/d7_entity_translation_settings.yml: 0 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -25,11 +25,6 @@ process:
  third_party_settings/content_translation/bundle_settings/untranslatable_fields_hide: untranslatable_fields_hide
destination:
  plugin: entity:language_content_settings
  content_translation_update_definitions:
    - comment
    - node
    - taxonomy_term
    - user
migration_dependencies:
  optional:
    - d7_comment_type
+0 −2
Changes for core/modules/content_translation/migrations/d7_node_translation.yml: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -33,8 +33,6 @@ process:
destination:
  plugin: entity:node
  translations: true
  content_translation_update_definitions:
    - node
  destination_module: content_translation
migration_dependencies:
  required:
+8 −16
Changes for core/modules/content_translation/src/ContentTranslationManager.php: 8 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -17,7 +17,10 @@ class ContentTranslationManager implements ContentTranslationManagerInterface, B
  /**
   * {@inheritdoc}
   */
  protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  protected $deprecatedProperties = [
    'entityManager' => 'entity.manager',
    'updatesManager' => 'content_translation.updates_manager',
  ];

  /**
   * The entity type bundle info provider.
@@ -33,28 +36,19 @@ class ContentTranslationManager implements ContentTranslationManagerInterface, B
   */
  protected $entityTypeManager;

  /**
   * The updates manager.
   *
   * @var \Drupal\content_translation\ContentTranslationUpdatesManager
   */
  protected $updatesManager;

  /**
   * Constructs a ContentTranslationManageAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\content_translation\ContentTranslationUpdatesManager $updates_manager
   *   The updates manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info provider.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentTranslationUpdatesManager $updates_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_bundle_info) {
    $this->entityTypeManager = $entity_type_manager;
    $this->updatesManager = $updates_manager;
    if (!$entity_type_bundle_info) {
      @trigger_error('The entity_type.bundle.info service must be passed to ContentTranslationManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);

    if (!($entity_type_bundle_info instanceof EntityTypeBundleInfoInterface)) {
      @trigger_error('The entity_type.bundle.info service should be passed to ContentTranslationManager::__construct() instead of the content_translation.updates_manager service since 8.7.0. This will be required in Drupal 9.0.0. See https://www.drupal.org/node/2549139 and https://www.drupal.org/node/2973222.', E_USER_DEPRECATED);
      $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
    }
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
@@ -104,8 +98,6 @@ public function getSupportedEntityTypes() {
  public function setEnabled($entity_type_id, $bundle, $value) {
    $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
    $config->setThirdPartySetting('content_translation', 'enabled', $value)->save();
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
    $this->updatesManager->updateDefinitions([$entity_type_id => $entity_type]);
  }

  /**
Loading