1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Provide Product Reference related Views integration.
|
5
|
*/
|
6
|
|
7
|
|
8
|
/**
|
9
|
* Implements hook_field_views_data().
|
10
|
*/
|
11
|
function commerce_product_reference_field_views_data($field) {
|
12
|
$data = field_views_field_default_views_data($field);
|
13
|
|
14
|
// Build an array of bundles the product reference field appears on.
|
15
|
$bundles = array();
|
16
|
|
17
|
foreach ($field['bundles'] as $entity => $entity_bundles) {
|
18
|
$bundles[] = $entity . ' (' . implode(', ', $entity_bundles) . ')';
|
19
|
}
|
20
|
|
21
|
$replacements = array('!field_name' => $field['field_name'], '@bundles' => implode(', ', $bundles));
|
22
|
|
23
|
foreach ($data as $table_name => $table_data) {
|
24
|
foreach ($table_data as $field_name => $field_data) {
|
25
|
if (isset($field_data['filter']['field_name']) && $field_name != 'delta') {
|
26
|
$data[$table_name][$field_name]['relationship'] = array(
|
27
|
'title' => t('Referenced products'),
|
28
|
'label' => t('Products referenced by !field_name', $replacements),
|
29
|
'help' => t('Relate this entity to products referenced by its !field_name value.', $replacements) . '<br />' . t('Appears in: @bundles.', $replacements),
|
30
|
'base' => 'commerce_product',
|
31
|
'base field' => 'product_id',
|
32
|
'handler' => 'views_handler_relationship',
|
33
|
);
|
34
|
}
|
35
|
}
|
36
|
}
|
37
|
|
38
|
return $data;
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Implements hook_views_data_alter().
|
43
|
*/
|
44
|
function commerce_product_reference_views_data_alter(&$data) {
|
45
|
// Add a node filter that filters by node types with product reference fields.
|
46
|
$data['node']['is_product_display'] = array(
|
47
|
'title' => t('Product display'),
|
48
|
'help' => t('Whether or not the content functions as a product display.'),
|
49
|
'real field' => 'type',
|
50
|
'filter' => array(
|
51
|
'handler' => 'commerce_product_reference_handler_filter_node_is_product_display',
|
52
|
'label' => t('Is a product display'),
|
53
|
'type' => 'yes-no',
|
54
|
),
|
55
|
);
|
56
|
|
57
|
// Add the node filter that filters by node types with product reference fields.
|
58
|
$data['node']['product_display_node_type'] = array(
|
59
|
'title' => t('Product display content type'),
|
60
|
'help' => t('Filters by the content type but only shows product display content types as options.'),
|
61
|
'real field' => 'type',
|
62
|
'filter' => array(
|
63
|
'handler' => 'commerce_product_reference_handler_filter_node_type',
|
64
|
),
|
65
|
);
|
66
|
|
67
|
// Add the line item filter for filtering by line items of a product line item
|
68
|
// type (based on $line_item_type['product']).
|
69
|
$data['commerce_line_item']['product_line_item_type'] = array(
|
70
|
'title' => t('Line item is of a product line item type'),
|
71
|
'help' => t("Filter line items to those of a product line item type (including but not limited to the default Product line item type)."),
|
72
|
'filter' => array(
|
73
|
'handler' => 'commerce_product_reference_handler_filter_product_line_item_type',
|
74
|
),
|
75
|
);
|
76
|
|
77
|
// Adds inverse relationships between the product and entity types referencing
|
78
|
// it (for example, nodes).
|
79
|
foreach (commerce_info_fields('commerce_product_reference') as $field_name => $field) {
|
80
|
foreach ($field['bundles'] as $entity_type => $bundles) {
|
81
|
if ($entity_type == 'commerce_line_item') {
|
82
|
continue;
|
83
|
}
|
84
|
|
85
|
$entity_info = entity_get_info($entity_type);
|
86
|
$replacements = array('@entity_type' => $entity_info['label'], '!field_name' => $field['field_name']);
|
87
|
|
88
|
$data['commerce_product'][$field['field_name']]['relationship'] = array(
|
89
|
'handler' => 'views_handler_relationship_entity_reverse',
|
90
|
'field_name' => $field['field_name'],
|
91
|
'field table' => _field_sql_storage_tablename($field),
|
92
|
'field field' => $field['field_name'] . '_product_id',
|
93
|
'base' => $entity_info['base table'],
|
94
|
'base field' => $entity_info['entity keys']['id'],
|
95
|
|
96
|
'label' => t('@entity_type referencing products from !field_name', $replacements),
|
97
|
'title' => t('Referencing @entity_type', $replacements),
|
98
|
'help' => t('Relate a product to the @entity_type referencing it through !field_name.', $replacements),
|
99
|
);
|
100
|
}
|
101
|
}
|
102
|
}
|