merged entityreference submodule

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-19 15:18:46 +02:00
37 changed files with 5394 additions and 0 deletions

View File

@@ -0,0 +1,136 @@
<?php
/**
* @file
* Views integration for Entity Reference.
*/
/**
* Implements hook_field_views_data().
*/
function entityreference_field_views_data($field) {
$data = field_views_field_default_views_data($field);
$entity_info = entity_get_info($field['settings']['target_type']);
foreach ($data as $table_name => $table_data) {
if (isset($entity_info['base table'])) {
$entity = $entity_info['label'];
if ($entity == t('Node')) {
$entity = t('Content');
}
$field_name = $field['field_name'] . '_target_id';
$parameters = array('@entity' => $entity, '!field_name' => $field['field_name']);
$data[$table_name][$field_name]['relationship'] = array(
'handler' => 'views_handler_relationship',
'base' => $entity_info['base table'],
'base field' => $entity_info['entity keys']['id'],
'label' => t('@entity entity referenced from !field_name', $parameters),
'group' => t('Entity Reference'),
'title' => t('Referenced Entity'),
'help' => t('A bridge to the @entity entity that is referenced via !field_name', $parameters),
);
}
}
// Invoke the behaviors to allow them to change the properties.
foreach (entityreference_get_behavior_handlers($field) as $handler) {
$handler->views_data_alter($data, $field);
}
return $data;
}
/**
* Options callback for Views handler views_handler_filter_in_operator.
*/
function entityreference_views_handler_options_list($field_name) {
$field = field_info_field($field_name);
return entityreference_options_list($field);
}
/**
* Implements hook_field_views_data_views_data_alter().
*
* Views integration to provide reverse relationships on entityreference fields.
*/
function entityreference_field_views_data_views_data_alter(&$data, $field) {
foreach ($field['bundles'] as $entity_type => $bundles) {
$target_entity_info = entity_get_info($field['settings']['target_type']);
if (isset($target_entity_info['base table'])) {
$entity_info = entity_get_info($entity_type);
$entity = $entity_info['label'];
if ($entity == t('Node')) {
$entity = t('Content');
}
$target_entity = $target_entity_info['label'];
if ($target_entity == t('Node')) {
$target_entity = t('Content');
}
$pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type;
$replacements = array('@entity' => $entity, '@target_entity' => $target_entity, '!field_name' => $field['field_name']);
$data[$target_entity_info['base table']][$pseudo_field_name]['relationship'] = array(
'handler' => 'views_handler_relationship_entity_reverse',
'field_name' => $field['field_name'],
'field table' => _field_sql_storage_tablename($field),
'field field' => $field['field_name'] . '_target_id',
'base' => $entity_info['base table'],
'base field' => $entity_info['entity keys']['id'],
'label' => t('@entity referencing @target_entity from !field_name', $replacements),
'group' => t('Entity Reference'),
'title' => t('Referencing entity'),
'help' => t('A bridge to the @entity entity that is referencing @target_entity via !field_name', $replacements),
);
}
}
}
/**
* Implements hook_views_plugins().
*/
function entityreference_views_plugins() {
$plugins = array(
'display' => array(
'entityreference' => array(
'title' => t('Entity Reference'),
'admin' => t('Entity Reference Source'),
'help' => 'Selects referenceable entities for an entity reference field',
'handler' => 'entityreference_plugin_display',
'uses hook menu' => FALSE,
'use ajax' => FALSE,
'use pager' => FALSE,
'accept attachments' => FALSE,
// Custom property, used with views_get_applicable_views() to retrieve
// all views with a 'Entity Reference' display.
'entityreference display' => TRUE,
),
),
'style' => array(
'entityreference_style' => array(
'title' => t('Entity Reference list'),
'help' => 'Returns results as a PHP array of labels and rendered rows.',
'handler' => 'entityreference_plugin_style',
'theme' => 'views_view_unformatted',
'uses row plugin' => TRUE,
'uses fields' => TRUE,
'uses options' => TRUE,
'type' => 'entityreference',
'even empty' => TRUE,
),
),
'row' => array(
'entityreference_fields' => array(
'title' => t('Inline fields'),
'help' => t('Displays the fields with an optional template.'),
'handler' => 'entityreference_plugin_row_fields',
'theme' => 'views_view_fields',
'theme path' => drupal_get_path('module', 'views') . '/theme',
'theme file' => 'theme.inc',
'uses fields' => TRUE,
'uses options' => TRUE,
'type' => 'entityreference',
),
),
);
return $plugins;
}

View File

@@ -0,0 +1,123 @@
<?php
/**
* @file
* Handler for entityreference_plugin_display.
*/
class entityreference_plugin_display extends views_plugin_display {
function option_definition() {
$options = parent::option_definition();
// Force the style plugin to 'entityreference_style' and the row plugin to
// 'fields'.
$options['style_plugin']['default'] = 'entityreference_style';
$options['defaults']['default']['style_plugin'] = FALSE;
$options['defaults']['default']['style_options'] = FALSE;
$options['row_plugin']['default'] = 'entityreference_fields';
$options['defaults']['default']['row_plugin'] = FALSE;
$options['defaults']['default']['row_options'] = FALSE;
// Set the display title to an empty string (not used in this display type).
$options['title']['default'] = '';
$options['defaults']['default']['title'] = FALSE;
return $options;
}
function get_style_type() {
return 'entityreference';
}
function execute() {
return $this->view->render($this->display->id);
}
function render() {
if (!empty($this->view->result) || !empty($this->view->style_plugin->definition['even empty'])) {
return $this->view->style_plugin->render($this->view->result);
}
return '';
}
function uses_exposed() {
return FALSE;
}
function query() {
$options = $this->get_option('entityreference_options');
// Play nice with Views UI 'preview' : if the view is not executed through
// EntityReference_SelectionHandler_Views::getReferencableEntities(),
// don't alter the query.
if (empty($options)) {
return;
}
// Make sure the id field is included in the results, and save its alias
// so that references_plugin_style can retrieve it.
$this->id_field_alias = $id_field = $this->view->query->add_field($this->view->base_table, $this->view->base_field);
if (strpos($id_field, '.') === FALSE) {
$id_field = $this->view->base_table . '.' . $this->id_field_alias;
}
// Restrict the autocomplete options based on what's been typed already.
if (isset($options['match'])) {
$style_options = $this->get_option('style_options');
$value = db_like($options['match']) . '%';
if ($options['match_operator'] != 'STARTS_WITH') {
$value = '%' . $value;
}
// Multiple search fields are OR'd together
$conditions = db_or();
// Build the condition using the selected search fields
foreach ($style_options['search_fields'] as $field_alias) {
if (!empty($field_alias)) {
// Get the table and field names for the checked field
if (empty($this->view->field[$field_alias]->field_info))
$field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
else {
$this->view->query->add_field($this->view->field[$field_alias]->options['table'], $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->options['field'], array());
$field = $this->view->query->fields[$this->view->field[$field_alias]->options['field']];
}
// Add an OR condition for the field
$conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
}
}
$this->view->query->add_where(NULL, $conditions);
}
// Add an IN condition for validation.
if (!empty($options['ids'])) {
$this->view->query->add_where(NULL, $id_field, $options['ids']);
}
$this->view->set_items_per_page($options['limit']);
}
/**
* Extend the default validation.
*/
function validate() {
$errors = parent::validate();
// Verify that search fields are set up.
$style_options = $this->get_option('style_options');
if (!isset($style_options['search_fields'])) {
$errors[] = t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title));
}
else {
// Verify that the search fields used actually exist.
//$fields = array_keys($this->view->get_items('field'));
$fields = array_keys($this->handlers['field']);
foreach ($style_options['search_fields'] as $field_alias => $enabled) {
if ($enabled && !in_array($field_alias, $fields)) {
$errors[] = t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title, '%field' => $field_alias));
}
}
}
return $errors;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* @file
* Handler for entityreference_plugin_row_fields.
*/
class entityreference_plugin_row_fields extends views_plugin_row_fields {
function option_definition() {
$options = parent::option_definition();
$options['separator'] = array('default' => '-');
return $options;
}
/**
* Provide a form for setting options.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
// Expand the description of the 'Inline field' checkboxes.
$form['inline']['#description'] .= '<br />' . t("<strong>Note:</strong> In 'Entity Reference' displays, all fields will be displayed inline unless an explicit selection of inline fields is made here." );
}
function pre_render($row) {
// Force all fields to be inline by default.
if (empty($this->options['inline'])) {
$fields = $this->view->get_items('field', $this->display->id);
$this->options['inline'] = drupal_map_assoc(array_keys($fields));
}
return parent::pre_render($row);
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* @file
* Handler for entityreference_plugin_style.
*/
class entityreference_plugin_style extends views_plugin_style {
function option_definition() {
$options = parent::option_definition();
$options['search_fields'] = array('default' => NULL);
return $options;
}
// Create the options form.
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$options = array();
if (isset($form['grouping'])) {
$options = $form['grouping'][0]['field']['#options'];
unset($options['']);
$form['search_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Search fields'),
'#options' => $options,
'#required' => TRUE,
'#default_value' => $this->options['search_fields'],
'#description' => t('Select the field(s) that will be searched when using the autocomplete widget.'),
'#weight' => -3,
);
}
}
function render() {
$options = $this->display->handler->get_option('entityreference_options');
// Play nice with Views UI 'preview' : if the view is not executed through
// EntityReference_SelectionHandler_Views::getReferencableEntities(), just
// display the HTML.
if (empty($options)) {
return parent::render();
}
// Group the rows according to the grouping field, if specified.
$sets = $this->render_grouping($this->view->result, $this->options['grouping']);
// Grab the alias of the 'id' field added by entityreference_plugin_display.
$id_field_alias = $this->display->handler->id_field_alias;
// @todo We don't display grouping info for now. Could be useful for select
// widget, though.
$results = array();
$this->view->row_index = 0;
foreach ($sets as $records) {
foreach ($records as $values) {
// Sanitize html, remove line breaks and extra whitespace.
$results[$values->{$id_field_alias}] = filter_xss_admin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $this->row_plugin->render($values))));
$this->view->row_index++;
}
}
unset($this->view->row_index);
return $results;
}
}