social_content_translation.install in Open Social 10.3.x
Same filename and directory in other branches
- 8.9 modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 8.4 modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 8.5 modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 8.6 modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 8.7 modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 8.8 modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 10.0.x modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 10.1.x modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 - 10.2.x modules/custom/social_language/modules/social_content_translation/social_content_translation.install
 
Installation tasks for the Social Language Content Translation module.
File
modules/custom/social_language/modules/social_content_translation/social_content_translation.installView source
<?php
/**
 * @file
 * Installation tasks for the Social Language Content Translation module.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\language\Entity\ContentLanguageSettings;
/**
 * Implements hook_install().
 */
function social_content_translation_install() {
  social_content_translation_install_language_settings_for_entities();
}
/**
 * Implements hook_uninstall().
 */
function social_content_translation_uninstall() {
  \Drupal::configFactory()
    ->getEditable('social_content_translation.settings')
    ->delete();
}
/**
 * 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.
 *
 * @deprecated
 *   The function is deprecated and will be removed as redundant.
 * @see https://github.com/goalgorilla/open_social/pull/2434
 */
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();
}
/**
 * Install translation configuration.
 *
 *  "Content Translation" module provide UI where we can enabled/disable
 *  translations for entity types we want. So here we enabling translations
 *  for modules if it have "language.content_settings*" configuration in
 *  "config/optional" folder.
 */
function social_content_translation_install_language_settings_for_entities() {
  $config_storage = \Drupal::service('config.storage');
  // Get enabled modules.
  $modules = \Drupal::moduleHandler()
    ->getModuleList();
  foreach ($modules as $module_name => $extension) {
    // Check if modules have target configuration.
    $config_path = drupal_get_path('module', $module_name) . '/config/optional';
    $source = new FileStorage($config_path);
    if (!($list = $source
      ->listAll('language.content_settings'))) {
      // Do nothing.
      continue;
    }
    foreach ($list as $config_name) {
      // Probably, configuration can be already existed if module provided
      // it was enabled after "Content Translation" module was enabled.
      // Otherwise lets install it.
      if (!$config_storage
        ->exists($config_name)) {
        $file = $source
          ->read($config_name);
        // We create a config as an entity because it allows to update field
        // definitions for translated fields.
        // @see content_translation_language_content_settings_insert()
        $config = ContentLanguageSettings::create($file);
        $config
          ->save();
      }
    }
  }
  \Drupal::service('entity_type.bundle.info')
    ->clearCachedBundles();
  \Drupal::service('router.builder')
    ->setRebuildNeeded();
}
/**
 * Add support translations for entities.
 */
function social_content_translation_update_8001() {
  $module_names = [
    'social_group_flexible_group',
  ];
  $config = \Drupal::configFactory()
    ->getEditable('social_content_translation.settings');
  foreach ($module_names as $module_name) {
    $config
      ->set($module_name, TRUE);
    $config
      ->save(TRUE);
  }
}
/**
 * Add translations compatibility for Topics.
 */
function social_content_translation_update_10301() {
  $module_names = [
    'social_topic',
  ];
  $config = \Drupal::configFactory()
    ->getEditable('social_content_translation.settings');
  foreach ($module_names as $module_name) {
    $config
      ->set($module_name, TRUE);
    $config
      ->save(TRUE);
  }
}Functions
| 
            Name | 
                  Description | 
|---|---|
| social_content_translation_disable_field_translations Deprecated | Disable field translations. | 
| social_content_translation_install | Implements hook_install(). | 
| social_content_translation_install_language_settings_for_entities | Install translation configuration. | 
| social_content_translation_uninstall | Implements hook_uninstall(). | 
| social_content_translation_update_10301 | Add translations compatibility for Topics. | 
| social_content_translation_update_8001 | Add support translations for entities. |