diff --git a/core/modules/language/src/Plugin/Field/FieldWidget/LanguageSelectWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php similarity index 59% rename from core/modules/language/src/Plugin/Field/FieldWidget/LanguageSelectWidget.php rename to core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php index 72396a8..e08a2e6 100644 --- a/core/modules/language/src/Plugin/Field/FieldWidget/LanguageSelectWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php @@ -2,10 +2,10 @@ /** * @file - * Contains \Drupal\language\Plugin\Field\LanguageSelectWidget. + * Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\LanguageSelectWidget. */ -namespace Drupal\language\Plugin\Field\FieldWidget; +namespace Drupal\Core\Field\Plugin\Field\FieldWidget; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; @@ -13,11 +13,11 @@ use Drupal\Core\Language\LanguageInterface; /** - * Plugin implementation of the 'langcode' widget. + * Plugin implementation of the 'Language' widget. * * @FieldWidget( * id = "language_select", - * label = @Translation("Language"), + * label = @Translation("Language select"), * field_types = { * "language" * } @@ -29,15 +29,12 @@ class LanguageSelectWidget extends WidgetBase { * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('node', $items->getEntity()->bundle())); $element['value'] = array( - '#title' => t('Language'), + '#title' => $this->t('Language'), '#type' => 'language_select', '#default_value' => $items->getEntity()->language()->getId(), '#languages' => LanguageInterface::STATE_ALL, - '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'], ); - return $element; } diff --git a/core/modules/aggregator/src/Entity/Feed.php b/core/modules/aggregator/src/Entity/Feed.php index 6532f36..ceb0090 100644 --- a/core/modules/aggregator/src/Entity/Feed.php +++ b/core/modules/aggregator/src/Entity/Feed.php @@ -153,7 +153,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['langcode'] = BaseFieldDefinition::create('language') ->setLabel(t('Language code')) - ->setDescription(t('The feed language code.')); + ->setDescription(t('The feed language code.')) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['url'] = BaseFieldDefinition::create('uri') ->setLabel(t('URL')) diff --git a/core/modules/aggregator/src/FeedForm.php b/core/modules/aggregator/src/FeedForm.php index f22de74..f05918d 100644 --- a/core/modules/aggregator/src/FeedForm.php +++ b/core/modules/aggregator/src/FeedForm.php @@ -23,26 +23,6 @@ class FeedForm extends ContentEntityForm { /** * {@inheritdoc} */ - public function form(array $form, FormStateInterface $form_state) { - $feed = $this->entity; - - // @todo: convert to a language selection widget defined in the base field. - // Blocked on https://drupal.org/node/2226493 which adds a generic - // language widget. - $form['langcode'] = array( - '#title' => $this->t('Language'), - '#type' => 'language_select', - '#default_value' => $feed->language()->id, - '#languages' => LanguageInterface::STATE_ALL, - '#weight' => -4, - ); - - return parent::form($form, $form_state, $feed); - } - - /** - * {@inheritdoc} - */ public function validate(array $form, FormStateInterface $form_state) { $feed = $this->buildEntity($form, $form_state); // Check for duplicate titles. diff --git a/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php index df923ee..e1070bb 100644 --- a/core/modules/block_content/src/BlockContentForm.php +++ b/core/modules/block_content/src/BlockContentForm.php @@ -103,24 +103,6 @@ public function form(array $form, FormStateInterface $form_state) { // names. $form['#attributes']['class'][0] = drupal_html_class('block-' . $block->bundle() . '-form'); - if ($this->moduleHandler->moduleExists('language')) { - $language_configuration = language_get_default_configuration('block_content', $block->bundle()); - - // Set the correct default language. - if ($block->isNew()) { - $language_default = $this->languageManager->getCurrentLanguage($language_configuration['langcode']); - $block->langcode->value = $language_default->id; - } - } - - $form['langcode'] = array( - '#title' => $this->t('Language'), - '#type' => 'language_select', - '#default_value' => $block->getUntranslated()->language()->id, - '#languages' => LanguageInterface::STATE_ALL, - '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'], - ); - $form['advanced'] = array( '#type' => 'vertical_tabs', '#weight' => 99, diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index e2320bd..f78d09d 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -156,9 +156,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setSetting('unsigned', TRUE); $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) + ->setLabel(t('Language')) ->setDescription(t('The custom block language code.')) - ->setRevisionable(TRUE); + ->setRevisionable(TRUE) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['info'] = BaseFieldDefinition::create('string') ->setLabel(t('Block description')) diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 5cf5d28..eae1de8 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -161,15 +161,6 @@ public function form(array $form, FormStateInterface $form_state) { $form['author']['name']['#attributes']['data-drupal-default-value'] = $this->config('user.settings')->get('anonymous'); } - $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('comment', $comment->getTypeId())); - $form['langcode'] = array( - '#title' => t('Language'), - '#type' => 'language_select', - '#default_value' => $comment->getUntranslated()->language()->id, - '#languages' => Language::STATE_ALL, - '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'], - ); - // Add author email and homepage fields depending on the current user. $form['author']['mail'] = array( '#type' => 'email', diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 57c7287..827cc60 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -221,8 +221,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setRequired(TRUE); $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The comment language code.')); + ->setLabel(t('Language')) + ->setDescription(t('The comment language code.')) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['subject'] = BaseFieldDefinition::create('string') ->setLabel(t('Subject')) diff --git a/core/modules/contact/src/Entity/Message.php b/core/modules/contact/src/Entity/Message.php index 20be2e8..0724312 100644 --- a/core/modules/contact/src/Entity/Message.php +++ b/core/modules/contact/src/Entity/Message.php @@ -144,8 +144,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setReadOnly(TRUE); $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The comment language code.')); + ->setLabel(t('Language')) + ->setDescription(t('The comment language code.')) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t("The sender's name")) diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 698fa8f..fbac8a3 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -98,15 +98,6 @@ public function form(array $form, FormStateInterface $form_state) { $form['preview']['message'] = $this->entityManager->getViewBuilder('contact_message')->view($message, 'full'); } - $language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('contact_message', $message->getContactForm()->id())); - $form['langcode'] = array( - '#title' => $this->t('Language'), - '#type' => 'language_select', - '#default_value' => $message->getUntranslated()->language()->id, - '#languages' => Language::STATE_ALL, - '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'], - ); - $form['name'] = array( '#type' => 'textfield', '#title' => $this->t('Your name'), diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 40b9bfa..d6b3ae9 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -5,6 +5,8 @@ * Add language handling functionality to Drupal. */ +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Routing\RouteMatchInterface; @@ -170,6 +172,24 @@ function language_process_language_select($element) { } /** + * Implements hook_field_widget_info_alter(). + */ +function language_field_widget_info_alter(array &$info) { + $info['language_select']['class'] = '\Drupal\language\Plugin\Field\FieldWidget\ConfigurableLanguageSelectWidget'; +} + +/** + * Implements hook_entity_base_field_info_alter(). + */ +function language_entity_base_field_info_alter(&$fields) { + foreach ($fields as $definition) { + if ($definition instanceof BaseFieldDefinition && $definition->getType() == 'language' && ($options = $definition->getDisplayOptions('form')) && !empty($options['type'])) { + $definition->setDisplayConfigurable('form', TRUE); + } + } +} + +/** * Implements hook_element_info(). * * @todo Remove once https://www.drupal.org/node/2326409 is in. diff --git a/core/modules/language/src/Plugin/Field/FieldWidget/ConfigurableLanguageSelectWidget.php b/core/modules/language/src/Plugin/Field/FieldWidget/ConfigurableLanguageSelectWidget.php new file mode 100644 index 0000000..3d84a1a --- /dev/null +++ b/core/modules/language/src/Plugin/Field/FieldWidget/ConfigurableLanguageSelectWidget.php @@ -0,0 +1,36 @@ +getFieldDefinition()->getTargetEntityTypeId(), $items->getEntity()->bundle()); + $element['value']['#access'] = !empty($language_configuration['language_show']); + + return $element; + } + +} diff --git a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php index ec719c2..e2e3dcf 100644 --- a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php +++ b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php @@ -385,8 +385,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { )); $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The node language code.')); + ->setLabel(t('Language')) + ->setDescription(t('The node language code.')) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['parent'] = BaseFieldDefinition::create('string') ->setLabel(t('Parent plugin ID')) diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index 4fb93b2..ac3d98f 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -257,21 +257,6 @@ public function form(array $form, FormStateInterface $form_state) { '#weight' => -2, ); - $language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('menu_link_content', 'menu_link_content')); - if ($this->entity->isNew()) { - $default_language = isset($language_configuration['langcode']) ? $language_configuration['langcode'] : $this->languageManager->getDefaultLanguage()->getId(); - } - else { - $default_language = $this->entity->getUntranslated()->language()->getId(); - } - $form['langcode'] = array( - '#title' => t('Language'), - '#type' => 'language_select', - '#default_value' => $default_language, - '#languages' => Language::STATE_ALL, - '#access' => !empty($language_configuration['language_show']), - ); - $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId(); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $this->entity->getPluginId()); $form['menu_parent']['#weight'] = 10; diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index 10a9313..2e4e020 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -352,9 +352,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setRevisionable(TRUE) ->setDisplayOptions('form', array( 'type' => 'language_select', - 'weight' => 0, - )) - ->setDisplayConfigurable('form', TRUE); + 'weight' => 2, + )); $fields['title'] = BaseFieldDefinition::create('string') ->setLabel(t('Title')) diff --git a/core/modules/shortcut/src/Entity/Shortcut.php b/core/modules/shortcut/src/Entity/Shortcut.php index 75d5b22..a4be0f5 100644 --- a/core/modules/shortcut/src/Entity/Shortcut.php +++ b/core/modules/shortcut/src/Entity/Shortcut.php @@ -213,8 +213,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDescription(t('A serialized array of route parameters of this shortcut.')); $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The language code of the shortcut.')); + ->setLabel(t('Language')) + ->setDescription(t('The language code of the shortcut.')) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['path'] = BaseFieldDefinition::create('string') ->setLabel(t('Path')) diff --git a/core/modules/shortcut/src/ShortcutForm.php b/core/modules/shortcut/src/ShortcutForm.php index c9f5d3d..ee53837 100644 --- a/core/modules/shortcut/src/ShortcutForm.php +++ b/core/modules/shortcut/src/ShortcutForm.php @@ -69,13 +69,6 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $this->entity->path->value, ); - $form['langcode'] = array( - '#title' => t('Language'), - '#type' => 'language_select', - '#default_value' => $this->entity->getUntranslated()->language()->id, - '#languages' => LanguageInterface::STATE_ALL, - ); - return $form; } diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php index b4cb26e..b80d221 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php @@ -76,8 +76,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setReadOnly(TRUE); $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The language code of the test entity.')); + ->setLabel(t('Language')) + ->setDescription(t('The language code of the test entity.')) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) diff --git a/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php b/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php index 1db41d4..ec7c3e0 100644 --- a/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php +++ b/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php @@ -35,13 +35,6 @@ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $entity = $this->entity; - $form['langcode'] = array( - '#title' => t('Language'), - '#type' => 'language_select', - '#default_value' => $entity->getUntranslated()->language()->id, - '#languages' => LanguageInterface::STATE_ALL, - ); - // @todo: Is there a better way to check if an entity type is revisionable? if ($entity->getEntityType()->hasKey('revision') && !$entity->isNew()) { $form['revision'] = array( diff --git a/core/modules/taxonomy/src/Entity/Term.php b/core/modules/taxonomy/src/Entity/Term.php index caebc53..047f5ad 100644 --- a/core/modules/taxonomy/src/Entity/Term.php +++ b/core/modules/taxonomy/src/Entity/Term.php @@ -118,8 +118,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setSetting('target_type', 'taxonomy_vocabulary'); $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The term language code.')); + ->setLabel(t('Language')) + ->setDescription(t('The term language code.')) + ->setDisplayOptions('form', array( + 'type' => 'language_select', + 'weight' => 2, + )); $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) diff --git a/core/modules/taxonomy/src/TermForm.php b/core/modules/taxonomy/src/TermForm.php index 65f1cfd..8cee383 100644 --- a/core/modules/taxonomy/src/TermForm.php +++ b/core/modules/taxonomy/src/TermForm.php @@ -28,15 +28,6 @@ public function form(array $form, FormStateInterface $form_state) { $form_state->set(['taxonomy', 'parent'], $parent); $form_state->set(['taxonomy', 'vocabulary'], $vocabulary); - $language_configuration = $this->moduleHandler->moduleExists('language') ? language_get_default_configuration('taxonomy_term', $vocabulary->id()) : FALSE; - $form['langcode'] = array( - '#type' => 'language_select', - '#title' => $this->t('Language'), - '#languages' => LanguageInterface::STATE_ALL, - '#default_value' => $term->getUntranslated()->language()->id, - '#access' => !empty($language_configuration['language_show']), - ); - $form['relations'] = array( '#type' => 'details', '#title' => $this->t('Relations'),