diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc index feb3e7a..d892d13 100644 --- a/core/modules/content_translation/content_translation.admin.inc +++ b/core/modules/content_translation/content_translation.admin.inc @@ -60,7 +60,8 @@ function content_translation_field_sync_widget(FieldDefinitionInterface $field) function _content_translation_form_language_content_settings_form_alter(array &$form, array &$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')) { + $account = \Drupal::currentUser(); + if (!$account->hasPermission('administer content translation')) { return; } @@ -146,7 +147,8 @@ function _content_translation_form_language_content_settings_form_alter(array &$ 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')) { + $account = \Drupal::currentUser(); + if (!$account->hasPermission('administer content translation')) { return; } diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index d90da1b..3c5169d 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -308,9 +308,15 @@ function _content_translation_menu_strip_loaders($path) { * The entity whose translation overview should be displayed. */ function content_translation_translate_access(EntityInterface $entity) { - $entity_type = $entity->entityType(); - return empty($entity->getUntranslated()->language()->locked) && language_multilingual() && $entity->isTranslatable() && - (Drupal::currentUser()->hasPermission('create content translations') || Drupal::currentUser()->hasPermission('update content translations') || Drupal::currentUser()->hasPermission('delete content translations')); + global $user; + + if (!isset($account)) { + // In the installer request session is not set, so we have to fall back + // to the global $user. In all other cases the session key is preferred. + $account = \Drupal::currentUser() ?: $user; + } + return $entity instanceof ContentEntityInterface && empty($entity->getUntranslated()->language()->locked) && language_multilingual() && $entity->isTranslatable() && + ($account->hasPermission('create content translations') || $account->hasPermission('update content translations') || $account->hasPermission('delete content translations')); } /** @@ -325,8 +331,20 @@ function content_translation_translate_access(EntityInterface $entity) { * the current user. */ function content_translation_view_access(EntityInterface $entity, $langcode, AccountInterface $account = NULL) { + global $user; + + if (!isset($account)) { + // In the installer request session is not set, so we have to fall back + // to the global $user. In all other cases the session key is preferred. + $account = \Drupal::currentUser() ?: $user; + } $entity_type = $entity->entityType(); - return !empty($entity->translation[$langcode]['status']) || $account->hasPermission('translate any entity') || $account->hasPermission("translate $entity_type entities"); + $info = $entity->entityInfo(); + $permission = "translate $entity_type"; + if (!empty($info['permission_granularity']) && $info['permission_granularity'] == 'bundle') { + $permission = "translate {$entity->bundle()} $entity_type"; + } + return !empty($entity->translation[$langcode]['status']) || $account->hasPermission('translate any entity', $account) || $account->hasPermission($permission, $account); } /** @@ -788,26 +806,23 @@ function content_translation_field_extra_fields() { * Implements hook_form_FORM_ID_alter() for 'field_ui_field_edit_form'. */ function content_translation_form_field_ui_field_edit_form_alter(array &$form, array &$form_state, $form_id) { - $field = $form['#field']; - $entity_type = $field['entity_type']; - $field_name = $field['field_name']; - $translatable = $field['translatable']; - $label = t('Field translation'); - - if ($field->hasData()) { - $form['field']['translatable'] = array( - '#type' => 'item', - '#title' => $label, - '#attributes' => array('class' => 'translatable'), - 'link' => array( - '#type' => 'link', - '#prefix' => t('This field has data in existing content.') . ' ', - '#title' => !$translatable ? t('Enable translation') : t('Disable translation'), - '#href' => "admin/config/regional/content_translation/translatable/$entity_type/$field_name", - '#options' => array('query' => drupal_get_destination()), - '#access' => Drupal::currentUser()->hasPermission('administer content translation'), - ), - ); + $form['field']['translatable'] = array( + '#type' => 'checkbox', + '#title' => t('Users may translate this field.'), + '#default_value' => $form['#field']->isTranslatable(), + '#weight' => 20, + ); + $form['#submit'][] = 'content_translation_form_field_ui_field_edit_form_submit'; +} + +/** + * Form submission handler for 'field_ui_field_edit_form'. + */ +function content_translation_form_field_ui_field_edit_form_submit($form, array &$form_state) { + $instance = $form_state['instance']; + $value = content_translation_get_config($instance->entity_type, $instance->bundle, 'fields'); + if (!isset($value)) { + $value = array(); } $value[$instance->getField()->getName()] = $form_state['values']['field']['translatable']; // Store the same value for all bundles as translatability is tracked per @@ -901,7 +916,14 @@ function content_translation_enable_widget($entity_type, $bundle, array &$form, * Processed language configuration element. */ function content_translation_language_configuration_element_process(array $element, array &$form_state, array &$form) { - if (empty($element['#content_translation_skip_alter']) && Drupal::currentUser()->hasPermission('administer content translation')) { + global $user; + + if (!isset($account)) { + // In the installer request session is not set, so we have to fall back + // to the global $user. In all other cases the session key is preferred. + $account = \Drupal::currentUser() ?: $user; + } + if (empty($element['#content_translation_skip_alter']) && $account->hasPermission('administer content translation')) { $form_state['content_translation']['key'] = $element['#name']; $context = $form_state['language'][$element['#name']]; diff --git a/core/modules/content_translation/content_translation.pages.inc b/core/modules/content_translation/content_translation.pages.inc index f55671e..26d32a3 100644 --- a/core/modules/content_translation/content_translation.pages.inc +++ b/core/modules/content_translation/content_translation.pages.inc @@ -23,8 +23,8 @@ function content_translation_overview(EntityInterface $entity) { $languages = language_list(); $original = $entity->getUntranslated()->language()->id; $translations = $entity->getTranslationLanguages(); - $field_ui = module_exists('field_ui') && Drupal::currentUser()->hasPermission('administer ' . $entity->entityType() . ' fields'); - + $account = \Drupal::currentUser(); + $field_ui = \Drupal::moduleHandler()->moduleExists('field_ui') && $account->hasPermission('administer ' . $entity->entityType() . ' fields'); $rel = array(); foreach (array('canonical', 'edit-form', 'drupal:content-translation-overview') as $name) { $rel[$name] = $entity->uri($name); diff --git a/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php b/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php index 6d14c1b..e2befb1 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/ContentTranslationController.php @@ -63,10 +63,11 @@ public function getTranslationAccess(EntityInterface $entity, $op) { $translate_permission = TRUE; // If no permission granularity is defined this entity type does not need an // explicit translate permission. - if (!user_access('translate any entity') && !empty($info['permission_granularity'])) { - $translate_permission = user_access($info['permission_granularity'] == 'bundle' ? "translate {$entity->bundle()} {$entity->entityType()}" : "translate {$entity->entityType()}"); + $account = \Drupal::currentUser(); + if (!$account->hasPermission('translate any entity') && !empty($info['permission_granularity'])) { + $translate_permission = $account->hasPermission($info['permission_granularity'] == 'bundle' ? "translate {$entity->bundle()} {$entity->entityType()}" : "translate {$entity->entityType()}"); } - return $translate_permission && user_access("$op content translations"); + return $translate_permission && $account->hasPermission("$op content translations"); } /**