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 ...@@ -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_field_uri.inc
files[] = views/handlers/entity_views_handler_relationship_by_bundle.inc files[] = views/handlers/entity_views_handler_relationship_by_bundle.inc
files[] = views/handlers/entity_views_handler_relationship.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() { ...@@ -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; return $data;
} }
...@@ -267,17 +285,18 @@ function _entity_views_field_identifier($field, array $table) { ...@@ -267,17 +285,18 @@ function _entity_views_field_identifier($field, array $table) {
*/ */
function entity_views_plugins() { function entity_views_plugins() {
foreach (views_fetch_data() as $table => $data) { 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; $base_tables[] = $table;
} }
} }
if (!empty($base_tables)) { if (!empty($base_tables)) {
return array( return array(
'module' => 'entity',
'row' => array( 'row' => array(
'entity' => array( 'entity' => array(
'title' => t('Entity'), 'title' => t('Rendered entity'),
'help' => t('Displays a single entity in a specific view mode (e.g. teaser).'), 'help' => t('Renders a single entity in a specific view mode (e.g. teaser).'),
'handler' => 'entity_plugin_row_entity_view', 'handler' => 'entity_views_plugin_row_entity_view',
'uses fields' => FALSE, 'uses fields' => FALSE,
'uses options' => TRUE, 'uses options' => TRUE,
'type' => 'normal', 'type' => 'normal',
......
...@@ -279,6 +279,9 @@ class EntityFieldHandlerHelper { ...@@ -279,6 +279,9 @@ class EntityFieldHandlerHelper {
$field = isset($field) ? $field : $handler->base_field; $field = isset($field) ? $field : $handler->base_field;
$selector = self::construct_property_selector($handler); $selector = self::construct_property_selector($handler);
$selector = $selector ? "$selector:$field" : $field; $selector = $selector ? "$selector:$field" : $field;
if (!isset($values->_entity_properties)) {
$values->_entity_properties = array();
}
if (!array_key_exists($selector, $values->_entity_properties)) { if (!array_key_exists($selector, $values->_entity_properties)) {
if (!isset($handler->wrappers[$handler->view->row_index])) { if (!isset($handler->wrappers[$handler->view->row_index])) {
$values->_entity_properties[$selector] = $default; $values->_entity_properties[$selector] = $default;
......
...@@ -21,6 +21,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity { ...@@ -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). * Overridden to add the field for the entity ID (if necessary).
*/ */
public function query() { public function query() {
parent::query();
EntityFieldHandlerHelper::query($this); EntityFieldHandlerHelper::query($this);
} }
...@@ -82,8 +83,8 @@ class entity_views_handler_field_entity extends views_handler_field_entity { ...@@ -82,8 +83,8 @@ class entity_views_handler_field_entity extends views_handler_field_entity {
'#dependency' => array('edit-options-display' => array('label', 'id')), '#dependency' => array('edit-options-display' => array('label', 'id')),
); );
// Stolen from entity_plugin_row_entity_view. // Stolen from entity_views_plugin_row_entity_view.
$entity_info = entity_get_info($this->definition['entity type']); $entity_info = entity_get_info($this->entity_type);
$options = array(); $options = array();
if (!empty($entity_info['view modes'])) { if (!empty($entity_info['view modes'])) {
foreach ($entity_info['view modes'] as $mode => $settings) { foreach ($entity_info['view modes'] as $mode => $settings) {
...@@ -122,7 +123,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity { ...@@ -122,7 +123,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity {
* object. * object.
*/ */
public function render_entity_link($entity, $values) { public function render_entity_link($entity, $values) {
$type = $this->definition['entity type']; $type = $this->entity_type;
if (!is_object($entity) && isset($entity) && $entity !== FALSE) { if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
$entity = entity_load_single($type, $entity); $entity = entity_load_single($type, $entity);
} }
...@@ -143,7 +144,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity { ...@@ -143,7 +144,7 @@ class entity_views_handler_field_entity extends views_handler_field_entity {
* Render a single field value. * Render a single field value.
*/ */
public function render_single_value($entity, $values) { public function render_single_value($entity, $values) {
$type = $this->definition['entity type']; $type = $this->entity_type;
if (!is_object($entity) && isset($entity) && $entity !== FALSE) { if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
$entity = entity_load_single($type, $entity); $entity = entity_load_single($type, $entity);
} }
......
...@@ -8,19 +8,16 @@ ...@@ -8,19 +8,16 @@
/** /**
* Plugin class for displaying Views results with entity_view. * Plugin class for displaying Views results with entity_view.
*/ */
class entity_plugin_row_entity_view extends views_plugin_row { class entity_views_plugin_row_entity_view extends views_plugin_row {
/**
* The entity type of the entity being displayed. protected $entity_type, $entities;
*/
protected $entity_type, $skip_load, $entities = array();
public function init(&$view, &$display, $options = NULL) { public function init(&$view, &$display, $options = NULL) {
parent::init($view, $display, $options); parent::init($view, $display, $options);
// Initialize the entity-type used.
$table_data = views_fetch_data($this->view->base_table); $table_data = views_fetch_data($this->view->base_table);
$this->entity_type = $table_data['table']['entity type']; $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 // Set base table and field information as used by views_plugin_row to
// select the entity id if used with default query class. // select the entity id if used with default query class.
$info = entity_get_info($this->entity_type); $info = entity_get_info($this->entity_type);
...@@ -67,44 +64,32 @@ class entity_plugin_row_entity_view extends views_plugin_row { ...@@ -67,44 +64,32 @@ class entity_plugin_row_entity_view extends views_plugin_row {
'#value' => $options ? key($options) : 'default', '#value' => $options ? key($options) : 'default',
); );
} }
return $form; return $form;
} }
/** public function pre_render(&$values) {
* Use entity_load() to load all entities at once if they aren't loaded yet. 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);
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;
}
} }
if (!$this->skip_load) { // Render the entities.
$this->entities = entity_load($this->entity_type, array_keys($this->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)) { * Overridden to return the entity object.
$id = entity_id($this->entity_type, $result); */
} function get_value($values, $field = NULL) {
else { return isset($this->entities[$this->view->row_index]) ? $this->entities[$this->view->row_index] : FALSE;
$id = (is_object($result->entity) ? entity_id($this->entity_type, $result->entity) : $result->entity); }
}
if (isset($id) && isset($this->entities[$id])) { public function render($values) {
$content = entity_view($this->entity_type, array($id => $this->entities[$id]), $this->options['view_mode']); if ($entity = $this->get_value($values)) {
return drupal_render($content); $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