diff --git a/field_collection.info b/field_collection.info index 39f62c2..494eb27 100644 --- a/field_collection.info +++ b/field_collection.info @@ -4,5 +4,6 @@ core = 7.x dependencies[] = entity files[] = field_collection.test files[] = field_collection.info.inc +files[] = views/field_collection_handler_relationship.inc configure = admin/structure/field-collections package = Fields diff --git a/views/field_collection.views.inc b/views/field_collection.views.inc index e6ea252..a4e8a0d 100644 --- a/views/field_collection.views.inc +++ b/views/field_collection.views.inc @@ -16,10 +16,11 @@ function field_collection_field_views_data($field) { // 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' => 'views_handler_relationship', + 'handler' => 'field_collection_handler_relationship', 'base' => 'field_collection_item', 'base field' => 'item_id', 'label' => t('field collection item from !field_name', array('!field_name' => $field['field_name'])), + 'field_name' => $field['field_name'], ); } } diff --git a/views/field_collection_handler_relationship.inc b/views/field_collection_handler_relationship.inc new file mode 100644 index 0000000..afbe121 --- /dev/null +++ b/views/field_collection_handler_relationship.inc @@ -0,0 +1,58 @@ + -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; + } +}