/** * Tests whether field languages are correctly set through the node form. */ function testMultilingualNodeForm() { // Create "Basic page" content. $langcode = language_get_default_langcode('node', 'page'); $title_key = 'title[0][value]'; $title_value = $this->randomMachineName(8); $body_key = 'body[0][value]'; $body_value = $this->randomMachineName(16); // Create node to edit. $edit = array(); $edit[$title_key] = $title_value; $edit[$body_key] = $body_value; $this->drupalPostForm('node/add/page', $edit, t('Save')); // Check that the node exists in the database. $node = $this->drupalGetNodeByTitle($edit[$title_key]); $this->assertTrue($node, 'Node found in database.'); $this->assertTrue($node->language()->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly set.'); // Change node language. $langcode = 'it'; $this->drupalGet("node/{$node->id()}/edit"); $edit = array($title_key => $this->randomMachineName(8), 'langcode[0][value]' => $langcode); $this->drupalPostForm(NULL, $edit, t('Save')); $node = $this->drupalGetNodeByTitle($edit[$title_key], TRUE); $this->assertTrue($node, 'Node found in database.'); $this->assertTrue($node->language()->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly changed.'); // Enable content language URL detection. $this->container->get('language_negotiator')->saveConfiguration(LanguageInterface::TYPE_CONTENT, array(LanguageNegotiationUrl::METHOD_ID => 0)); // Test multilingual field language fallback logic. $this->drupalGet("it/node/{$node->id()}"); $this->assertRaw($body_value, 'Body correctly displayed using Italian as requested language'); $this->drupalGet("node/{$node->id()}"); $this->assertRaw($body_value, 'Body correctly displayed using English as requested language'); }
/** * Returns a rendered edit form to create a new term associated to the given vocabulary. * * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary * The vocabulary this term will be added to. * * @return array * The taxonomy term add form. */ public function addForm(VocabularyInterface $taxonomy_vocabulary) { $term = $this->entityManager()->getStorage('taxonomy_term')->create(array('vid' => $taxonomy_vocabulary->id())); if ($this->moduleHandler()->moduleExists('language')) { $term->langcode = language_get_default_langcode('taxonomy_term', $taxonomy_vocabulary->id()); } return $this->entityFormBuilder()->getForm($term); }
/** * Returns a rendered edit form to create a new shortcut associated to the * given shortcut set. * * @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set * The shortcut set this shortcut will be added to. * * @return array * The shortcut add form. */ public function addForm(ShortcutSetInterface $shortcut_set) { $shortcut = $this->entityManager()->getStorage('shortcut')->create(array('shortcut_set' => $shortcut_set->id())); if ($this->moduleHandler()->moduleExists('language')) { $shortcut->langcode = language_get_default_langcode('shortcut', $shortcut_set->id()); } return $this->entityFormBuilder()->getForm($shortcut, 'add'); }
/** * Tests that the language_get_default_langcode() returns the correct values. */ public function testDefaultLangcode() { // Add some custom languages. foreach (array('aa', 'bb', 'cc') as $language_code) { $language = new Language(array('id' => $language_code, 'name' => $this->randomName())); language_save($language); } // Fixed language. language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'bb', 'language_show' => TRUE)); $langcode = language_get_default_langcode('custom_type', 'custom_bundle'); $this->assertEqual($langcode, 'bb'); // Current interface. language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'current_interface', 'language_show' => TRUE)); $langcode = language_get_default_langcode('custom_type', 'custom_bundle'); $language_interface = \Drupal::languageManager()->getCurrentLanguage(); $this->assertEqual($langcode, $language_interface->id); // Site's default. $old_default = \Drupal::languageManager()->getDefaultLanguage(); // Ensure the language entity default value is correct. $language_entity = entity_load('language_entity', $old_default->getId()); $this->assertTrue($language_entity->get('default'), 'The en language entity is flagged as the default language.'); $old_default->default = FALSE; language_save($old_default); $new_default = \Drupal::languageManager()->getLanguage('cc'); $new_default->default = TRUE; language_save($new_default); language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'site_default', 'language_show' => TRUE)); $langcode = language_get_default_langcode('custom_type', 'custom_bundle'); $this->assertEqual($langcode, 'cc'); // Ensure the language entity default value is correct. $language_entity = entity_load('language_entity', $old_default->getId()); $this->assertFalse($language_entity->get('default'), 'The en language entity is not flagged as the default language.'); $language_entity = entity_load('language_entity', 'cc'); // Check calling the Drupal\language\Entity\Language::isDefault() method // directly. $this->assertTrue($language_entity->isDefault(), 'The cc language entity is flagged as the default language.'); // Check the default value of a language field when authors preferred option // is selected. // Create first an user and assign a preferred langcode to him. $some_user = $this->drupalCreateUser(); $some_user->preferred_langcode = 'bb'; $some_user->save(); $this->drupalLogin($some_user); language_save_default_configuration('custom_type', 'some_bundle', array('langcode' => 'authors_default', 'language_show' => TRUE)); $this->drupalGet('language-tests/language_configuration_element_test'); $this->assertOptionSelected('edit-langcode', 'bb'); }
/** * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize(). * * @param array $data * Entity data to restore. * @param string $class * Unused, entity_create() is used to instantiate entity objects. * @param string $format * Format the given data was extracted from. * @param array $context * Options available to the denormalizer. Keys that can be used: * - request_method: if set to "patch" the denormalization will clear out * all default values for entity fields before applying $data to the * entity. * * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException */ public function denormalize($data, $class, $format = NULL, array $context = array()) { // Get type, necessary for determining which bundle to create. if (!isset($data['_links']['type'])) { throw new UnexpectedValueException('The type link relation must be specified.'); } // Create the entity. $typed_data_ids = $this->getTypedDataIds($data['_links']['type']); // Figure out the language to use. if (isset($data['langcode'])) { $langcode = $data['langcode'][0]['value']; // Remove the langcode so it does not get iterated over below. unset($data['langcode']); } elseif ($this->moduleHandler->moduleExists('language')) { $langcode = language_get_default_langcode($typed_data_ids['entity_type'], $typed_data_ids['bundle']); } else { $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED; } $entity_type = $this->entityManager->getDefinition($typed_data_ids['entity_type']); $values = array('langcode' => $langcode); if ($entity_type->hasKey('bundle')) { $bundle_key = $entity_type->getKey('bundle'); $values[$bundle_key] = $typed_data_ids['bundle']; // Unset the bundle key from data, if it's there. unset($data[$bundle_key]); } $entity = $this->entityManager->getStorage($typed_data_ids['entity_type'])->create($values); // Special handling for PATCH: destroy all possible default values that // might have been set on entity creation. We want an "empty" entity that // will only get filled with fields from the data array. if (isset($context['request_method']) && $context['request_method'] == 'patch') { foreach ($entity as $field_name => $field) { $entity->set($field_name, NULL); } } // Remove links from data array. unset($data['_links']); // Get embedded resources and remove from data array. $embedded = array(); if (isset($data['_embedded'])) { $embedded = $data['_embedded']; unset($data['_embedded']); } // Flatten the embedded values. foreach ($embedded as $relation => $field) { $field_ids = $this->linkManager->getRelationInternalIds($relation); if (!empty($field_ids)) { $field_name = $field_ids['field_name']; $data[$field_name] = $field; } } // Iterate through remaining items in data array. These should all // correspond to fields. foreach ($data as $field_name => $field_data) { // Remove any values that were set as a part of entity creation (e.g // uuid). If this field is set to an empty array in the data, this will // also have the effect of marking the field for deletion in REST module. $entity->{$field_name} = array(); $field = $entity->get($field_name); // Get the class of the field. This will generally be the default Field // class. $field_class = get_class($field); // Pass in the empty field object as a target instance. Since the context // is already prepared for the field, any data added to it is // automatically added to the entity. $context['target_instance'] = $field; $this->serializer->denormalize($field_data, $field_class, $format, $context); } return $entity; }
/** * {@inheritdoc} */ public function buildForm(array $form, array &$form_state) { $form['langcode'] = array('#title' => t('Language select'), '#type' => 'language_select', '#default_value' => language_get_default_langcode('custom_type', 'some_bundle')); return $form; }
/** * Tests that the language_get_default_langcode() returns the correct values. */ public function testDefaultLangcode() { // Add some custom languages. foreach (array('aa', 'bb', 'cc') as $language_code) { ConfigurableLanguage::create(array('id' => $language_code, 'label' => $this->randomMachineName()))->save(); } // Fixed language. ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')->setLanguageAlterable(TRUE)->setDefaultLangcode('bb')->save(); $langcode = language_get_default_langcode('entity_test', 'custom_bundle'); $this->assertEqual($langcode, 'bb'); // Current interface. ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')->setLanguageAlterable(TRUE)->setDefaultLangcode('current_interface')->save(); $langcode = language_get_default_langcode('entity_test', 'custom_bundle'); $language_interface = \Drupal::languageManager()->getCurrentLanguage(); $this->assertEqual($langcode, $language_interface->getId()); // Site's default. $old_default = \Drupal::languageManager()->getDefaultLanguage(); // Ensure the language entity default value is correct. $configurable_language = entity_load('configurable_language', $old_default->getId()); $this->assertTrue($configurable_language->isDefault(), 'The en language entity is flagged as the default language.'); $this->config('system.site')->set('langcode', 'cc')->save(); ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')->setLanguageAlterable(TRUE)->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT)->save(); $langcode = language_get_default_langcode('entity_test', 'custom_bundle'); $this->assertEqual($langcode, 'cc'); // Ensure the language entity default value is correct. $configurable_language = entity_load('configurable_language', $old_default->getId()); $this->assertFalse($configurable_language->isDefault(), 'The en language entity is not flagged as the default language.'); $configurable_language = entity_load('configurable_language', 'cc'); // Check calling the // \Drupal\language\ConfigurableLanguageInterface::isDefault() method // directly. $this->assertTrue($configurable_language->isDefault(), 'The cc language entity is flagged as the default language.'); // Check the default value of a language field when authors preferred option // is selected. // Create first an user and assign a preferred langcode to him. $some_user = $this->drupalCreateUser(); $some_user->preferred_langcode = 'bb'; $some_user->save(); $this->drupalLogin($some_user); ContentLanguageSettings::create(['target_entity_type_id' => 'entity_test', 'target_bundle' => 'some_bundle'])->setLanguageAlterable(TRUE)->setDefaultLangcode('authors_default')->save(); $this->drupalGet('language-tests/language_configuration_element_test'); $this->assertOptionSelected('edit-langcode', 'bb'); }
/** * Provides default language code of given entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity whose language code to be loaded. * * @return string * A string language code. */ public function getDefaultLangcode(EntityInterface $entity) { return language_get_default_langcode($entity->getEntityTypeId(), $entity->bundle()); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['langcode'] = array('#title' => t('Language select'), '#type' => 'language_select', '#default_value' => language_get_default_langcode('entity_test', 'some_bundle')); return $form; }