function ContentTranslationHooks::entityBundleInfoAlter

Same name and namespace in other branches
  1. 11.x core/modules/content_translation/src/Hook/ContentTranslationHooks.php \Drupal\content_translation\Hook\ContentTranslationHooks::entityBundleInfoAlter()

Implements hook_entity_bundle_info_alter().

Attributes

#[Hook('entity_bundle_info_alter', order: Order::First)]

File

core/modules/content_translation/src/Hook/ContentTranslationHooks.php, line 225

Class

ContentTranslationHooks
Hook implementations for content_translation.

Namespace

Drupal\content_translation\Hook

Code

public function entityBundleInfoAlter(&$entity_type_bundles) : void {
  // Inline the logic from ContentTranslationManager::isEnabled() to avoid
  // having to load configuration entities one by one.
  $content_translation_manager = \Drupal::service('content_translation.manager');
  $entity_type_manager = \Drupal::entityTypeManager();
  $ids = [];
  foreach ($entity_type_bundles as $entity_type => $bundles) {
    if ($content_translation_manager->isSupported($entity_type)) {
      foreach ($bundles as $bundle => $bundle_info) {
        $ids[] = $entity_type . '.' . $bundle;
      }
    }
  }
  $language_content_settings = $entity_type_manager->getStorage('language_content_settings')
    ->loadMultiple($ids);
  foreach ($language_content_settings as $language_content_setting) {
    if ($language_content_setting->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
      $entity_type_id = $language_content_setting->get('target_entity_type_id');
      $bundle = $language_content_setting->get('target_bundle');
      $entity_type_bundles[$entity_type_id][$bundle]['translatable'] = TRUE;
      if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
        $settings = $content_translation_manager->getBundleTranslationSettings($entity_type_id, $bundle);
        $entity_type_bundles[$entity_type_id][$bundle]['untranslatable_fields.default_translation_affected'] = !empty($settings['untranslatable_fields_hide']);
      }
    }
  }
  foreach ($entity_type_bundles as $entity_type => $info) {
    foreach ($info as $bundle => $bundle_info) {
      if (!isset($bundle_info['translatable'])) {
        $entity_type_bundles[$entity_type][$bundle]['translatable'] = FALSE;
      }
    }
  }
  // Always hide untranslatable field widgets if pending revision support is
  // enabled otherwise changes in pending
  // revisions might be overridden by changes in later default revisions.
  // This can't use
  // Drupal\content_translation\ContentTranslationManager::isPendingRevisionSupportEnabled()
  // since that depends on entity bundle information to be completely built.
  if (\Drupal::moduleHandler()->moduleExists('content_moderation')) {
    foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
      /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
      $plugin = $workflow->getTypePlugin();
      foreach ($plugin->getEntityTypes() as $entity_type_id) {
        foreach ($plugin->getBundlesForEntityType($entity_type_id) as $bundle_id) {
          if (isset($entity_type_bundles[$entity_type_id][$bundle_id])) {
            $entity_type_bundles[$entity_type_id][$bundle_id]['untranslatable_fields.default_translation_affected'] = TRUE;
          }
        }
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.