You are here

function _content_translation_form_language_content_settings_form_alter in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/content_translation/content_translation.admin.inc \_content_translation_form_language_content_settings_form_alter()

(proxied) Implements hook_form_FORM_ID_alter().

1 call to _content_translation_form_language_content_settings_form_alter()
content_translation_form_language_content_settings_form_alter in core/modules/content_translation/content_translation.module
Implements hook_form_FORM_ID_alter() for language_content_settings_form().

File

core/modules/content_translation/content_translation.admin.inc, line 69
The content translation administration forms.

Code

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;
  }
  $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';
  $dependent_options_settings = array();
  $entity_manager = Drupal::entityManager();
  foreach ($form['#labels'] as $entity_type_id => $label) {
    $entity_type = $entity_manager
      ->getDefinition($entity_type_id);
    $storage_definitions = $entity_type instanceof ContentEntityTypeInterface ? $entity_manager
      ->getFieldStorageDefinitions($entity_type_id) : array();
    $entity_type_translatable = $content_translation_manager
      ->isSupported($entity_type_id);
    foreach (entity_get_bundles($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).', array(
          '@label' => $entity_type
            ->getLabel(),
        ));
        continue;
      }
      $fields = $entity_manager
        ->getFieldDefinitions($entity_type_id, $bundle);
      if ($fields) {
        foreach ($fields as $field_name => $definition) {
          if (!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] = array(
              '#label' => $definition
                ->getLabel(),
              '#type' => 'checkbox',
              '#default_value' => $definition
                ->isTranslatable(),
            );

            // Display the column translatability configuration widget.
            $column_element = content_translation_field_sync_widget($definition);
            if ($column_element) {
              $form['settings'][$entity_type_id][$bundle]['columns'][$field_name] = $column_element;

              // @todo This should not concern only files.
              if (isset($column_element['#options']['file'])) {
                $dependent_options_settings["settings[{$entity_type_id}][{$bundle}][columns][{$field_name}]"] = array(
                  'file',
                );
              }
            }
          }
        }
        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'] = array(
            '#type' => 'checkbox',
            '#default_value' => $content_translation_manager
              ->isEnabled($entity_type_id, $bundle),
          );
        }
      }
    }
  }
  $settings = array(
    'dependent_selectors' => $dependent_options_settings,
  );
  $form['#attached']['drupalSettings']['contentTranslationDependentOptions'] = $settings;
  $form['#validate'][] = 'content_translation_form_language_content_settings_validate';
  $form['#submit'][] = 'content_translation_form_language_content_settings_submit';
}