Commit a4bb2fa5 authored by alexpott's avatar alexpott

Issue #2383197 by queenvictoria, bforchhammer: Entities not loaded for...

Issue #2383197 by queenvictoria, bforchhammer: Entities not loaded for relationships on same entity type
parent 0a38107e
......@@ -122,8 +122,12 @@ public function testRelationship() {
$this->executeView($view);
foreach (array_keys($view->result) as $index) {
// Just check that the actual ID of the entity is the expected one.
// 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
......@@ -141,6 +145,7 @@ 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);
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\entity_reference\Tests\Views;
use Drupal\simpletest\WebTestBase;
use Drupal\views\Views;
/**
* Tests entity reference selection handler.
......@@ -19,18 +20,33 @@ class SelectionTest extends WebTestBase {
public static $modules = array('node', 'views', 'entity_reference', 'entity_reference_test', 'entity_test');
/**
* Tests the selection handler.
* Nodes for testing.
*
* @var array
*/
public function testSelectionHandler() {
protected $nodes = array();
/**
* The entity reference field to test.
*
* @var \Drupal\Core\Field\FieldDefinitionInterface
*/
protected $field;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// 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) {
$nodes[$node->getType()][$node->id()] = $node->label();
$this->nodes[$node->getType()][$node->id()] = $node->label();
}
// Create a field.
......@@ -60,21 +76,72 @@ public function testSelectionHandler() {
),
));
$field->save();
$this->field = $field;
}
// Get values from selection handler.
$handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($field);
$result = $handler->getReferenceableEntities();
/**
* Confirm the expected results are returned.
*
* @param array $result
* Query results keyed by node type and nid.
*/
protected function assertResults(array $result) {
$success = FALSE;
foreach ($result as $node_type => $values) {
foreach ($values as $nid => $label) {
if (!$success = $nodes[$node_type][$nid] == trim(strip_tags($label))) {
if (!$success = $this->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);
}
}
......@@ -277,8 +277,8 @@ public function getEntityTableInfo() {
);
// Include the entity provider.
if (!empty($table_data['table']['provider'])) {
$entity_tables[$table_data['table']['entity type']]['provider'] = $table_data['table']['provider'];
if (!empty($base_table_data['table']['provider'])) {
$entity_tables[$base_table_data['table']['entity type']]['provider'] = $base_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[$table_data['table']['entity type']] = array(
$entity_tables[$relationship_id . '__' . $relationship->tableAlias] = 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[$table_data['table']['entity type']]['provider'] = $table_data['table']['provider'];
$entity_tables[$relationship_id . '__' . $relationship->tableAlias]['provider'] = $table_data['table']['provider'];
}
}
}
......
......@@ -1254,7 +1254,7 @@ public function query($get_count = FALSE) {
}
foreach ($entity_information as $entity_type_id => $info) {
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
$entity_type = \Drupal::entityManager()->getDefinition($info['entity_type']);
$base_field = empty($table['revision']) ? $entity_type->getKey('id') : $entity_type->getKey('revision');
$this->addField($info['alias'], $base_field, '', $params);
}
......@@ -1468,30 +1468,40 @@ 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 $entity_type => $info) {
$entity_info = \Drupal::entityManager()->getDefinition($entity_type);
foreach ($entity_information as $info) {
$relationship_id = $info['relationship_id'];
$entity_type = $info['entity_type'];
$entity_info = $entity_types[$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] = $result->$id_alias;
$ids_by_type[$entity_type][$index][$relationship_id] = $result->$id_alias;
}
}
}
// Load all entities and assign them to the correct result row.
foreach ($ids_by_type as $entity_type => $ids) {
$info = $entity_information[$entity_type];
$relationship_id = $info['relationship_id'];
$flat_ids = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($ids)), FALSE);
// Drupal core currently has no way to load multiple revisions. Sad.
if ($info['revision']) {
if (isset($entity_table_info[$entity_type]['revision']) && $entity_table_info[$entity_type]['revision'] == TRUE) {
$entities = array();
foreach ($ids as $revision_id) {
foreach ($flat_ids as $revision_id) {
$entity = entity_revision_load($entity_type, $revision_id);
if ($entity) {
$entities[$revision_id] = $entity;
......@@ -1499,22 +1509,24 @@ function loadEntities(&$results) {
}
}
else {
$entities = entity_load_multiple($entity_type, $ids);
$entities = entity_load_multiple($entity_type, $flat_ids);
}
foreach ($ids as $index => $id) {
if (isset($entities[$id])) {
$entity = $entities[$id];
}
else {
$entity = NULL;
}
foreach ($ids as $index => $relationships) {
foreach ($relationships as $relationship_id => $entity_id) {
if (isset($entities[$entity_id])) {
$entity = $entities[$entity_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;
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment