first import
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Provide handler to replace reference with title.
|
||||
*/
|
||||
class references_handler_argument extends views_handler_argument_numeric {
|
||||
|
||||
/**
|
||||
* Use entity title for % placeholders in argument titles.
|
||||
*/
|
||||
function title_query() {
|
||||
// Use the same table and field than those used for summary lists
|
||||
// ('name table', 'name field').
|
||||
$table_data = views_fetch_data($this->name_table);
|
||||
$table_info = $table_data['table']['join'][$this->table];
|
||||
$table = $table_info['table'];
|
||||
$key_field = $table_info['field'];
|
||||
$title_field = $this->name_field;
|
||||
|
||||
$results = db_select($table, 'base_table')
|
||||
->fields('base_table', array($key_field, $title_field))
|
||||
->condition("base_table.$key_field", $this->value)
|
||||
->execute()
|
||||
// Grab results as 'key => title' array.
|
||||
->fetchAllKeyed();
|
||||
|
||||
// Sanitize titles and put them back in the correct order in an unkeyed
|
||||
// array.
|
||||
$titles = array();
|
||||
foreach ($this->value as $key) {
|
||||
if (isset($results[$key])) {
|
||||
$titles[] = check_plain($results[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $titles;
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Provide relationship handler for reference fields.
|
||||
*/
|
||||
class references_handler_relationship extends views_handler_relationship {
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
$options['delta'] = array('default' => -1);
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a delta selector for multiple fields.
|
||||
*/
|
||||
function options_form(&$form, &$form_state) {
|
||||
parent::options_form($form, $form_state);
|
||||
|
||||
$field = field_info_field($this->definition['field_name']);
|
||||
|
||||
// Only add the delta selector if the field is multiple.
|
||||
if ($field['cardinality']) {
|
||||
$max_delta = ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) ? 10 : $field['cardinality'];
|
||||
|
||||
$options = array('-1' => t('All'));
|
||||
for ($i = 0; $i < $max_delta; $i++) {
|
||||
$options[$i] = $i + 1;
|
||||
}
|
||||
$form['delta'] = array(
|
||||
'#type' => 'select',
|
||||
'#options' => $options,
|
||||
'#default_value' => $this->options['delta'],
|
||||
'#title' => t('Delta'),
|
||||
'#description' => t('The delta allows you to select which item in a multiple value field to key the relationship off of. Select "1" to use the first item, "2" for the second item, and so on. If you select "All", each item in the field will create a new row, which may appear to cause duplicates.'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function ensure_my_table() {
|
||||
$field = field_info_field($this->definition['field_name']);
|
||||
|
||||
if (!isset($this->table_alias)) {
|
||||
$join = $this->get_join();
|
||||
if ($this->options['delta'] != -1 && $field['cardinality']) {
|
||||
$join->extra[] = array(
|
||||
'field' => 'delta',
|
||||
'value' => $this->options['delta'],
|
||||
'numeric' => TRUE,
|
||||
);
|
||||
}
|
||||
$this->table_alias = $this->query->add_table($this->table, $this->relationship, $join);
|
||||
}
|
||||
return $this->table_alias;
|
||||
}
|
||||
}
|
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Handler for references_plugin_display.
|
||||
*/
|
||||
class references_plugin_display extends views_plugin_display {
|
||||
|
||||
function option_definition() {
|
||||
$options = parent::option_definition();
|
||||
|
||||
// Force the style plugin to 'references_style' and the row plugin to
|
||||
// 'fields'.
|
||||
$options['style_plugin']['default'] = 'references_style';
|
||||
$options['defaults']['default']['style_plugin'] = FALSE;
|
||||
$options['defaults']['default']['style_options'] = FALSE;
|
||||
$options['row_plugin']['default'] = 'references_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 'references';
|
||||
}
|
||||
|
||||
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('references_options');
|
||||
|
||||
// Play nice with View UI 'preview' : if the view is not executed through
|
||||
// _*_reference_potential_references_views(), 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 = $this->view->query->add_field($this->view->base_table, $this->view->base_field);
|
||||
|
||||
// Restrict on the incoming string, or incoming ids.
|
||||
if ($options['string'] !== '') {
|
||||
switch ($options['match']) {
|
||||
case 'equals':
|
||||
$operator = '=';
|
||||
$value = $options['string'];
|
||||
break;
|
||||
|
||||
case 'starts_with':
|
||||
$operator = 'LIKE';
|
||||
$value = db_like($options['string']) . '%';
|
||||
break;
|
||||
|
||||
case 'contains':
|
||||
default:
|
||||
$operator = 'LIKE';
|
||||
$value = '%' . db_like($options['string']) . '%';
|
||||
break;
|
||||
}
|
||||
|
||||
$table_alias = $this->view->query->ensure_table($this->view->base_table);
|
||||
$this->view->query->add_where(NULL, $table_alias . '.' . $options['title_field'], $value, $operator);
|
||||
}
|
||||
elseif ($options['ids']) {
|
||||
$table_alias = $this->view->query->ensure_table($this->view->base_table);
|
||||
$this->view->query->add_where(NULL, $table_alias . '.' . $this->view->base_field, $options['ids'], 'IN');
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Handler for references_plugin_row_fields.
|
||||
*/
|
||||
class references_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 'References' 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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Handler for references_plugin_style.
|
||||
*/
|
||||
class references_plugin_style extends views_plugin_style {
|
||||
function render() {
|
||||
$options = $this->display->handler->get_option('references_options');
|
||||
|
||||
// Play nice with View UI 'preview' : if the view is not executed through
|
||||
// _*_reference_potential_references_views(), just display the HTML.
|
||||
if (empty($options)) {
|
||||
return parent::render();
|
||||
}
|
||||
|
||||
$title_field = $options['title_field'];
|
||||
|
||||
// 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 references_plugin_display.
|
||||
$id_field_alias = $this->display->handler->id_field_alias;
|
||||
|
||||
$results = array();
|
||||
$this->view->row_index = 0;
|
||||
foreach ($sets as $title => $records) {
|
||||
foreach ($records as $label => $values) {
|
||||
// Render the row.
|
||||
$rendered = $this->row_plugin->render($values);
|
||||
// Remove linebreaks and extra spaces introduced by templates.
|
||||
$rendered = preg_replace('/\s+/', ' ', trim($rendered));
|
||||
|
||||
// Collect the rendered row, and the raw title value.
|
||||
$results[$values->{$id_field_alias}] = array(
|
||||
'rendered' => $rendered,
|
||||
'group' => $title,
|
||||
'title' => $this->view->field[$title_field]->get_value($values),
|
||||
);
|
||||
|
||||
$this->view->row_index++;
|
||||
}
|
||||
}
|
||||
unset($this->view->row_index);
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user