You are here

function social_content_translation_disable_field_translations in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  2. 8.4 modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  3. 8.5 modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  4. 8.6 modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  5. 8.7 modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  6. 8.8 modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  7. 10.3.x modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  8. 10.0.x modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()
  9. 10.1.x modules/custom/social_language/modules/social_content_translation/social_content_translation.install \social_content_translation_disable_field_translations()

Disable field translations.

Disable content translation for all fields in nodes, paragraphs and taxonomy terms. They are enabled by default when turning on content translation but we want to control this to only the fields that are in ContentTranslationDefaultsConfigOverride classes.

1 call to social_content_translation_disable_field_translations()
social_content_translation_install in modules/custom/social_language/modules/social_content_translation/social_content_translation.install
Implements hook_install().

File

modules/custom/social_language/modules/social_content_translation/social_content_translation.install, line 23
Installation tasks for the Social Language Content Translation module.

Code

function social_content_translation_disable_field_translations() {
  $entity_types = [
    'node',
    'paragraph',
    'taxonomy_term',
  ];

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
  $field_manager = \Drupal::service('entity_field.manager');

  /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info */
  $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
  $all_bundles = $entity_type_bundle_info
    ->getAllBundleInfo();

  // Only disable the listed entity types.
  $bundles = array_filter($all_bundles, function ($k) use ($entity_types) {
    return in_array($k, $entity_types);
  }, ARRAY_FILTER_USE_KEY);
  foreach ($bundles as $entity_type_id => $bundle_info) {
    $definition = \Drupal::entityTypeManager()
      ->getDefinition($entity_type_id);

    // Fields that must remain translatable for content translation to work
    // properly.
    $exceptions = [
      $definition
        ->getKey('default_langcode'),
      $definition
        ->getKey('langcode'),
      $definition
        ->getKey('status'),
      $definition
        ->getKey('uid'),
      $definition
        ->getKey('content_translation_source'),
      $definition
        ->getKey('content_translation_outdated'),
      $definition
        ->getKey('revision_translation_affected'),
    ];
    foreach (array_keys($bundle_info) as $bundle) {
      $fields = $field_manager
        ->getFieldDefinitions($entity_type_id, $bundle);
      foreach ($fields as $field) {
        $field_config = $field
          ->getConfig($bundle);

        // Only disable translatable fields and always allow some exceptions
        // that are required for content translation to function properly.
        if ($field_config
          ->isTranslatable() && !in_array($field
          ->getName(), $exceptions)) {
          $field_config
            ->setTranslatable(FALSE)
            ->save();
        }
      }
    }
  }

  // Ensure entity and menu router information are correctly rebuilt.
  \Drupal::service('entity_type.manager')
    ->clearCachedDefinitions();
  \Drupal::service('router.builder')
    ->setRebuildNeeded();
}