comparison sites/all/modules/references/references.module @ 4:ce11bbd8f642

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