-
Notifications
You must be signed in to change notification settings - Fork 6
/
references.module
234 lines (214 loc) · 7.66 KB
/
references.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @file
* Defines common base features for the various reference field types.
*/
/**
* Menu access callback for reference autocomplete paths.
*
* Check for both 'edit' and 'view' access in the unlikely event
* a user has edit but not view access.
*/
function reference_autocomplete_access($entity_type, $bundle, $field_name, $entity = NULL, $account = NULL) {
return user_access('access content', $account)
&& field_info_instance($entity_type, $field_name, $bundle)
&& ($field = field_info_field($field_name))
&& field_access('view', $field, $entity_type, $entity, $account)
&& field_access('edit', $field, $entity_type, $entity, $account);
}
/**
* Implements hook_init().
*/
function references_init() {
// Include feeds.module integration.
if (module_exists('feeds')) {
module_load_include('inc', 'references', 'references.feeds');
}
}
/**
* Implements hook_views_api().
*/
function references_views_api() {
return array(
'api' => 3,
'path' => backdrop_get_path('module', 'references') . '/views',
);
}
/**
* Implements hook_autoload_info().
*/
function references_autoload_info() {
return array(
'references_handler_argument' => 'views/references_handler_argument.inc',
'references_handler_relationship' => 'views/references_handler_relationship.inc',
'references_plugin_display' => 'views/references_plugin_display.inc',
'references_plugin_row_fields' => 'views/references_plugin_row_fields.inc',
'references_plugin_style' => 'views/references_plugin_style.inc',
);
}
/**
* Implements hook_views_plugins().
*
* Defines some plugins used by the Views modes for
* user_reference.
*/
function references_views_plugins() {
$plugins = array(
'display' => array(
'references' => array(
'title' => t('References'),
'admin' => t('References'),
'help' => 'Selects referenceable entities for a reference field (node_reference, user_reference...)',
'handler' => 'references_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 'References' display.
'references display' => TRUE,
),
),
'style' => array(
'references_style' => array(
'title' => t('References list'),
'help' => 'Returns results as a PHP array of names + rendered rows.',
'handler' => 'references_plugin_style',
'theme' => 'views_view_unformatted',
'theme path' => backdrop_get_path('module', 'views') . '/templates',
'theme file' => 'views.theme.inc',
'uses row plugin' => TRUE,
'uses fields' => TRUE,
'uses options' => TRUE,
'uses grouping' => TRUE,
'type' => 'references',
'even empty' => TRUE,
),
),
'row' => array(
'references_fields' => array(
'title' => t('Inline fields'),
'help' => t('Displays the fields with an optional template.'),
'handler' => 'references_plugin_row_fields',
'theme' => 'views_view_fields',
'theme path' => backdrop_get_path('module', 'views') . '/templates',
'theme file' => 'views.theme.inc',
'uses fields' => TRUE,
'uses options' => TRUE,
'type' => 'references',
),
),
);
return $plugins;
}
/**
* Get Views Options.
*
* Retrieves the list of views with a 'references' display, in a format suitable
* for a 'select' form element..
*
* @param string $entity_type
* The entity type.
*
* @return array
* An array of eligible views displays.
*/
function references_get_views_options($entity_type) {
// Filter views that contain a 'references' display. This actually returns a
// list of displays (the same view appears several times).
$displays = views_get_applicable_views('references display');
// Filter views that list the entity type we want, and group the separate
// displays by view.
$entity_info = entity_get_info($entity_type);
$options = array();
foreach ($displays as $data) {
list($view, $display_id) = $data;
if ($view->base_table == $entity_info['base table']) {
$options[$view->name . ':' . $display_id] = $view->name . ' - ' . $view->display[$display_id]->display_title;
}
}
return $options;
}
/**
* Retrieves an array of candidate referenceable entities, defined by a view.
*
* @param string $entity_type
* The entity type.
* @param string $view_name
* The name of the view.
* @param string $display_name
* The name of the view's display. This has to be a 'References' display.
* @param array $args
* The array of arguments ("contextual filters") for the view.
* @param array $options
* Array of options to limit the scope of the returned list. This parameter
* is similar to the $options parameter for
* node_reference_potential_references(). An additional key is required:
* - title_field: the name of the column holding entities 'titles' within the
* entity base table.
*
* @return array
* An array of entities, in the format expected by
* node_reference_potential_references().
*
* @see node_reference_potential_references()
* @see _node_reference_potential_references_views()
*
* @codingStandardsIgnoreStart
*/
function references_potential_references_view($entity_type, $view_name, $display_name, $args, $options) {
// @codingStandardsIgnoreEnd
$entity_info = entity_get_info($entity_type);
// Check that the view is valid and the display still exists.
$view = views_get_view($view_name);
if (!$view || $view->base_table != $entity_info['base table'] || !isset($view->display[$display_name])) {
return FALSE;
}
// If we have no access to the View an empty result should be returned to
// avoid triggering the fallback results.
if (!$view->access(array($display_name))) {
return array();
}
// Temporary backwards compatibility for fields migrated from CCK D6: accept
// 'default' display, but dynamically add a 'references' display out of it.
if ($display_name == 'default') {
$display_name = $view->add_display('references');
}
$view->set_display($display_name);
// @todo From merlinofchaos on IRC : arguments using summary view can defeat
// the style setting.
// We might also need to check if there's an argument, and set its
// style_plugin as well.
// Set additional options to let references_plugin_display::query() narrow
// the results.
$references_options = array(
'ids' => $options['ids'],
'title_field' => $options['title_field'],
'string' => $options['string'],
'match' => $options['match'],
);
$view->display_handler->set_option('references_options', $references_options);
// We need the title field for autocomplete widgets, so add it (hidden) if not
// present.
$fields = $view->get_items('field', $display_name);
if (!isset($fields[$options['title_field']])) {
$label_options = array(
'exclude' => 1,
);
$view->add_item($display_name, 'field', $entity_info['base table'], $options['title_field'], $label_options);
}
// Make sure the query is not cached.
$view->is_cacheable = FALSE;
// Get the results.
$results = $view->execute_display($display_name, $args);
return $results;
}
/**
* Form element validation handler for integer elements that must be positive.
*/
function references_element_validate_integer_positive($element, &$form_state) {
$value = $element['#value'];
if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value <= 0)) {
form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
}
}