123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723 |
- <?php
- function entity_views_data() {
- $data = array();
- foreach (entity_crud_get_info() as $type => $info) {
-
-
- if (!isset($info['views controller class'])) {
- $info['views controller class'] = isset($info['module']) && !module_hook($info['module'], 'views_data') ? 'EntityDefaultViewsController' : FALSE;
- }
- if ($info['views controller class']) {
- $controller = new $info['views controller class']($type);
-
-
- foreach ($controller->views_data() as $table => $table_data) {
- $data += array($table => array());
- $data[$table] = array_merge($data[$table], $table_data);
- }
- }
- }
-
- foreach (entity_get_info() as $type => $info) {
- $table = entity_views_table_definition($type);
- if ($table) {
- $data['entity_' . $type] = $table;
- }
-
- $data['views_entity_' . $type] = array();
- foreach (entity_get_all_property_info($type) as $key => $property) {
- if (!empty($property['entity views field'])) {
- entity_views_field_definition($key, $property, $data['views_entity_' . $type]);
- }
- }
- }
-
- foreach (entity_get_info() as $entity_type => $info) {
- if (entity_type_supports($entity_type, 'view')) {
-
- $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,
-
-
- 'real field' => 'entity object',
- ),
- );
- }
- }
- $data['entity__global']['table']['group'] = t('Entity');
- $data['entity__global']['table']['join'] = array(
-
- '#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;
- }
- function entity_views_table_definition($type, $exclude = TRUE) {
-
-
- $tables = &drupal_static(__FUNCTION__, array());
- if (!isset($tables[$type])) {
-
-
-
- if (!class_exists('EntityFieldHandlerHelper')) {
- module_load_include('inc', 'entity', 'views/handlers/entity_views_field_handler_helper');
- }
- $info = entity_get_info($type);
- $tables[$type]['table'] = array(
- 'group' => $info['label'],
- 'entity type' => $type,
- );
- foreach (entity_get_all_property_info($type) as $key => $property) {
- if (!$exclude || empty($property['entity views field'])) {
- entity_views_field_definition($key, $property, $tables[$type]);
- }
- }
- }
- return $tables[$type];
- }
- function entity_views_field_definition($field, array $property_info, array &$table, $title_prefix = '') {
- $additional = array();
- $additional_field = array();
-
-
- $key = _entity_views_field_identifier($field, $table);
- if ($key != $field) {
- $additional['real field'] = $field;
- }
- $field_name = EntityFieldHandlerHelper::get_selector_field_name($field);
- $field_handlers = entity_views_get_field_handlers();
- $property_info += entity_property_info_defaults();
- $type = entity_property_extract_innermost_type($property_info['type']);
- $title = $title_prefix . $property_info['label'];
- if ($info = entity_get_info($type)) {
- $additional['relationship'] = array(
- 'handler' => $field_handlers['relationship'],
- 'base' => 'entity_' . $type,
- 'base field' => $info['entity keys']['id'],
- 'relationship field' => $field,
- 'label' => $title,
- );
- if ($property_info['type'] != $type) {
-
- $additional['relationship']['multiple'] = TRUE;
- }
-
-
- if (!isset($field_handlers[$type])) {
- $type = 'entity';
- }
- }
- elseif (!empty($property_info['field'])) {
- $type = 'field';
-
- $additional_field['field_name'] = $field_name;
- $additional_field['entity_tables'] = array();
- $additional_field['entity type'] = $table['table']['entity type'];
- $additional_field['is revision'] = FALSE;
- }
-
- elseif (isset($property_info['options list']) && is_callable($property_info['options list'])) {
-
- $type = 'options';
- $additional_field['options callback'] = array(
- 'function' => $property_info['options list'],
- 'info' => $property_info,
- );
- }
- elseif ($type == 'decimal') {
- $additional_field['float'] = TRUE;
- }
- if (isset($field_handlers[$type])) {
- $table += array($key => array());
- $table[$key] += array(
- 'title' => $title,
- 'help' => empty($property_info['description']) ? t('(No information available)') : $property_info['description'],
- 'field' => array(),
- );
- $table[$key]['field'] += array(
- 'handler' => $field_handlers[$type],
- 'type' => $property_info['type'],
- );
- $table[$key] += $additional;
- $table[$key]['field'] += $additional_field;
- }
- if (!empty($property_info['property info'])) {
- foreach ($property_info['property info'] as $nested_key => $nested_property) {
- entity_views_field_definition($field . ':' . $nested_key, $nested_property, $table, $title . ' » ');
- }
- }
- }
- function entity_views_get_field_handlers() {
- $field_handlers = drupal_static(__FUNCTION__);
- if (!isset($field_handlers)) {
-
- $field_handlers = array(
- 'text' => 'entity_views_handler_field_text',
- 'token' => 'entity_views_handler_field_text',
- 'integer' => 'entity_views_handler_field_numeric',
- 'decimal' => 'entity_views_handler_field_numeric',
- 'date' => 'entity_views_handler_field_date',
- 'duration' => 'entity_views_handler_field_duration',
- 'boolean' => 'entity_views_handler_field_boolean',
- 'uri' => 'entity_views_handler_field_uri',
- 'options' => 'entity_views_handler_field_options',
- 'field' => 'entity_views_handler_field_field',
- 'entity' => 'entity_views_handler_field_entity',
- 'relationship' => 'entity_views_handler_relationship',
- );
- drupal_alter('entity_views_field_handlers', $field_handlers);
- }
- return $field_handlers;
- }
- function _entity_views_field_identifier($field, array $table) {
- $key = $base = preg_replace('/[^a-zA-Z0-9]+/S', '_', $field);
- $i = 0;
-
-
-
-
-
-
-
-
- while ($key == 'table' || (isset($table[$key]) && (isset($table[$key]['real field']) ? $table[$key]['real field'] : $key) != $field)) {
- $key = $base . '_' . ++$i;
- }
- return $key;
- }
- function entity_views_plugins() {
-
-
- $data = views_cache_get('entity_base_tables', TRUE);
- if (!empty($data->data)) {
- $base_tables = $data->data;
- }
- else {
- $base_tables = array();
- foreach (views_fetch_data() as $table => $data) {
- if (!empty($data['table']['entity type']) && !empty($data['table']['base'])) {
- $base_tables[] = $table;
- }
- }
- views_cache_set('entity_base_tables', $base_tables, TRUE);
- }
- if (!empty($base_tables)) {
- return array(
- 'module' => 'entity',
- 'row' => array(
- 'entity' => array(
- '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',
- 'base' => $base_tables,
- ),
- ),
- );
- }
- }
- class EntityDefaultViewsController {
- protected $type, $info, $relationships;
- public function __construct($type) {
- $this->type = $type;
- $this->info = entity_get_info($type);
- }
-
- public function views_data() {
- $data = array();
- $this->relationships = array();
- if (!empty($this->info['base table'])) {
- $table = $this->info['base table'];
-
-
- $data[$table]['table']['group'] = drupal_ucfirst($this->info['label']);
- $data[$table]['table']['entity type'] = $this->type;
-
- $label = isset($this->info['plural label']) ? $this->info['plural label'] : $this->info['label'];
- $data[$table]['table']['base'] = array(
- 'field' => $this->info['entity keys']['id'],
- 'access query tag' => $this->type . '_access',
- 'title' => drupal_ucfirst($label),
- 'help' => isset($this->info['description']) ? $this->info['description'] : '',
- );
- $data[$table]['table']['entity type'] = $this->type;
- $data[$table] += $this->schema_fields();
-
- $data += $this->relationships;
- }
- if (!empty($this->info['revision table']) && !empty($this->info['entity keys']['revision'])) {
- $revision_table = $this->info['revision table'];
- $data[$table]['table']['default_relationship'] = array(
- $revision_table => array(
- 'table' => $revision_table,
- 'field' => $this->info['entity keys']['revision'],
- ),
- );
-
-
- $data[$revision_table]['table']['group'] = drupal_ucfirst($this->info['label']) . ' ' . t('Revisions');
- $data[$revision_table]['table']['entity type'] = $this->type;
-
- $label = isset($this->info['plural label']) ? $this->info['plural label'] : $this->info['label'];
- $data[$revision_table]['table']['base'] = array(
- 'field' => $this->info['entity keys']['revision'],
- 'access query tag' => $this->type . '_access',
- 'title' => drupal_ucfirst($label) . ' ' . t('Revisions'),
- 'help' => (isset($this->info['description']) ? $this->info['description'] . ' ' : '') . t('Revisions'),
- );
- $data[$revision_table]['table']['entity type'] = $this->type;
- $data[$revision_table] += $this->schema_revision_fields();
-
- $data += $this->relationships;
-
- $data[$revision_table]['table']['join'] = array(
-
- $table => array(
- 'left_field' => $this->info['entity keys']['revision'],
- 'field' => $this->info['entity keys']['revision'],
- ),
- );
- $data[$revision_table]['table']['default_relationship'] = array(
- $table => array(
- 'table' => $table,
- 'field' => $this->info['entity keys']['id'],
- ),
- );
- }
- return $data;
- }
-
- protected function schema_fields() {
- $schema = drupal_get_schema($this->info['base table']);
- $properties = entity_get_property_info($this->type) + array('properties' => array());
- $data = array();
- foreach ($properties['properties'] as $name => $property_info) {
- if (isset($property_info['schema field']) && isset($schema['fields'][$property_info['schema field']])) {
- if ($views_info = $this->map_from_schema_info($name, $schema['fields'][$property_info['schema field']], $property_info)) {
- $data[$name] = $views_info;
- }
- }
- }
- return $data;
- }
-
- protected function schema_revision_fields() {
- $data = array();
- if (!empty($this->info['revision table'])) {
- $schema = drupal_get_schema($this->info['revision table']);
- $properties = entity_get_property_info($this->type) + array('properties' => array());
- foreach ($properties['properties'] as $name => $property_info) {
- if (isset($property_info['schema field']) && isset($schema['fields'][$property_info['schema field']])) {
- if ($views_info = $this->map_from_schema_info($name, $schema['fields'][$property_info['schema field']], $property_info)) {
- $data[$name] = $views_info;
- }
- }
- }
- }
- return $data;
- }
-
- protected function map_from_schema_info($property_name, $schema_field_info, $property_info) {
- $type = isset($property_info['type']) ? $property_info['type'] : 'text';
- $views_field_name = $property_info['schema field'];
- $return = array();
- if (!empty($schema_field_info['serialize'])) {
- return FALSE;
- }
- $description = array(
- 'title' => $property_info['label'],
- 'help' => isset($property_info['description']) ? $property_info['description'] : NULL,
- );
-
- if (($info = entity_get_info($type)) && !empty($info['base table'])) {
-
- $label_lowercase = drupal_strtolower($this->info['label'][0]) . drupal_substr($this->info['label'], 1);
- $property_label_lowercase = drupal_strtolower($property_info['label'][0]) . drupal_substr($property_info['label'], 1);
-
-
-
- $name = !isset($this->relationships[$info['base table']][$this->info['base table']]) ? $this->info['base table'] : $this->info['base table'] . '_' . $views_field_name;
- $this->relationships[$info['base table']][$name] = array(
- 'title' => $this->info['label'],
- 'help' => t("Associated @label via the @label's @property.", array('@label' => $label_lowercase, '@property' => $property_label_lowercase)),
- 'relationship' => array(
- 'label' => $this->info['label'],
- 'handler' => $this->getRelationshipHandlerClass($this->type, $type),
- 'base' => $this->info['base table'],
- 'base field' => $views_field_name,
- 'relationship field' => isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'],
- ),
- );
- $return['relationship'] = array(
- 'label' => drupal_ucfirst($info['label']),
- 'handler' => $this->getRelationshipHandlerClass($type, $this->type),
- 'base' => $info['base table'],
- 'base field' => isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'],
- 'relationship field' => $views_field_name,
- );
-
- $type = isset($info['entity keys']['name']) ? 'token' : 'integer';
-
-
- if ($property_name != $views_field_name) {
- $description['title'] .= ' ' . $views_field_name;
- }
- }
- switch ($type) {
- case 'token':
- case 'text':
- $return += $description + array(
- 'field' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_field',
- 'click sortable' => TRUE,
- ),
- 'sort' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_sort',
- ),
- 'filter' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_filter_string',
- ),
- 'argument' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_argument_string',
- ),
- );
- break;
- case 'decimal':
- case 'integer':
- $return += $description + array(
- 'field' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_field_numeric',
- 'click sortable' => TRUE,
- 'float' => ($type == 'decimal'),
- ),
- 'sort' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_sort',
- ),
- 'filter' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_filter_numeric',
- ),
- 'argument' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_argument_numeric',
- ),
- );
- break;
- case 'date':
- $return += $description + array(
- 'field' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_field_date',
- 'click sortable' => TRUE,
- ),
- 'sort' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_sort_date',
- ),
- 'filter' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_filter_date',
- ),
- 'argument' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_argument_date',
- ),
- );
- break;
- case 'duration':
- $return += $description + array(
- 'field' => array(
- 'real field' => $views_field_name,
- 'handler' => 'entity_views_handler_field_duration',
- 'click sortable' => TRUE,
- ),
- 'sort' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_sort',
- ),
- 'filter' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_filter_numeric',
- ),
- 'argument' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_argument_numeric',
- ),
- );
- break;
- case 'uri':
- $return += $description + array(
- 'field' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_field_url',
- 'click sortable' => TRUE,
- ),
- 'sort' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_sort',
- ),
- 'filter' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_filter_string',
- ),
- 'argument' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_argument_string',
- ),
- );
- break;
- case 'boolean':
- $return += $description + array(
- 'field' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_field_boolean',
- 'click sortable' => TRUE,
- ),
- 'sort' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_sort',
- ),
- 'filter' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_filter_boolean_operator',
- ),
- 'argument' => array(
- 'real field' => $views_field_name,
- 'handler' => 'views_handler_argument_string',
- ),
- );
- break;
- }
-
- if (isset($return['filter']) && !empty($property_info['options list'])) {
- $return['filter']['handler'] = 'views_handler_filter_in_operator';
- $return['filter']['options callback'] = array('EntityDefaultViewsController', 'optionsListCallback');
- $return['filter']['options arguments'] = array($this->type, $property_name, 'view');
- }
-
- if (isset($return['field']) && !empty($property_info['options list']) && class_exists('views_handler_field_machine_name')) {
- $return['field']['handler'] = 'views_handler_field_machine_name';
- $return['field']['options callback'] = array('EntityDefaultViewsController', 'optionsListCallback');
- $return['field']['options arguments'] = array($this->type, $property_name, 'view');
- }
- return $return;
- }
-
- function getRelationshipHandlerClass($entity_type, $left_type) {
-
-
-
- foreach (entity_get_info() as $type => $info) {
-
-
- if (!empty($info['bundle of']) && $info['bundle of'] == $entity_type && $type != $left_type) {
- return 'entity_views_handler_relationship_by_bundle';
- }
- }
- return 'views_handler_relationship';
- }
-
- public static function optionsListCallback($type, $selector, $op = 'view') {
- $wrapper = entity_metadata_wrapper($type, NULL);
- $parts = explode(':', $selector);
- foreach ($parts as $part) {
- $wrapper = $wrapper->get($part);
- }
- return $wrapper->optionsList($op);
- }
- }
|