Skip to content
Snippets Groups Projects
Commit e042f9d4 authored by Jeroen Bobbeldijk's avatar Jeroen Bobbeldijk
Browse files

Views support

parent 44d19785
No related branches found
Tags 7.x-1.0-alpha3
No related merge requests found
......@@ -3,3 +3,5 @@ description = Paragraphs module to control your content flow.
core = 7.x
dependencies[] = entity
files[] = views/paragraphs_handler_relationship.inc
......@@ -2240,3 +2240,13 @@ function paragraphs_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
$form['field']['cardinality']['#default_value'] = FIELD_CARDINALITY_UNLIMITED;
}
}
/**
* Implements hook_views_api().
*/
function paragraphs_views_api() {
return array(
'api' => '3.0-alpha1',
'path' => drupal_get_path('module', 'paragraphs') . '/views',
);
}
<?php
/**
* Implements hook_field_views_data().
*
* Views integration for paragraphs fields. Adds a relationship to the
* default field data.
*
* @see field_views_field_default_views_data()
*/
function paragraphs_field_views_data($field) {
$data = field_views_field_default_views_data($field);
foreach ($data as $table_name => $table_data) {
foreach ($table_data as $field_name => $field_data) {
// Only operate on the "field_api_field_name"_value column.
if (strrpos($field_name, '_value') === (strlen($field_name) - strlen('_value'))) {
$data[$table_name][$field_name]['relationship'] = array(
'handler' => 'paragraphs_handler_relationship',
'base' => 'paragraphs_item',
'base field' => 'item_id',
'label' => t('Paragraph item from !field_name', array('!field_name' => $field['field_name'])),
'field_name' => $field['field_name'],
);
}
}
}
foreach ($field['bundles'] as $entity_type => $bundles) {
$entity_info = entity_get_info($entity_type);
$pseudo_field_name = $field['field_name'] . '_' . $entity_type;
list($label, $all_labels) = field_views_field_label($field['field_name']);
$entity = $entity_info['label'];
if ($entity == t('Node')) {
$entity = t('Content');
}
$data['paragraphs_item'][$pseudo_field_name]['relationship'] = array(
'title' => t('Entity with the @field (@field_name)', array('@entity' => $entity, '@field' => $label, '@field_name' => $field['field_name'])),
'help' => t('Relate each @entity using @field.', array('@entity' => $entity, '@field' => $label)),
'handler' => 'views_handler_relationship_entity_reverse',
'field_name' => $field['field_name'],
'field table' => _field_sql_storage_tablename($field),
'field field' => $field['field_name'] . '_value',
'base' => $entity_info['base table'],
'base field' => $entity_info['entity keys']['id'],
'label' => t('!field_name', array('!field_name' => $field['field_name'])),
);
}
return $data;
}
<?php
/**
* @file
* Provide relationship handler for paragraphs fields.
*/
class paragraphs_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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment