Commit e29298cc authored by fago's avatar fago

#1209678 patch by mh86, Damien Tournoud, fago: Add a Views field handler to...

 #1209678 patch by mh86, Damien Tournoud, fago: Add a Views field handler to display rendered entities. Requires latest Views dev version to appear.
parent 9b58bf9b
......@@ -22,4 +22,4 @@ files[] = views/handlers/entity_views_handler_field_text.inc
files[] = views/handlers/entity_views_handler_field_uri.inc
files[] = views/handlers/entity_views_handler_relationship_by_bundle.inc
files[] = views/handlers/entity_views_handler_relationship.inc
files[] = views/plugins/entity_plugin_row_entity_view.inc
files[] = views/plugins/entity_views_plugin_row_entity_view.inc
\ No newline at end of file
......@@ -45,6 +45,24 @@ function entity_views_data() {
}
}
// Expose generally usable entity-related fields.
foreach (entity_get_info() as $entity_type => $info) {
if (entity_type_supports($entity_type, 'view')) {
// Expose a field allowing to display the rendered entity.
$data['views_entity_' . $entity_type]['rendered_entity'] = array(
'title' => t('Rendered @entity-type', array('@entity-type' => $info['label'])),
'help' => t('The @entity-type of the current relationship rendered using a view mode.', array('@entity-type' => $info['label'])),
'field' => array(
'handler' => 'entity_views_handler_field_entity',
'type' => $entity_type,
// The EntityFieldHandlerHelper class treats the 'entity object' field
// as special case for loading the base entity.
'real field' => 'entity object',
),
);
}
}
return $data;
}
......@@ -267,17 +285,18 @@ function _entity_views_field_identifier($field, array $table) {
*/
function entity_views_plugins() {
foreach (views_fetch_data() as $table => $data) {
if (!empty($data['table']['entity type'])) {
if (!empty($data['table']['entity type']) && !empty($data['table']['base'])) {
$base_tables[] = $table;
}
}
if (!empty($base_tables)) {
return array(
'module' => 'entity',
'row' => array(
'entity' => array(
'title' => t('Entity'),
'help' => t('Displays a single entity in a specific view mode (e.g. teaser).'),
'handler' => 'entity_plugin_row_entity_view',
'title' => t('Rendered entity'),
'help' => t('Renders a single entity in a specific view mode (e.g. teaser).'),
'handler' => 'entity_views_plugin_row_entity_view',
'uses fields' => FALSE,
'uses options' => TRUE,
'type' => 'normal',
......
......@@ -279,6 +279,9 @@ class EntityFieldHandlerHelper {
$field = isset($field) ? $field : $handler->base_field;
$selector = self::construct_property_selector($handler);
$selector = $selector ? "$selector:$field" : $field;
if (!isset($values->_entity_properties)) {
$values->_entity_properties = array();
}
if (!array_key_exists($selector, $values->_entity_properties)) {
if (!isset($handler->wrappers[$handler->view->row_index])) {
$values->_entity_properties[$selector] = $default;
......
......@@ -21,6 +21,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity {
* Overridden to add the field for the entity ID (if necessary).
*/
public function query() {
parent::query();
EntityFieldHandlerHelper::query($this);
}
......@@ -82,8 +83,8 @@ class entity_views_handler_field_entity extends views_handler_field_entity {
'#dependency' => array('edit-options-display' => array('label', 'id')),
);
// Stolen from entity_plugin_row_entity_view.
$entity_info = entity_get_info($this->definition['entity type']);
// Stolen from entity_views_plugin_row_entity_view.
$entity_info = entity_get_info($this->entity_type);
$options = array();
if (!empty($entity_info['view modes'])) {
foreach ($entity_info['view modes'] as $mode => $settings) {
......@@ -122,7 +123,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity {
* object.
*/
public function render_entity_link($entity, $values) {
$type = $this->definition['entity type'];
$type = $this->entity_type;
if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
$entity = entity_load_single($type, $entity);
}
......@@ -143,7 +144,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity {
* Render a single field value.
*/
public function render_single_value($entity, $values) {
$type = $this->definition['entity type'];
$type = $this->entity_type;
if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
$entity = entity_load_single($type, $entity);
}
......
......@@ -8,19 +8,16 @@
/**
* Plugin class for displaying Views results with entity_view.
*/
class entity_plugin_row_entity_view extends views_plugin_row {
/**
* The entity type of the entity being displayed.
*/
protected $entity_type, $skip_load, $entities = array();
class entity_views_plugin_row_entity_view extends views_plugin_row {
protected $entity_type, $entities;
public function init(&$view, &$display, $options = NULL) {
parent::init($view, $display, $options);
// Initialize the entity-type used.
$table_data = views_fetch_data($this->view->base_table);
$this->entity_type = $table_data['table']['entity type'];
$this->skip_load = !empty($table_data['table']['skip entity load']);
// Set base table and field information as used by views_plugin_row to
// select the entity id if used with default query class.
$info = entity_get_info($this->entity_type);
......@@ -67,44 +64,32 @@ class entity_plugin_row_entity_view extends views_plugin_row {
'#value' => $options ? key($options) : 'default',
);
}
return $form;
}
/**
* Use entity_load() to load all entities at once if they aren't loaded yet.
*/
public function pre_render($results) {
$this->entities = array();
foreach ($results as $result) {
if (!isset($result->entity)) {
$id = entity_id($this->entity_type, $result);
$this->entities[$id] = $result;
}
else {
$id = (is_object($result->entity) ? entity_id($this->entity_type, $result->entity) : $result->entity);
$this->entities[$id] = $result->entity;
}
// Force loading in case there is no object at all.
if (!empty($id) && $id == $this->entities[$id]) {
$this->skip_load = FALSE;
}
public function pre_render(&$values) {
if (!empty($values)) {
list($this->entity_type, $this->entities) = $this->view->query->get_result_entities($values, !empty($this->relationship) ? $this->relationship : NULL, isset($this->field_alias) ? $this->field_alias : NULL);
}
if (!$this->skip_load) {
$this->entities = entity_load($this->entity_type, array_keys($this->entities));
// Render the entities.
if ($this->entities) {
$render = entity_view($this->entity_type, $this->entities, $this->options['view_mode']);
// Remove the first level of the render array.
$this->rendered_content = reset($render);
}
}
public function render($result) {
if (!isset($result->entity)) {
$id = entity_id($this->entity_type, $result);
}
else {
$id = (is_object($result->entity) ? entity_id($this->entity_type, $result->entity) : $result->entity);
}
if (isset($id) && isset($this->entities[$id])) {
$content = entity_view($this->entity_type, array($id => $this->entities[$id]), $this->options['view_mode']);
return drupal_render($content);
/**
* Overridden to return the entity object.
*/
function get_value($values, $field = NULL) {
return isset($this->entities[$this->view->row_index]) ? $this->entities[$this->view->row_index] : FALSE;
}
public function render($values) {
if ($entity = $this->get_value($values)) {
$render = $this->rendered_content[entity_id($this->entity_type, $entity)];
return drupal_render($render);
}
}
}
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