Skip to content

Instantly share code, notes, and snippets.

@plach79
Created November 19, 2014 23:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plach79/c1425c28d671ec828a6c to your computer and use it in GitHub Desktop.
Save plach79/c1425c28d671ec828a6c to your computer and use it in GitHub Desktop.
Content language settings form
diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc
index 231dbc6..5832aed 100644
--- a/core/modules/content_translation/content_translation.admin.inc
+++ b/core/modules/content_translation/content_translation.admin.inc
@@ -86,12 +86,17 @@ function _content_translation_form_language_content_settings_form_alter(array &$
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 = $entity_type->isTranslatable();
+ $form['settings'][$entity_type_id]['#translatable'] = $entity_type_translatable;
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.
$form['settings'][$entity_type_id][$bundle]['settings']['language']['#content_translation_skip_alter'] = TRUE;
+ if (!$entity_type_translatable) {
+ continue;
+ }
$fields = $entity_manager->getFieldDefinitions($entity_type_id, $bundle);
if ($fields) {
@@ -138,13 +143,12 @@ 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')) {
+ $element = $variables['element'];
+ if (empty($element['#translatable']) || !\Drupal::currentUser()->hasPermission('administer content translation')) {
return;
}
- $element = $variables['element'];
$build = &$variables['build'];
-
array_unshift($build['#header'], array('data' => t('Translatable'), 'class' => array('translatable')));
$rows = array();
diff --git a/core/modules/language/language.module b/core/modules/language/language.module
index 01113ca..565e008 100644
--- a/core/modules/language/language.module
+++ b/core/modules/language/language.module
@@ -547,6 +547,8 @@ function language_field_info_alter(&$info) {
* Implements hook_entity_field_access()
*/
function language_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
+ // @todo Use the language entity key here.
+ // See https://www.drupal.org/node/2143729.
if ($field_definition->getName() == 'langcode') {
$language_configuration = language_get_default_configuration($field_definition->getTargetEntityTypeId(), $items->getEntity()->bundle());
return AccessResult::forbiddenIf(!$language_configuration['language_show']);
diff --git a/core/modules/language/src/Form/ContentLanguageSettingsForm.php b/core/modules/language/src/Form/ContentLanguageSettingsForm.php
index a415379..b48517f 100644
--- a/core/modules/language/src/Form/ContentLanguageSettingsForm.php
+++ b/core/modules/language/src/Form/ContentLanguageSettingsForm.php
@@ -8,6 +8,7 @@
namespace Drupal\language\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
@@ -68,7 +69,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$bundles = $this->entityManager->getAllBundleInfo();
$language_configuration = array();
foreach ($entity_types as $entity_type_id => $entity_type) {
- if (!$entity_type->isTranslatable()) {
+ if (!$entity_type instanceof ContentEntityTypeInterface || !isset($this->entityManager->getBaseFieldDefinitions($entity_type_id)['langcode'])) {
continue;
}
$labels[$entity_type_id] = $entity_type->getLabel() ?: $entity_type_id;
@plach79
Copy link
Author

plach79 commented Nov 20, 2014

Line #69 does not work well as all (core) content entity types have a language field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment