summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordereine2012-01-24 12:27:33 (GMT)
committerfago2012-01-24 12:27:33 (GMT)
commitaf04c9f4a739e254248c50c5b845792d70eee807 (patch)
treeba183752eaaf6af40095227973e3366566c4b644
parent82b88ad57ee36f7c1a8beb06823b65b9eb640acf (diff)
Issue #1368478 by dereine: Provide a views entity-render area handler.
-rw-r--r--entity.info1
-rw-r--r--views/entity.views.inc13
-rw-r--r--views/handlers/entity_views_handler_area_entity.inc111
3 files changed, 125 insertions, 0 deletions
diff --git a/entity.info b/entity.info
index 12cd58f..f658098 100644
--- a/entity.info
+++ b/entity.info
@@ -11,6 +11,7 @@ files[] = includes/entity.ui.inc
files[] = includes/entity.wrapper.inc
files[] = views/entity.views.inc
files[] = views/handlers/entity_views_field_handler_helper.inc
+files[] = views/handlers/entity_views_handler_area_entity.inc
files[] = views/handlers/entity_views_handler_field_boolean.inc
files[] = views/handlers/entity_views_handler_field_date.inc
files[] = views/handlers/entity_views_handler_field_duration.inc
diff --git a/views/entity.views.inc b/views/entity.views.inc
index 596d721..0aaa39c 100644
--- a/views/entity.views.inc
+++ b/views/entity.views.inc
@@ -63,6 +63,19 @@ function entity_views_data() {
}
}
+ $data['entity__global']['table']['group'] = t('Entity');
+ $data['entity__global']['table']['join'] = array(
+ // #global let's it appear all the time.
+ '#global' => array(),
+ );
+ $data['entity__global']['entity'] = array(
+ 'title' => t('Rendered entity'),
+ 'help' => t('Displays a single chosen entity.'),
+ 'area' => array(
+ 'handler' => 'entity_views_handler_area_entity',
+ ),
+ );
+
return $data;
}
diff --git a/views/handlers/entity_views_handler_area_entity.inc b/views/handlers/entity_views_handler_area_entity.inc
new file mode 100644
index 0000000..de5863b
--- /dev/null
+++ b/views/handlers/entity_views_handler_area_entity.inc
@@ -0,0 +1,111 @@
+<?php
+/**
+ * @file
+ * Renders a full entity in a views area.
+ */
+
+class entity_views_handler_area_entity extends views_handler_area {
+ public function option_definition() {
+ $options = parent::option_definition();
+ $options['entity_type'] = array('default' => 'node');
+ $options['entity_id'] = array('default' => '');
+ $options['view_mode'] = array('default' => 'full');
+ return $options;
+ }
+
+ function options_form(&$form, &$form_state) {
+ parent::options_form($form, $form_state);
+
+ $entity_type_options = array();
+ foreach (entity_get_info() as $entity_type => $entity_info) {
+ $entity_type_options[$entity_type] = $entity_info['label'];
+ }
+
+ $entity_type = $this->options['entity_type'];
+
+ $form['entity_type'] = array(
+ '#type' => 'select',
+ '#title' => t('Entity type'),
+ '#options' => $entity_type_options,
+ '#description' => t('Choose the entity type you want to display in the area.'),
+ '#default_value' => $entity_type,
+ '#ajax' => array(
+ 'path' => views_ui_build_form_url($form_state),
+ ),
+ '#submit' => array('views_ui_config_item_form_submit_temporary'),
+ '#executes_submit_callback' => TRUE,
+ );
+
+ $form['entity_id'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Entity id'),
+ '#description' => t('Choose the entity you want to display in the area.'),
+ '#default_value' => $this->options['entity_id'],
+ );
+
+ if ($entity_type) {
+ $entity_info = entity_get_info($entity_type);
+ $options = array();
+ if (!empty($entity_info['view modes'])) {
+ foreach ($entity_info['view modes'] as $mode => $settings) {
+ $options[$mode] = $settings['label'];
+ }
+ }
+
+ if (count($options) > 1) {
+ $form['view_mode'] = array(
+ '#type' => 'select',
+ '#options' => $options,
+ '#title' => t('View mode'),
+ '#default_value' => $this->options['view_mode'],
+ );
+ }
+ else {
+ $form['view_mode_info'] = array(
+ '#type' => 'item',
+ '#title' => t('View mode'),
+ '#description' => t('Only one view mode is available for this entity type.'),
+ '#markup' => $options ? current($options) : t('Default'),
+ );
+ $form['view_mode'] = array(
+ '#type' => 'value',
+ '#value' => $options ? key($options) : 'default',
+ );
+ }
+ }
+ return $form;
+ }
+
+ public function admin_summary() {
+ $label = parent::admin_summary();
+ if (!empty($this->options['entity_id'])) {
+ return t('@label @entity_type:@entity_id', array(
+ '@label' => $label,
+ '@entity_type' => $this->options['entity_type'],
+ '@entity_id' => $this->options['entity_id'],
+ ));
+ }
+ }
+
+ public function render($empty = FALSE) {
+ if (!$empty || !empty($this->options['empty'])) {
+ return $this->render_entity($this->options['entity_type'], $this->options['entity_id'], $this->options['view_mode']);
+ }
+ return '';
+ }
+
+ /**
+ * Render an entity using the view mode.
+ */
+ public function render_entity($entity_type, $entity_id, $view_mode) {
+ if (!empty($entity_type) && !empty($entity_id) && !empty($view_mode)) {
+ $entities = entity_load($entity_type, array($entity_id));
+ $render = entity_view($entity_type, $entities, $view_mode);
+ $render_entity = reset($render);
+ return drupal_render($render_entity);
+ }
+ else {
+ return '';
+ }
+ }
+}