social_content_translation.install in Open Social 8.7
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.8 modules/custom/social_language/modules/social_content_translation/social_content_translation.install
- 10.3.x 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.
*/
/**
* Implements hook_install().
*/
function 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.
*/
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::entityManager()
->clearCachedDefinitions();
\Drupal::service('router.builder')
->setRebuildNeeded();
}
Functions
Name | Description |
---|---|
social_content_translation_disable_field_translations | Disable field translations. |
social_content_translation_install | Implements hook_install(). |