Issue #2034947 by damiankloip: Change view results to use a classed object.

8.0.x
Alex Pott 2013-07-21 08:46:30 +01:00
parent 2f0e07fa96
commit 08aeb8717f
33 changed files with 102 additions and 45 deletions

View File

@ -9,6 +9,7 @@ namespace Drupal\aggregator\Plugin\views\field;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\Component\Annotation\PluginID;
@ -64,7 +65,7 @@ class Category extends FieldPluginBase {
* @return data
* Returns string for the link text.
*/
protected function render_link($data, $values) {
protected function render_link($data, ResultRow $values) {
$cid = $this->getValue($values, 'cid');
if (!empty($this->options['link_to_category']) && !empty($cid) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;

View File

@ -9,6 +9,7 @@ namespace Drupal\aggregator\Plugin\views\field;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\Component\Annotation\PluginID;
@ -73,7 +74,7 @@ class TitleLink extends FieldPluginBase {
* @return data
* Returns string for the link text.
*/
protected function render_link($data, $values) {
protected function render_link($data, ResultRow $values) {
$link = $this->getValue($values, 'link');
if (!empty($this->options['display_as_link'])) {
$this->options['alter']['make_link'] = TRUE;

View File

@ -7,6 +7,7 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
@ -61,7 +62,7 @@ class Comment extends FieldPluginBase {
parent::buildOptionsForm($form, $form_state);
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
if (!empty($this->options['link_to_comment'])) {
$this->options['alter']['make_link'] = TRUE;
$nid = $this->getValue($values, 'nid');

View File

@ -9,6 +9,7 @@ namespace Drupal\comment\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Base field handler to present a link.
@ -47,7 +48,7 @@ class Link extends FieldPluginBase {
return $this->render_link($comment, $values);
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('view');
$comment = $data;
$nid = $comment->nid;

View File

@ -7,6 +7,7 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\Component\Annotation\PluginID;
/**
@ -23,7 +24,7 @@ class LinkApprove extends Link {
return user_access('administer comments');
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
$status = $this->getValue($values, 'status');
// Don't show an approve link on published nodes.

View File

@ -8,6 +8,7 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to delete a comment.
@ -23,7 +24,7 @@ class LinkDelete extends Link {
return user_access('administer comments');
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('delete');
$cid = $this->getValue($values, 'cid');

View File

@ -8,6 +8,7 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to edit a comment.
@ -36,7 +37,7 @@ class LinkEdit extends Link {
);
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
parent::render_link($data, $values);
// Ensure user has access to edit this comment.
$comment = $this->getValue($values);

View File

@ -8,6 +8,7 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to reply to a comment.
@ -23,7 +24,7 @@ class LinkReply extends Link {
return user_access('post comments');
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('reply');
$nid = $this->getValue($values, 'nid');
$cid = $this->getValue($values, 'cid');

View File

@ -11,6 +11,7 @@ use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\field\Numeric;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -127,7 +128,7 @@ class NodeNewComments extends Numeric {
}
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
$node = entity_create('node', array(
'nid' => $this->getValue($values, 'nid'),

View File

@ -7,6 +7,7 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
@ -48,7 +49,7 @@ class Username extends FieldPluginBase {
parent::buildOptionsForm($form, $form_state);
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
if (!empty($this->options['link_to_user'])) {
$account = entity_create('user', array());
$account->uid = $this->getValue($values, 'uid');

View File

@ -10,6 +10,7 @@ namespace Drupal\contact\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Annotation\PluginID;
use Drupal\user\Plugin\views\field\Link;
use Drupal\views\ResultRow;
/**
* Defines a field that links to the user contact page, if access is permitted.
@ -42,7 +43,7 @@ class ContactLink extends Link {
/**
* {@inheritdoc}
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
public function render_link(EntityInterface $entity, ResultRow $values) {
if (empty($entity)) {
return;

View File

@ -10,6 +10,7 @@ namespace Drupal\content_translation\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Entity\EntityInterface;
use Drupal\views\ResultRow;
/**
* Provides a translation link for an entity.
@ -59,7 +60,7 @@ class TranslationLink extends FieldPluginBase {
* @return string
* The acutal rendered text (without the link) of this field.
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
public function render_link(EntityInterface $entity, ResultRow $values) {
if (content_translation_translate_access($entity)) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('translate');

View File

@ -7,6 +7,7 @@
namespace Drupal\file\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Component\Annotation\PluginID;
@ -56,7 +57,7 @@ class File extends FieldPluginBase {
*
* Data should be made XSS safe prior to calling this function.
*/
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
if (!empty($this->options['link_to_file']) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = file_create_url($this->getValue($values, 'uri'));

View File

@ -9,6 +9,7 @@ namespace Drupal\node\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to the node.
@ -51,7 +52,7 @@ class Link extends FieldPluginBase {
}
}
function render_link($node, $values) {
function render_link($node, ResultRow $values) {
if (node_access('view', $node)) {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = 'node/' . $node->id();

View File

@ -9,6 +9,7 @@ namespace Drupal\node\Plugin\views\field;
use Drupal\node\Plugin\views\field\Link;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to delete a node.
@ -22,7 +23,7 @@ class LinkDelete extends Link {
/**
* Renders the link.
*/
function render_link($node, $values) {
function render_link($node, ResultRow $values) {
// Ensure user has access to delete this node.
if (!node_access('delete', $node)) {
return;

View File

@ -9,6 +9,7 @@ namespace Drupal\node\Plugin\views\field;
use Drupal\node\Plugin\views\field\Link;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link node edit.
@ -22,7 +23,7 @@ class LinkEdit extends Link {
/**
* Renders the link.
*/
function render_link($node, $values) {
function render_link($node, ResultRow $values) {
// Ensure user has access to edit this node.
if (!node_access('update', $node)) {
return;

View File

@ -7,6 +7,7 @@
namespace Drupal\node\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
@ -63,7 +64,7 @@ class Node extends FieldPluginBase {
*
* Data should be made XSS safe prior to calling this function.
*/
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
if (!empty($this->options['link_to_node']) && !empty($this->additional_fields['nid'])) {
if ($data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;

View File

@ -7,6 +7,7 @@
namespace Drupal\node\Plugin\views\field;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\node\Plugin\views\field\Node;
@ -59,7 +60,7 @@ class Revision extends Node {
*
* Data should be made XSS safe prior to calling this function.
*/
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
if (!empty($this->options['link_to_node_revision']) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$nid = $this->getValue($values, 'nid');

View File

@ -9,6 +9,7 @@ namespace Drupal\node\Plugin\views\field;
use Drupal\node\Plugin\views\field\Link;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\Component\Annotation\PluginID;
@ -34,7 +35,7 @@ class RevisionLink extends Link {
return user_access('view revisions') || user_access('administer nodes');
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
list($node, $vid) = $this->get_revision_entity($values, 'view');
if (!isset($vid)) {
return;

View File

@ -9,6 +9,7 @@ namespace Drupal\node\Plugin\views\field;
use Drupal\node\Plugin\views\field\RevisionLink;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present link to delete a node revision.
@ -23,7 +24,7 @@ class RevisionLinkDelete extends RevisionLink {
return user_access('delete revisions') || user_access('administer nodes');
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
list($node, $vid) = $this->get_revision_entity($values, 'delete');
if (!isset($vid)) {
return;

View File

@ -9,6 +9,7 @@ namespace Drupal\node\Plugin\views\field;
use Drupal\node\Plugin\views\field\RevisionLink;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to revert a node to a revision.
@ -23,7 +24,7 @@ class RevisionLinkRevert extends RevisionLink {
return user_access('revert revisions') || user_access('administer nodes');
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
list($node, $vid) = $this->get_revision_entity($values, 'update');
if (!isset($vid)) {
return;

View File

@ -9,6 +9,7 @@ namespace Drupal\taxonomy\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\Component\Annotation\PluginID;
@ -68,7 +69,7 @@ class Taxonomy extends FieldPluginBase {
*
* Data should be made XSS safe prior to calling this function.
*/
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
$tid = $this->getValue($values, 'tid');
if (!empty($this->options['link_to_taxonomy']) && !empty($tid) && $data !== NULL && $data !== '') {
$term = entity_create('taxonomy_term', array(

View File

@ -8,6 +8,7 @@
namespace Drupal\user\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Views field handler for user language.
@ -18,7 +19,7 @@ use Drupal\Component\Annotation\PluginID;
*/
class Language extends User {
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
$uid = $this->getValue($values, 'uid');
if (!empty($this->options['link_to_user'])) {
$uid = $this->getValue($values, 'uid');

View File

@ -9,6 +9,7 @@ namespace Drupal\user\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Annotation\PluginID;
@ -73,7 +74,7 @@ class Link extends FieldPluginBase {
* @return string
* The acutal rendered text (without the link) of this field.
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
public function render_link(EntityInterface $entity, ResultRow $values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('View');
$this->options['alter']['make_link'] = TRUE;

View File

@ -9,6 +9,7 @@ namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to user cancel.
@ -22,7 +23,7 @@ class LinkCancel extends Link {
/**
* Overrides \Drupal\user\Plugin\views\field\Link::render_link().
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
public function render_link(EntityInterface $entity, ResultRow $values) {
if ($entity && $entity->access('delete')) {
$this->options['alter']['make_link'] = TRUE;

View File

@ -9,6 +9,7 @@ namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
* Field handler to present a link to user edit.
@ -22,7 +23,7 @@ class LinkEdit extends Link {
/**
* Overrides \Drupal\user\Plugin\views\field\Link::render_link().
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
public function render_link(EntityInterface $entity, ResultRow $values) {
if ($entity && $entity->access('update')) {
$this->options['alter']['make_link'] = TRUE;

View File

@ -8,6 +8,7 @@
namespace Drupal\user\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
/**
/**
@ -39,7 +40,7 @@ class Mail extends User {
);
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
parent::render_link($data, $values);
if ($this->options['link_to_user'] == 'mailto') {

View File

@ -10,6 +10,7 @@ namespace Drupal\user\Plugin\views\field;
use Drupal\user\Plugin\views\field\User;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
/**
@ -74,7 +75,7 @@ class Name extends User {
);
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
$account = entity_create('user', array());
$account->uid = $this->getValue($values, 'uid');
$account->name = $this->getValue($values);

View File

@ -10,6 +10,7 @@ namespace Drupal\user\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Component\Annotation\PluginID;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
/**
@ -51,7 +52,7 @@ class User extends FieldPluginBase {
parent::buildOptionsForm($form, $form_state);
}
function render_link($data, $values) {
function render_link($data, ResultRow $values) {
if (!empty($this->options['link_to_user']) && user_access('access user profiles') && ($entity = $this->getEntity($values)) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$uri = $entity->uri();

View File

@ -9,6 +9,7 @@ namespace Drupal\views\Plugin\views\field;
use Drupal\views\Plugin\views\HandlerBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
/**
@ -361,13 +362,13 @@ abstract class FieldPluginBase extends HandlerBase {
/**
* Gets the entity matching the current row and relationship.
*
* @param \stdClass $values
* @param \Drupal\views\ResultRow $values
* An object containing all retrieved values.
*
* @return \Drupal\Core\Entity\EntityInterface
* Returns the entity matching the values.
*/
public function getEntity(\stdClass $values) {
public function getEntity(ResultRow $values) {
$relationship_id = $this->options['relationship'];
if ($relationship_id == 'none') {
return $values->_entity;

View File

@ -1424,11 +1424,9 @@ class Sql extends QueryPluginBase {
}
$result = $query->execute();
$result->setFetchMode(\PDO::FETCH_CLASS, 'Drupal\views\ResultRow');
$view->result = array();
foreach ($result as $item) {
$view->result[] = $item;
}
$view->result = iterator_to_array($result);
$view->pager->postExecute($view->result);
$view->pager->updatePageInfo();
@ -1519,12 +1517,6 @@ class Sql extends QueryPluginBase {
return;
}
// Initialize the entity placeholders in $results.
foreach ($results as $index => $result) {
$results[$index]->_entity = FALSE;
$results[$index]->_relationship_entities = array();
}
// Assemble a list of entities to load.
$ids_by_table = array();
foreach ($entity_tables as $table_alias => $table) {
@ -1562,7 +1554,12 @@ class Sql extends QueryPluginBase {
}
foreach ($ids as $index => $id) {
$entity = isset($entities[$id]) ? $entities[$id] : FALSE;
if (isset($entities[$id])) {
$entity = $entities[$id];
}
else {
$entity = NULL;
}
if ($relationship_id == 'none') {
$results[$index]->_entity = $entity;

View File

@ -0,0 +1,29 @@
<?php
/**
* @file
* Contains \Drupal\views\ResultRow.
*/
namespace Drupal\views;
/**
* A class representing a view result row.
*/
class ResultRow {
/**
* The entity for this result.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
public $_entity = NULL;
/**
* An array of relationship entities.
*
* @var array \Drupal\Core\Entity\EntityInterface[]
*/
public $_relationship_entities = array();
}

View File

@ -10,6 +10,7 @@ namespace Drupal\views\Tests\Plugin;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views_test_data\Plugin\views\row\RowTest;
use Drupal\views\Plugin\views\row\Fields;
use Drupal\views\ResultRow;
use Drupal\views_test_data\Plugin\views\style\StyleTest as StyleTestPlugin;
/**
@ -149,14 +150,14 @@ class StyleTest extends ViewTestBase {
$expected['Job: Singer']['group'] = 'Job: Singer';
$expected['Job: Singer']['rows']['Age: 25'] = array();
$expected['Job: Singer']['rows']['Age: 25']['group'] = 'Age: 25';
$expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new \stdClass();
$expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new ResultRow();
$expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_name = 'John';
$expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_job = 'Singer';
$expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_age = '25';
$expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_id = '1';
$expected['Job: Singer']['rows']['Age: 27'] = array();
$expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27';
$expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new \stdClass();
$expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new ResultRow();
$expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_name = 'George';
$expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_job = 'Singer';
$expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_age = '27';
@ -165,7 +166,7 @@ class StyleTest extends ViewTestBase {
$expected['Job: Drummer']['group'] = 'Job: Drummer';
$expected['Job: Drummer']['rows']['Age: 28'] = array();
$expected['Job: Drummer']['rows']['Age: 28']['group'] = 'Age: 28';
$expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new \stdClass();
$expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new ResultRow();
$expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_name = 'Ringo';
$expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_job = 'Drummer';
$expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_age = '28';