diff --git a/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php b/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php index 9ae79a7..da87544 100644 --- a/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php +++ b/core/modules/entity_reference/src/Tests/Views/EntityReferenceRelationshipTest.php @@ -122,12 +122,8 @@ public function testRelationship() { $this->executeView($view); foreach (array_keys($view->result) as $index) { - // Check that the actual ID of the entity is the expected one. + // Just check that the actual ID of the entity is the expected one. $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id()); - - // Also check that we have the correct result entity. - $this->assertEqual($view->result[$index]->_entity->id(), $this->entities[$index + 1]->id()); - // Test the forward relationship. // The second and third entity refer to the first one. // The value key on the result will be in the format @@ -145,7 +141,6 @@ public function testRelationship() { foreach (array_keys($view->result) as $index) { $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id()); - $this->assertEqual($view->result[$index]->_entity->id(), $this->entities[$index + 1]->id()); // The second and third entity refer to the first one. $this->assertEqual($view->result[$index]->entity_test_entity_test__field_test_id, $index == 0 ? NULL : 1); } diff --git a/core/modules/entity_reference/src/Tests/Views/SelectionTest.php b/core/modules/entity_reference/src/Tests/Views/SelectionTest.php index 26cc00e..60d6f04 100644 --- a/core/modules/entity_reference/src/Tests/Views/SelectionTest.php +++ b/core/modules/entity_reference/src/Tests/Views/SelectionTest.php @@ -8,7 +8,6 @@ namespace Drupal\entity_reference\Tests\Views; use Drupal\simpletest\WebTestBase; -use Drupal\views\Views; /** * Tests entity reference selection handler. @@ -20,33 +19,18 @@ class SelectionTest extends WebTestBase { public static $modules = array('node', 'views', 'entity_reference', 'entity_reference_test', 'entity_test'); /** - * Nodes for testing. - * - * @var array - */ - protected $nodes = array(); - - /** - * The entity reference field to test. - * - * @var \Drupal\Core\Field\FieldDefinitionInterface - */ - protected $field; - - /** - * {@inheritdoc} + * Tests the selection handler. */ - public function setUp() { - parent::setUp(); - + public function testSelectionHandler() { // Create nodes. $type = $this->drupalCreateContentType()->id(); $node1 = $this->drupalCreateNode(array('type' => $type)); $node2 = $this->drupalCreateNode(array('type' => $type)); $node3 = $this->drupalCreateNode(); + $nodes = array(); foreach (array($node1, $node2, $node3) as $node) { - $this->nodes[$node->getType()][$node->id()] = $node->label(); + $nodes[$node->getType()][$node->id()] = $node->label(); } // Create a field. @@ -76,72 +60,21 @@ public function setUp() { ), )); $field->save(); - $this->field = $field; - } - /** - * Confirm the expected results are returned. - * - * @param array $result - * Query results keyed by node type and nid. - */ - protected function assertResults(array $result) { + // Get values from selection handler. + $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($field); + $result = $handler->getReferenceableEntities(); + $success = FALSE; foreach ($result as $node_type => $values) { foreach ($values as $nid => $label) { - if (!$success = $this->nodes[$node_type][$nid] == trim(strip_tags($label))) { + if (!$success = $nodes[$node_type][$nid] == trim(strip_tags($label))) { // There was some error, so break. break; } } } - $this->assertTrue($success, 'Views selection handler returned expected values.'); - } - /** - * Tests the selection handler. - */ - public function testSelectionHandler() { - // Get values from selection handler. - $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($this->field); - $result = $handler->getReferenceableEntities(); - $this->assertResults($result); - } - - /** - * Tests the selection handler with a relationship. - */ - public function testSelectionHandlerRelationship() { - // Add a relationship to the view. - $view = Views::getView('test_entity_reference'); - $view->setDisplay(); - $view->displayHandlers->get('default')->setOption('relationships', array( - 'test_relationship' => array( - 'id' => 'uid', - 'table' => 'users', - 'field' => 'uid', - ), - )); - - // Add a filter depending on the relationship to the test view. - $view->displayHandlers->get('default')->setOption('filters', array( - 'uid' => array( - 'id' => 'uid', - 'table' => 'users', - 'field' => 'uid', - 'relationship' => 'test_relationship', - ) - )); - - // Set view to distinct so only one row per node is returned. - $query_options = $view->display_handler->getOption('query'); - $query_options['options']['distinct'] = TRUE; - $view->display_handler->setOption('query', $query_options); - $view->save(); - - // Get values from the selection handler. - $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($this->field); - $result = $handler->getReferenceableEntities(); - $this->assertResults($result); + $this->assertTrue($success, 'Views selection handler returned expected values.'); } } diff --git a/core/modules/language/src/ConfigurableLanguageManager.php b/core/modules/language/src/ConfigurableLanguageManager.php index a4ca11b..98c5ff0 100644 --- a/core/modules/language/src/ConfigurableLanguageManager.php +++ b/core/modules/language/src/ConfigurableLanguageManager.php @@ -353,10 +353,11 @@ public function updateLockedLanguageWeights() { if (reset($locked_languages)->getWeight() <= $max_weight) { foreach ($locked_languages as $language) { // Update system languages weight. - $max_weight++; - ConfigurableLanguage::load($language->getId()) - ->setWeight($max_weight) - ->save(); + if ($language = ConfigurableLanguage::load($language->getId())) { + $max_weight++; + $language->setWeight($max_weight) + ->save(); + } } } } diff --git a/core/modules/language/src/Entity/ConfigurableLanguage.php b/core/modules/language/src/Entity/ConfigurableLanguage.php index e52f763..4f1f1b3 100644 --- a/core/modules/language/src/Entity/ConfigurableLanguage.php +++ b/core/modules/language/src/Entity/ConfigurableLanguage.php @@ -120,6 +120,18 @@ public function preSave(EntityStorageInterface $storage) { // For the uncommon case of custom languages the label should be given in // English. $this->langcode = 'en'; + // When adding a new language the weight shouldn't be zero to avoid a + // reordering of the languages list when their names change i.e. interface + // translation. + $languages = \Drupal::languageManager()->getLanguages(); + $last_language = end($languages); + // When adding the first Configurable language leave the weight by default. + // This happens when you are installing language module. + if ($this->isNew() && $last_language->getId() != $this->getId()) { + // The newly created language will have the weight of the heaviest + // language +1. + $this->setWeight($last_language->getWeight() + 1); + } } /** diff --git a/core/modules/language/src/Tests/LanguageListTest.php b/core/modules/language/src/Tests/LanguageListTest.php index c27f981..7fa533a 100644 --- a/core/modules/language/src/Tests/LanguageListTest.php +++ b/core/modules/language/src/Tests/LanguageListTest.php @@ -35,6 +35,10 @@ function testLanguageList() { $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration')); $this->drupalLogin($admin_user); + // Get the weight of the last language. + $languages = \Drupal::service('language_manager')->getLanguages(); + $last_language_weight = end($languages)->getWeight(); + // Add predefined language. $edit = array( 'predefined_langcode' => 'fr', @@ -43,6 +47,14 @@ function testLanguageList() { $this->assertText('French', 'Language added successfully.'); $this->assertUrl(\Drupal::url('language.admin_overview', [], ['absolute' => TRUE])); + // Get the weight of the last language and check that the weight is one unit + // heavier and that is French. + $this->rebuildContainer(); + $languages = \Drupal::service('language_manager')->getLanguages(); + $last_language = end($languages); + $this->AssertEqual($last_language->getWeight(), $last_language_weight + 1); + $this->AssertEqual($last_language->getId(), $edit['predefined_langcode']); + // Add custom language. $langcode = 'xx'; $name = $this->randomMachineName(16); @@ -54,7 +66,7 @@ function testLanguageList() { ); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); $this->assertUrl(\Drupal::url('language.admin_overview', [], ['absolute' => TRUE])); - $this->assertRaw('"edit-languages-' . $langcode .'-weight"', 'Language code found.'); + $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.'); $this->assertText(t($name), 'Test language added.'); $language = \Drupal::service('language_manager')->getLanguage($langcode); diff --git a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php index 1c254dc..98e7e50 100644 --- a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php +++ b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php @@ -277,8 +277,8 @@ public function getEntityTableInfo() { ); // Include the entity provider. - if (!empty($base_table_data['table']['provider'])) { - $entity_tables[$base_table_data['table']['entity type']]['provider'] = $base_table_data['table']['provider']; + if (!empty($table_data['table']['provider'])) { + $entity_tables[$table_data['table']['entity type']]['provider'] = $table_data['table']['provider']; } } @@ -286,7 +286,7 @@ public function getEntityTableInfo() { foreach ((array) $this->view->relationship as $relationship_id => $relationship) { $table_data = $views_data->get($relationship->definition['base']); if (isset($table_data['table']['entity type'])) { - $entity_tables[$relationship_id . '__' . $relationship->tableAlias] = array( + $entity_tables[$table_data['table']['entity type']] = array( 'base' => $relationship->definition['base'], 'relationship_id' => $relationship_id, 'alias' => $relationship->alias, @@ -296,7 +296,7 @@ public function getEntityTableInfo() { // Include the entity provider. if (!empty($table_data['table']['provider'])) { - $entity_tables[$relationship_id . '__' . $relationship->tableAlias]['provider'] = $table_data['table']['provider']; + $entity_tables[$table_data['table']['entity type']]['provider'] = $table_data['table']['provider']; } } } diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index 7a6706c..3aed415 100644 --- a/core/modules/views/src/Plugin/views/query/Sql.php +++ b/core/modules/views/src/Plugin/views/query/Sql.php @@ -1254,7 +1254,7 @@ public function query($get_count = FALSE) { } foreach ($entity_information as $entity_type_id => $info) { - $entity_type = \Drupal::entityManager()->getDefinition($info['entity_type']); + $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); $base_field = empty($table['revision']) ? $entity_type->getKey('id') : $entity_type->getKey('revision'); $this->addField($info['alias'], $base_field, '', $params); } @@ -1468,40 +1468,30 @@ function loadEntities(&$results) { return; } - // Extract all entity types from entity_information. - $entity_types = array(); - foreach ($entity_information as $info) { - $entity_type = $info['entity_type']; - if (!isset($entity_types[$entity_type])) { - $entity_types[$entity_type] = \Drupal::entityManager()->getDefinition($entity_type); - } - } - // Assemble a list of entities to load. $ids_by_type = array(); - foreach ($entity_information as $info) { - $relationship_id = $info['relationship_id']; - $entity_type = $info['entity_type']; - $entity_info = $entity_types[$entity_type]; + foreach ($entity_information as $entity_type => $info) { + $entity_info = \Drupal::entityManager()->getDefinition($entity_type); $id_key = empty($table['revision']) ? $entity_info->getKey('id') : $entity_info->getKey('revision'); $id_alias = $this->getFieldAlias($info['alias'], $id_key); foreach ($results as $index => $result) { // Store the entity id if it was found. if (isset($result->{$id_alias}) && $result->{$id_alias} != '') { - $ids_by_type[$entity_type][$index][$relationship_id] = $result->$id_alias; + $ids_by_type[$entity_type][$index] = $result->$id_alias; } } } // Load all entities and assign them to the correct result row. foreach ($ids_by_type as $entity_type => $ids) { - $flat_ids = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($ids)), FALSE); + $info = $entity_information[$entity_type]; + $relationship_id = $info['relationship_id']; // Drupal core currently has no way to load multiple revisions. Sad. - if (isset($entity_table_info[$entity_type]['revision']) && $entity_table_info[$entity_type]['revision'] == TRUE) { + if ($info['revision']) { $entities = array(); - foreach ($flat_ids as $revision_id) { + foreach ($ids as $revision_id) { $entity = entity_revision_load($entity_type, $revision_id); if ($entity) { $entities[$revision_id] = $entity; @@ -1509,24 +1499,22 @@ function loadEntities(&$results) { } } else { - $entities = entity_load_multiple($entity_type, $flat_ids); + $entities = entity_load_multiple($entity_type, $ids); } - foreach ($ids as $index => $relationships) { - foreach ($relationships as $relationship_id => $entity_id) { - if (isset($entities[$entity_id])) { - $entity = $entities[$entity_id]; - } - else { - $entity = NULL; - } + foreach ($ids as $index => $id) { + if (isset($entities[$id])) { + $entity = $entities[$id]; + } + else { + $entity = NULL; + } - if ($relationship_id == 'none') { - $results[$index]->_entity = $entity; - } - else { - $results[$index]->_relationship_entities[$relationship_id] = $entity; - } + if ($relationship_id == 'none') { + $results[$index]->_entity = $entity; + } + else { + $results[$index]->_relationship_entities[$relationship_id] = $entity; } } }