getDefinition($field->getType()); $column_groups = $definition['column_groups']; if (!empty($column_groups) && count($column_groups) > 1) { $options = []; $default = []; $require_all_groups_for_translation = []; foreach ($column_groups as $group => $info) { $options[$group] = $info['label']; $default[$group] = !empty($info['translatable']) ? $group : FALSE; if (!empty($info['require_all_groups_for_translation'])) { $require_all_groups_for_translation[] = $group; } } $default = $field->getThirdPartySetting('content_translation', 'translation_sync', $default); $element = [ '#type' => 'checkboxes', '#title' => t('Translatable elements'), '#options' => $options, '#default_value' => $default, ]; if ($require_all_groups_for_translation) { // The actual checkboxes are sometimes rendered separately and the parent // element is ignored. Attach to the first option to ensure that this // does not get lost. $element[key($options)]['#attached']['drupalSettings']['contentTranslationDependentOptions'] = [ 'dependent_selectors' => [ $element_name => $require_all_groups_for_translation, ], ]; $element[key($options)]['#attached']['library'][] = 'content_translation/drupal.content_translation.admin'; } } return $element; } /** * (proxied) Implements hook_form_FORM_ID_alter(). */ function _content_translation_form_language_content_settings_form_alter(array &$form, FormStateInterface $form_state) { // Inject into the content language settings the translation settings if the // user has the required permission. if (!\Drupal::currentUser()->hasPermission('administer content translation')) { return; } /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */ $content_translation_manager = \Drupal::service('content_translation.manager'); $default = $form['entity_types']['#default_value']; foreach ($default as $entity_type_id => $enabled) { $default[$entity_type_id] = $enabled || $content_translation_manager->isEnabled($entity_type_id) ? $entity_type_id : FALSE; } $form['entity_types']['#default_value'] = $default; $form['#attached']['library'][] = 'content_translation/drupal.content_translation.admin'; $entity_type_manager = \Drupal::entityTypeManager(); /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ $entity_field_manager = \Drupal::service('entity_field.manager'); $bundle_info_service = \Drupal::service('entity_type.bundle.info'); foreach ($form['#labels'] as $entity_type_id => $label) { $entity_type = $entity_type_manager->getDefinition($entity_type_id); $storage_definitions = $entity_type instanceof ContentEntityTypeInterface ? $entity_field_manager->getFieldStorageDefinitions($entity_type_id) : []; $entity_type_translatable = $content_translation_manager->isSupported($entity_type_id); foreach ($bundle_info_service->getBundleInfo($entity_type_id) as $bundle => $bundle_info) { // Here we do not want the widget to be altered and hold also the "Enable // translation" checkbox, which would be redundant. Hence we add this key // to be able to skip alterations. Alter the title and display the message // about UI integration. $form['settings'][$entity_type_id][$bundle]['settings']['language']['#content_translation_skip_alter'] = TRUE; if (!$entity_type_translatable) { $form['settings'][$entity_type_id]['#title'] = t('@label (Translation is not supported).', ['@label' => $entity_type->getLabel()]); continue; } // Displayed the "shared fields widgets" toggle. if ($content_translation_manager instanceof BundleTranslationSettingsInterface) { $settings = $content_translation_manager->getBundleTranslationSettings($entity_type_id, $bundle); $force_hidden = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $bundle); $form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['untranslatable_fields_hide'] = [ '#type' => 'checkbox', '#title' => t('Hide non translatable fields on translation forms'), '#default_value' => $force_hidden || !empty($settings['untranslatable_fields_hide']), '#disabled' => $force_hidden, '#description' => $force_hidden ? t('Moderated content requires non-translatable fields to be edited in the original language form.') : '', '#states' => [ 'visible' => [ ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [ 'checked' => TRUE, ], ], ], ]; } $fields = $entity_field_manager->getFieldDefinitions($entity_type_id, $bundle); if ($fields) { foreach ($fields as $field_name => $definition) { if ($definition->isComputed() || (!empty($storage_definitions[$field_name]) && _content_translation_is_field_translatability_configurable($entity_type, $storage_definitions[$field_name]))) { $form['settings'][$entity_type_id][$bundle]['fields'][$field_name] = [ '#label' => $definition->getLabel(), '#type' => 'checkbox', '#default_value' => $definition->isTranslatable(), ]; // Display the column translatability configuration widget. $column_element = content_translation_field_sync_widget($definition, "settings[{$entity_type_id}][{$bundle}][columns][{$field_name}]"); if ($column_element) { $form['settings'][$entity_type_id][$bundle]['columns'][$field_name] = $column_element; } } } if (!empty($form['settings'][$entity_type_id][$bundle]['fields'])) { // Only show the checkbox to enable translation if the bundles in the // entity might have fields and if there are fields to translate. $form['settings'][$entity_type_id][$bundle]['translatable'] = [ '#type' => 'checkbox', '#default_value' => $content_translation_manager->isEnabled($entity_type_id, $bundle), ]; } } } } $form['#validate'][] = 'content_translation_form_language_content_settings_validate'; $form['#submit'][] = 'content_translation_form_language_content_settings_submit'; } /** * Checks whether translatability should be configurable for a field. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition * The field storage definition. * * @return bool * TRUE if field translatability can be configured, FALSE otherwise. * * @internal */ function _content_translation_is_field_translatability_configurable(EntityTypeInterface $entity_type, FieldStorageDefinitionInterface $definition) { // Allow to configure only fields supporting multilingual storage. We skip our // own fields as they are always translatable. Additionally we skip a set of // well-known fields implementing entity system business logic. return $definition->isTranslatable() && $definition->getProvider() != 'content_translation' && !in_array($definition->getName(), [$entity_type->getKey('langcode'), $entity_type->getKey('default_langcode'), 'revision_translation_affected']); } /** * (proxied) Implements hook_preprocess_HOOK(). */ function _content_translation_preprocess_language_content_settings_table(&$variables) { // Alter the 'build' variable injecting the translation settings if the user // has the required permission. if (!\Drupal::currentUser()->hasPermission('administer content translation')) { return; } $element = $variables['element']; $build = &$variables['build']; array_unshift($build['#header'], ['data' => t('Translatable'), 'class' => ['translatable']]); $rows = []; foreach (Element::children($element) as $bundle) { $field_names = !empty($element[$bundle]['fields']) ? Element::children($element[$bundle]['fields']) : []; if (!empty($element[$bundle]['translatable'])) { $checkbox_id = $element[$bundle]['translatable']['#id']; } $rows[$bundle] = $build['#rows'][$bundle]; if (!empty($element[$bundle]['translatable'])) { $translatable = [ 'data' => $element[$bundle]['translatable'], 'class' => ['translatable'], ]; array_unshift($rows[$bundle]['data'], $translatable); $rows[$bundle]['data'][1]['data']['#prefix'] = '