Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Primarily Drupal hooks and global API functions to manipulate views.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Component\Render\MarkupInterface;
|
Chris@0
|
9 use Drupal\Component\Utility\Html;
|
Chris@0
|
10 use Drupal\Core\Database\Query\AlterableInterface;
|
Chris@0
|
11 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
12 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
13 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
14 use Drupal\Core\Url;
|
Chris@0
|
15 use Drupal\views\Plugin\Derivative\ViewsLocalTask;
|
Chris@14
|
16 use Drupal\views\ViewEntityInterface;
|
Chris@0
|
17 use Drupal\views\ViewExecutable;
|
Chris@0
|
18 use Drupal\views\Entity\View;
|
Chris@0
|
19 use Drupal\views\Render\ViewsRenderPipelineMarkup;
|
Chris@0
|
20 use Drupal\views\Views;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Implements hook_help().
|
Chris@0
|
24 */
|
Chris@0
|
25 function views_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
26 switch ($route_name) {
|
Chris@0
|
27 case 'help.page.views':
|
Chris@0
|
28 $output = '';
|
Chris@0
|
29 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@0
|
30 $output .= '<p>' . t('The Views module provides a back end to fetch information from content, user accounts, taxonomy terms, and other entities from the database and present it to the user as a grid, HTML list, table, unformatted list, etc. The resulting displays are known generally as <em>views</em>.') . '</p>';
|
Chris@0
|
31 $output .= '<p>' . t('For more information, see the <a href=":views">online documentation for the Views module</a>.', [':views' => 'https://www.drupal.org/documentation/modules/views']) . '</p>';
|
Chris@18
|
32 $output .= '<p>' . t('In order to create and modify your own views using the administration and configuration user interface, you will need to enable either the Views UI module in core or a contributed module that provides a user interface for Views. See the <a href=":views-ui">Views UI module help page</a> for more information.', [':views-ui' => (\Drupal::moduleHandler()->moduleExists('views_ui')) ? Url::fromRoute('help.page', ['name' => 'views_ui'])->toString() : '#']) . '</p>';
|
Chris@0
|
33 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
34 $output .= '<dl>';
|
Chris@0
|
35 $output .= '<dt>' . t('Adding functionality to administrative pages') . '</dt>';
|
Chris@0
|
36 $output .= '<dd>' . t('The Views module adds functionality to some core administration pages. For example, <em>admin/content</em> uses Views to filter and sort content. With Views uninstalled, <em>admin/content</em> is more limited.') . '</dd>';
|
Chris@0
|
37 $output .= '<dt>' . t('Expanding Views functionality') . '</dt>';
|
Chris@0
|
38 $output .= '<dd>' . t('Contributed projects that support the Views module can be found in the <a href=":node">online documentation for Views-related contributed modules</a>.', [':node' => 'https://www.drupal.org/documentation/modules/views/add-ons']) . '</dd>';
|
Chris@0
|
39 $output .= '<dt>' . t('Improving table accessibility') . '</dt>';
|
Chris@0
|
40 $output .= '<dd>' . t('Views tables include semantic markup to improve accessibility. Data cells are automatically associated with header cells through id and header attributes. To improve the accessibility of your tables you can add descriptive elements within the Views table settings. The <em>caption</em> element can introduce context for a table, making it easier to understand. The <em>summary</em> element can provide an overview of how the data has been organized and how to navigate the table. Both the caption and summary are visible by default and also implemented according to HTML5 guidelines.') . '</dd>';
|
Chris@0
|
41 $output .= '<dt>' . t('Working with multilingual views') . '</dt>';
|
Chris@0
|
42 $output .= '<dd>' . t('If your site has multiple languages and translated entities, each result row in a view will contain one translation of each involved entity (a view can involve multiple entities if it uses relationships). You can use a filter to restrict your view to one language: without filtering, if an entity has three translations it will add three rows to the results; if you filter by language, at most one result will appear (it could be zero if that particular entity does not have a translation matching your language filter choice). If a view uses relationships, each entity in the relationship needs to be filtered separately. You can filter a view to a fixed language choice, such as English or Spanish, or to the language selected by the page the view is displayed on (the language that is selected for the page by the language detection settings either for Content or User interface).') . '</dd>';
|
Chris@0
|
43 $output .= '<dd>' . t('Because each result row contains a specific translation of each entity, field-level filters are also relative to these entity translations. For example, if your view has a filter that specifies that the entity title should contain a particular English word, you will presumably filter out all rows containing Chinese translations, since they will not contain the English word. If your view also has a second filter specifying that the title should contain a particular Chinese word, and if you are using "And" logic for filtering, you will presumably end up with no results in the view, because there are probably not any entity translations containing both the English and Chinese words in the title.') . '</dd>';
|
Chris@0
|
44 $output .= '<dd>' . t('Independent of filtering, you can choose the display language (the language used to display the entities and their fields) via a setting on the display. Your language choices are the same as the filter language choices, with an additional choice of "Content language of view row" and "Original language of content in view row", which means to display each entity in the result row using the language that entity has or in which it was originally created. In theory, this would give you the flexibility to filter to French translations, for instance, and then display the results in Spanish. The more usual choices would be to use the same language choices for the display language and each entity filter in the view, or to use the Row language setting for the display.') . '</dd>';
|
Chris@0
|
45 $output .= '</dl>';
|
Chris@0
|
46 return $output;
|
Chris@0
|
47 }
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Implements hook_views_pre_render().
|
Chris@0
|
52 */
|
Chris@0
|
53 function views_views_pre_render($view) {
|
Chris@0
|
54 // If using AJAX, send identifying data about this view.
|
Chris@0
|
55 if ($view->ajaxEnabled() && empty($view->is_attachment) && empty($view->live_preview)) {
|
Chris@0
|
56 $view->element['#attached']['drupalSettings']['views'] = [
|
Chris@18
|
57 'ajax_path' => Url::fromRoute('views.ajax')->toString(),
|
Chris@0
|
58 'ajaxViews' => [
|
Chris@0
|
59 'views_dom_id:' . $view->dom_id => [
|
Chris@0
|
60 'view_name' => $view->storage->id(),
|
Chris@0
|
61 'view_display_id' => $view->current_display,
|
Chris@0
|
62 'view_args' => Html::escape(implode('/', $view->args)),
|
Chris@0
|
63 'view_path' => Html::escape(Url::fromRoute('<current>')->toString()),
|
Chris@0
|
64 'view_base_path' => $view->getPath(),
|
Chris@0
|
65 'view_dom_id' => $view->dom_id,
|
Chris@0
|
66 // To fit multiple views on a page, the programmer may have
|
Chris@0
|
67 // overridden the display's pager_element.
|
Chris@0
|
68 'pager_element' => isset($view->pager) ? $view->pager->getPagerId() : 0,
|
Chris@0
|
69 ],
|
Chris@0
|
70 ],
|
Chris@0
|
71 ];
|
Chris@0
|
72 $view->element['#attached']['library'][] = 'views/views.ajax';
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 return $view;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Implements hook_theme().
|
Chris@0
|
80 *
|
Chris@0
|
81 * Register views theming functions and those that are defined via views plugin
|
Chris@0
|
82 * definitions.
|
Chris@0
|
83 */
|
Chris@0
|
84 function views_theme($existing, $type, $theme, $path) {
|
Chris@0
|
85 \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
|
Chris@0
|
86
|
Chris@0
|
87 // Some quasi clever array merging here.
|
Chris@0
|
88 $base = [
|
Chris@0
|
89 'file' => 'views.theme.inc',
|
Chris@0
|
90 ];
|
Chris@0
|
91
|
Chris@0
|
92 // Our extra version of pager from pager.inc
|
Chris@0
|
93 $hooks['views_mini_pager'] = $base + [
|
Chris@0
|
94 'variables' => ['tags' => [], 'quantity' => 9, 'element' => 0, 'parameters' => []],
|
Chris@0
|
95 ];
|
Chris@0
|
96
|
Chris@0
|
97 $variables = [
|
Chris@0
|
98 // For displays, we pass in a dummy array as the first parameter, since
|
Chris@0
|
99 // $view is an object but the core contextual_preprocess() function only
|
Chris@0
|
100 // attaches contextual links when the primary theme argument is an array.
|
Chris@0
|
101 'display' => [
|
Chris@0
|
102 'view_array' => [],
|
Chris@0
|
103 'view' => NULL,
|
Chris@0
|
104 'rows' => [],
|
Chris@0
|
105 'header' => [],
|
Chris@0
|
106 'footer' => [],
|
Chris@0
|
107 'empty' => [],
|
Chris@0
|
108 'exposed' => [],
|
Chris@0
|
109 'more' => [],
|
Chris@0
|
110 'feed_icons' => [],
|
Chris@0
|
111 'pager' => [],
|
Chris@0
|
112 'title' => '',
|
Chris@0
|
113 'attachment_before' => [],
|
Chris@0
|
114 'attachment_after' => [],
|
Chris@0
|
115 ],
|
Chris@0
|
116 'style' => ['view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL],
|
Chris@0
|
117 'row' => ['view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL],
|
Chris@0
|
118 'exposed_form' => ['view' => NULL, 'options' => NULL],
|
Chris@0
|
119 'pager' => [
|
Chris@0
|
120 'view' => NULL,
|
Chris@0
|
121 'options' => NULL,
|
Chris@0
|
122 'tags' => [],
|
Chris@0
|
123 'quantity' => 9,
|
Chris@0
|
124 'element' => 0,
|
Chris@0
|
125 'parameters' => [],
|
Chris@0
|
126 ],
|
Chris@0
|
127 ];
|
Chris@0
|
128
|
Chris@0
|
129 // Default view themes
|
Chris@0
|
130 $hooks['views_view_field'] = $base + [
|
Chris@0
|
131 'variables' => ['view' => NULL, 'field' => NULL, 'row' => NULL],
|
Chris@0
|
132 ];
|
Chris@0
|
133 $hooks['views_view_grouping'] = $base + [
|
Chris@0
|
134 'variables' => ['view' => NULL, 'grouping' => NULL, 'grouping_level' => NULL, 'rows' => NULL, 'title' => NULL],
|
Chris@0
|
135 ];
|
Chris@0
|
136
|
Chris@0
|
137 // Only display, pager, row, and style plugins can provide theme hooks.
|
Chris@0
|
138 $plugin_types = [
|
Chris@0
|
139 'display',
|
Chris@0
|
140 'pager',
|
Chris@0
|
141 'row',
|
Chris@0
|
142 'style',
|
Chris@0
|
143 'exposed_form',
|
Chris@0
|
144 ];
|
Chris@0
|
145 $plugins = [];
|
Chris@0
|
146 foreach ($plugin_types as $plugin_type) {
|
Chris@0
|
147 $plugins[$plugin_type] = Views::pluginManager($plugin_type)->getDefinitions();
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 $module_handler = \Drupal::moduleHandler();
|
Chris@0
|
151
|
Chris@0
|
152 // Register theme functions for all style plugins. It provides a basic auto
|
Chris@0
|
153 // implementation of theme functions or template files by using the plugin
|
Chris@0
|
154 // definitions (theme, theme_file, module, register_theme). Template files are
|
Chris@0
|
155 // assumed to be located in the templates folder.
|
Chris@0
|
156 foreach ($plugins as $type => $info) {
|
Chris@0
|
157 foreach ($info as $def) {
|
Chris@0
|
158 // Not all plugins have theme functions, and they can also explicitly
|
Chris@0
|
159 // prevent a theme function from being registered automatically.
|
Chris@0
|
160 if (!isset($def['theme']) || empty($def['register_theme'])) {
|
Chris@0
|
161 continue;
|
Chris@0
|
162 }
|
Chris@0
|
163 // For each theme registration, we have a base directory to check for the
|
Chris@0
|
164 // templates folder. This will be relative to the root of the given module
|
Chris@0
|
165 // folder, so we always need a module definition.
|
Chris@0
|
166 // @todo: watchdog or exception?
|
Chris@0
|
167 if (!isset($def['provider']) || !$module_handler->moduleExists($def['provider'])) {
|
Chris@0
|
168 continue;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 $hooks[$def['theme']] = [
|
Chris@0
|
172 'variables' => $variables[$type],
|
Chris@0
|
173 ];
|
Chris@0
|
174
|
Chris@0
|
175 // We always use the module directory as base dir.
|
Chris@0
|
176 $module_dir = drupal_get_path('module', $def['provider']);
|
Chris@0
|
177 $hooks[$def['theme']]['path'] = $module_dir;
|
Chris@0
|
178
|
Chris@0
|
179 // For the views module we ensure views.theme.inc is included.
|
Chris@0
|
180 if ($def['provider'] == 'views') {
|
Chris@0
|
181 if (!isset($hooks[$def['theme']]['includes'])) {
|
Chris@0
|
182 $hooks[$def['theme']]['includes'] = [];
|
Chris@0
|
183 }
|
Chris@0
|
184 if (!in_array('views.theme.inc', $hooks[$def['theme']]['includes'])) {
|
Chris@0
|
185 $hooks[$def['theme']]['includes'][] = $module_dir . '/views.theme.inc';
|
Chris@0
|
186 }
|
Chris@0
|
187 }
|
Chris@0
|
188 // The theme_file definition is always relative to the modules directory.
|
Chris@0
|
189 elseif (!empty($def['theme_file'])) {
|
Chris@0
|
190 $hooks[$def['theme']]['file'] = $def['theme_file'];
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 // Whenever we have a theme file, we include it directly so we can
|
Chris@0
|
194 // auto-detect the theme function.
|
Chris@0
|
195 if (isset($def['theme_file'])) {
|
Chris@0
|
196 $include = \Drupal::root() . '/' . $module_dir . '/' . $def['theme_file'];
|
Chris@0
|
197 if (is_file($include)) {
|
Chris@0
|
198 require_once $include;
|
Chris@0
|
199 }
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 // If there is no theme function for the given theme definition, it must
|
Chris@0
|
203 // be a template file. By default this file is located in the /templates
|
Chris@0
|
204 // directory of the module's folder. If a module wants to define its own
|
Chris@0
|
205 // location it has to set register_theme of the plugin to FALSE and
|
Chris@0
|
206 // implement hook_theme() by itself.
|
Chris@0
|
207 if (!function_exists('theme_' . $def['theme'])) {
|
Chris@0
|
208 $hooks[$def['theme']]['path'] .= '/templates';
|
Chris@0
|
209 $hooks[$def['theme']]['template'] = Html::cleanCssIdentifier($def['theme']);
|
Chris@0
|
210 }
|
Chris@0
|
211 else {
|
Chris@0
|
212 $hooks[$def['theme']]['function'] = 'theme_' . $def['theme'];
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 $hooks['views_form_views_form'] = $base + [
|
Chris@0
|
218 'render element' => 'form',
|
Chris@0
|
219 ];
|
Chris@0
|
220
|
Chris@0
|
221 $hooks['views_exposed_form'] = $base + [
|
Chris@0
|
222 'render element' => 'form',
|
Chris@0
|
223 ];
|
Chris@0
|
224
|
Chris@0
|
225 return $hooks;
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 /**
|
Chris@0
|
229 * A theme preprocess function to automatically allow view-based node
|
Chris@0
|
230 * templates if called from a view.
|
Chris@0
|
231 *
|
Chris@0
|
232 * The 'modules/node.views.inc' file is a better place for this, but
|
Chris@0
|
233 * we haven't got a chance to load that file before Drupal builds the
|
Chris@0
|
234 * node portion of the theme registry.
|
Chris@0
|
235 */
|
Chris@0
|
236 function views_preprocess_node(&$variables) {
|
Chris@0
|
237 // The 'view' attribute of the node is added in
|
Chris@0
|
238 // \Drupal\views\Plugin\views\row\EntityRow::preRender().
|
Chris@0
|
239 if (!empty($variables['node']->view) && $variables['node']->view->storage->id()) {
|
Chris@0
|
240 $variables['view'] = $variables['node']->view;
|
Chris@0
|
241 // If a node is being rendered in a view, and the view does not have a path,
|
Chris@0
|
242 // prevent drupal from accidentally setting the $page variable:
|
Chris@0
|
243 if (!empty($variables['view']->current_display)
|
Chris@0
|
244 && $variables['page']
|
Chris@0
|
245 && $variables['view_mode'] == 'full'
|
Chris@0
|
246 && !$variables['view']->display_handler->hasPath()) {
|
Chris@0
|
247 $variables['page'] = FALSE;
|
Chris@0
|
248 }
|
Chris@0
|
249 }
|
Chris@0
|
250 }
|
Chris@0
|
251
|
Chris@0
|
252 /**
|
Chris@0
|
253 * Implements hook_theme_suggestions_HOOK_alter().
|
Chris@0
|
254 */
|
Chris@0
|
255 function views_theme_suggestions_node_alter(array &$suggestions, array $variables) {
|
Chris@0
|
256 $node = $variables['elements']['#node'];
|
Chris@0
|
257 if (!empty($node->view) && $node->view->storage->id()) {
|
Chris@0
|
258 $suggestions[] = 'node__view__' . $node->view->storage->id();
|
Chris@0
|
259 if (!empty($node->view->current_display)) {
|
Chris@0
|
260 $suggestions[] = 'node__view__' . $node->view->storage->id() . '__' . $node->view->current_display;
|
Chris@0
|
261 }
|
Chris@0
|
262 }
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * A theme preprocess function to automatically allow view-based node
|
Chris@0
|
267 * templates if called from a view.
|
Chris@0
|
268 */
|
Chris@0
|
269 function views_preprocess_comment(&$variables) {
|
Chris@0
|
270 // The view data is added to the comment in
|
Chris@0
|
271 // \Drupal\views\Plugin\views\row\EntityRow::preRender().
|
Chris@0
|
272 if (!empty($variables['comment']->view) && $variables['comment']->view->storage->id()) {
|
Chris@0
|
273 $variables['view'] = $variables['comment']->view;
|
Chris@0
|
274 }
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 /**
|
Chris@0
|
278 * Implements hook_theme_suggestions_HOOK_alter().
|
Chris@0
|
279 */
|
Chris@0
|
280 function views_theme_suggestions_comment_alter(array &$suggestions, array $variables) {
|
Chris@0
|
281 $comment = $variables['elements']['#comment'];
|
Chris@0
|
282 if (!empty($comment->view) && $comment->view->storage->id()) {
|
Chris@0
|
283 $suggestions[] = 'comment__view__' . $comment->view->storage->id();
|
Chris@0
|
284 if (!empty($comment->view->current_display)) {
|
Chris@0
|
285 $suggestions[] = 'comment__view__' . $comment->view->storage->id() . '__' . $comment->view->current_display;
|
Chris@0
|
286 }
|
Chris@0
|
287 }
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 /**
|
Chris@0
|
291 * Implements hook_theme_suggestions_HOOK_alter().
|
Chris@0
|
292 */
|
Chris@0
|
293 function views_theme_suggestions_container_alter(array &$suggestions, array $variables) {
|
Chris@0
|
294 if (!empty($variables['element']['#type']) && $variables['element']['#type'] == 'more_link' && !empty($variables['element']['#view']) && $variables['element']['#view'] instanceof ViewExecutable) {
|
Chris@0
|
295 $suggestions = array_merge($suggestions, $variables['element']['#view']->buildThemeFunctions('container__more_link'));
|
Chris@0
|
296 }
|
Chris@0
|
297 }
|
Chris@0
|
298
|
Chris@0
|
299 /**
|
Chris@0
|
300 * Adds contextual links associated with a view display to a renderable array.
|
Chris@0
|
301 *
|
Chris@0
|
302 * This function should be called when a view is being rendered in a particular
|
Chris@0
|
303 * location and you want to attach the appropriate contextual links (e.g.,
|
Chris@0
|
304 * links for editing the view) to it.
|
Chris@0
|
305 *
|
Chris@0
|
306 * The function operates by checking the view's display plugin to see if it has
|
Chris@0
|
307 * defined any contextual links that are intended to be displayed in the
|
Chris@0
|
308 * requested location; if so, it attaches them. The contextual links intended
|
Chris@0
|
309 * for a particular location are defined by the 'contextual links' and
|
Chris@0
|
310 * 'contextual_links_locations' properties in the plugin annotation; as a
|
Chris@0
|
311 * result, these hook implementations have full control over where and how
|
Chris@0
|
312 * contextual links are rendered for each display.
|
Chris@0
|
313 *
|
Chris@0
|
314 * In addition to attaching the contextual links to the passed-in array (via
|
Chris@0
|
315 * the standard #contextual_links property), this function also attaches
|
Chris@0
|
316 * additional information via the #views_contextual_links_info property. This
|
Chris@0
|
317 * stores an array whose keys are the names of each module that provided
|
Chris@0
|
318 * views-related contextual links (same as the keys of the #contextual_links
|
Chris@0
|
319 * array itself) and whose values are themselves arrays whose keys ('location',
|
Chris@0
|
320 * 'view_name', and 'view_display_id') store the location, name of the view,
|
Chris@0
|
321 * and display ID that were passed in to this function. This allows you to
|
Chris@0
|
322 * access information about the contextual links and how they were generated in
|
Chris@0
|
323 * a variety of contexts where you might be manipulating the renderable array
|
Chris@0
|
324 * later on (for example, alter hooks which run later during the same page
|
Chris@0
|
325 * request).
|
Chris@0
|
326 *
|
Chris@0
|
327 * @param $render_element
|
Chris@0
|
328 * The renderable array to which contextual links will be added. This array
|
Chris@16
|
329 * should be suitable for passing in to
|
Chris@16
|
330 * \Drupal\Core\Render\RendererInterface::render() and will normally contain a
|
Chris@16
|
331 * representation of the view display whose contextual links are being
|
Chris@16
|
332 * requested.
|
Chris@0
|
333 * @param $location
|
Chris@0
|
334 * The location in which the calling function intends to render the view and
|
Chris@0
|
335 * its contextual links. The core system supports three options for this
|
Chris@0
|
336 * parameter:
|
Chris@0
|
337 * - 'block': Used when rendering a block which contains a view. This
|
Chris@0
|
338 * retrieves any contextual links intended to be attached to the block
|
Chris@0
|
339 * itself.
|
Chris@0
|
340 * - 'page': Used when rendering the main content of a page which contains a
|
Chris@0
|
341 * view. This retrieves any contextual links intended to be attached to the
|
Chris@0
|
342 * page itself (for example, links which are displayed directly next to the
|
Chris@0
|
343 * page title).
|
Chris@0
|
344 * - 'view': Used when rendering the view itself, in any context. This
|
Chris@0
|
345 * retrieves any contextual links intended to be attached directly to the
|
Chris@0
|
346 * view.
|
Chris@0
|
347 * If you are rendering a view and its contextual links in another location,
|
Chris@0
|
348 * you can pass in a different value for this parameter. However, you will
|
Chris@0
|
349 * also need to set 'contextual_links_locations' in your plugin annotation to
|
Chris@0
|
350 * indicate which view displays support having their contextual links
|
Chris@0
|
351 * rendered in the location you have defined.
|
Chris@0
|
352 * @param string $display_id
|
Chris@0
|
353 * The ID of the display within $view whose contextual links will be added.
|
Chris@0
|
354 * @param array $view_element
|
Chris@0
|
355 * The render array of the view. It should contain the following properties:
|
Chris@0
|
356 * - #view_id: The ID of the view.
|
Chris@0
|
357 * - #view_display_show_admin_links: A boolean whether the admin links
|
Chris@0
|
358 * should be shown.
|
Chris@0
|
359 * - #view_display_plugin_id: The plugin ID of the display.
|
Chris@0
|
360 *
|
Chris@0
|
361 * @see \Drupal\views\Plugin\Block\ViewsBlock::addContextualLinks()
|
Chris@0
|
362 * @see template_preprocess_views_view()
|
Chris@0
|
363 */
|
Chris@0
|
364 function views_add_contextual_links(&$render_element, $location, $display_id, array $view_element = NULL) {
|
Chris@0
|
365 if (!isset($view_element)) {
|
Chris@0
|
366 $view_element = $render_element;
|
Chris@0
|
367 }
|
Chris@0
|
368 $view_element['#cache_properties'] = ['view_id', 'view_display_show_admin_links', 'view_display_plugin_id'];
|
Chris@0
|
369 $view_id = $view_element['#view_id'];
|
Chris@0
|
370 $show_admin_links = $view_element['#view_display_show_admin_links'];
|
Chris@0
|
371 $display_plugin_id = $view_element['#view_display_plugin_id'];
|
Chris@0
|
372
|
Chris@0
|
373 // Do not do anything if the view is configured to hide its administrative
|
Chris@0
|
374 // links or if the Contextual Links module is not enabled.
|
Chris@0
|
375 if (\Drupal::moduleHandler()->moduleExists('contextual') && $show_admin_links) {
|
Chris@0
|
376 // Also do not do anything if the display plugin has not defined any
|
Chris@0
|
377 // contextual links that are intended to be displayed in the requested
|
Chris@0
|
378 // location.
|
Chris@0
|
379 $plugin = Views::pluginManager('display')->getDefinition($display_plugin_id);
|
Chris@0
|
380 // If contextual_links_locations are not set, provide a sane default. (To
|
Chris@0
|
381 // avoid displaying any contextual links at all, a display plugin can still
|
Chris@0
|
382 // set 'contextual_links_locations' to, e.g., {""}.)
|
Chris@0
|
383
|
Chris@0
|
384 if (!isset($plugin['contextual_links_locations'])) {
|
Chris@0
|
385 $plugin['contextual_links_locations'] = ['view'];
|
Chris@0
|
386 }
|
Chris@0
|
387 elseif ($plugin['contextual_links_locations'] == [] || $plugin['contextual_links_locations'] == ['']) {
|
Chris@0
|
388 $plugin['contextual_links_locations'] = [];
|
Chris@0
|
389 }
|
Chris@0
|
390 else {
|
Chris@0
|
391 $plugin += ['contextual_links_locations' => ['view']];
|
Chris@0
|
392 }
|
Chris@0
|
393
|
Chris@0
|
394 // On exposed_forms blocks contextual links should always be visible.
|
Chris@0
|
395 $plugin['contextual_links_locations'][] = 'exposed_filter';
|
Chris@0
|
396 $has_links = !empty($plugin['contextual links']) && !empty($plugin['contextual_links_locations']);
|
Chris@0
|
397 if ($has_links && in_array($location, $plugin['contextual_links_locations'])) {
|
Chris@0
|
398 foreach ($plugin['contextual links'] as $group => $link) {
|
Chris@0
|
399 $args = [];
|
Chris@0
|
400 $valid = TRUE;
|
Chris@0
|
401 if (!empty($link['route_parameters_names'])) {
|
Chris@0
|
402 $view_storage = \Drupal::entityManager()
|
Chris@0
|
403 ->getStorage('view')
|
Chris@0
|
404 ->load($view_id);
|
Chris@0
|
405 foreach ($link['route_parameters_names'] as $parameter_name => $property) {
|
Chris@0
|
406 // If the plugin is trying to create an invalid contextual link
|
Chris@0
|
407 // (for example, "path/to/{$view->storage->property}", where
|
Chris@0
|
408 // $view->storage->{property} does not exist), we cannot construct
|
Chris@0
|
409 // the link, so we skip it.
|
Chris@0
|
410 if (!property_exists($view_storage, $property)) {
|
Chris@0
|
411 $valid = FALSE;
|
Chris@0
|
412 break;
|
Chris@0
|
413 }
|
Chris@0
|
414 else {
|
Chris@0
|
415 $args[$parameter_name] = $view_storage->get($property);
|
Chris@0
|
416 }
|
Chris@0
|
417 }
|
Chris@0
|
418 }
|
Chris@0
|
419 // If the link was valid, attach information about it to the renderable
|
Chris@0
|
420 // array.
|
Chris@0
|
421 if ($valid) {
|
Chris@0
|
422 $render_element['#views_contextual_links'] = TRUE;
|
Chris@0
|
423 $render_element['#contextual_links'][$group] = [
|
Chris@0
|
424 'route_parameters' => $args,
|
Chris@0
|
425 'metadata' => [
|
Chris@0
|
426 'location' => $location,
|
Chris@0
|
427 'name' => $view_id,
|
Chris@0
|
428 'display_id' => $display_id,
|
Chris@0
|
429 ],
|
Chris@0
|
430 ];
|
Chris@0
|
431 // If we're setting contextual links on a page, for a page view, for a
|
Chris@0
|
432 // user that may use contextual links, attach Views' contextual links
|
Chris@0
|
433 // JavaScript.
|
Chris@0
|
434 $render_element['#cache']['contexts'][] = 'user.permissions';
|
Chris@0
|
435 }
|
Chris@0
|
436 }
|
Chris@0
|
437 }
|
Chris@0
|
438 }
|
Chris@0
|
439 }
|
Chris@0
|
440
|
Chris@0
|
441 /**
|
Chris@0
|
442 * Implements hook_ENTITY_TYPE_insert() for 'field_config'.
|
Chris@0
|
443 */
|
Chris@0
|
444 function views_field_config_insert(EntityInterface $field) {
|
Chris@0
|
445 Views::viewsData()->clear();
|
Chris@0
|
446 }
|
Chris@0
|
447
|
Chris@0
|
448 /**
|
Chris@0
|
449 * Implements hook_ENTITY_TYPE_update() for 'field_config'.
|
Chris@0
|
450 */
|
Chris@0
|
451 function views_field_config_update(EntityInterface $entity) {
|
Chris@0
|
452 Views::viewsData()->clear();
|
Chris@0
|
453 }
|
Chris@0
|
454
|
Chris@0
|
455 /**
|
Chris@0
|
456 * Implements hook_ENTITY_TYPE_delete() for 'field_config'.
|
Chris@0
|
457 */
|
Chris@0
|
458 function views_field_config_delete(EntityInterface $entity) {
|
Chris@0
|
459 Views::viewsData()->clear();
|
Chris@0
|
460 }
|
Chris@0
|
461
|
Chris@0
|
462 /**
|
Chris@0
|
463 * Implements hook_ENTITY_TYPE_insert().
|
Chris@0
|
464 */
|
Chris@0
|
465 function views_base_field_override_insert(EntityInterface $entity) {
|
Chris@0
|
466 Views::viewsData()->clear();
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 /**
|
Chris@0
|
470 * Implements hook_ENTITY_TYPE_update().
|
Chris@0
|
471 */
|
Chris@0
|
472 function views_base_field_override_update(EntityInterface $entity) {
|
Chris@0
|
473 Views::viewsData()->clear();
|
Chris@0
|
474 }
|
Chris@0
|
475
|
Chris@0
|
476 /**
|
Chris@0
|
477 * Implements hook_ENTITY_TYPE_delete().
|
Chris@0
|
478 */
|
Chris@0
|
479 function views_base_field_override_delete(EntityInterface $entity) {
|
Chris@0
|
480 Views::viewsData()->clear();
|
Chris@0
|
481 }
|
Chris@0
|
482
|
Chris@0
|
483 /**
|
Chris@0
|
484 * Invalidate the views cache, forcing a rebuild on the next grab of table data.
|
Chris@0
|
485 */
|
Chris@0
|
486 function views_invalidate_cache() {
|
Chris@0
|
487 // Set the menu as needed to be rebuilt.
|
Chris@0
|
488 \Drupal::service('router.builder')->setRebuildNeeded();
|
Chris@0
|
489
|
Chris@0
|
490 $module_handler = \Drupal::moduleHandler();
|
Chris@0
|
491
|
Chris@0
|
492 // Reset the RouteSubscriber from views.
|
Chris@0
|
493 \Drupal::getContainer()->get('views.route_subscriber')->reset();
|
Chris@0
|
494
|
Chris@0
|
495 // Invalidate the block cache to update views block derivatives.
|
Chris@0
|
496 if ($module_handler->moduleExists('block')) {
|
Chris@0
|
497 \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
|
Chris@0
|
498 }
|
Chris@0
|
499
|
Chris@0
|
500 // Allow modules to respond to the Views cache being cleared.
|
Chris@0
|
501 $module_handler->invokeAll('views_invalidate_cache');
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 /**
|
Chris@0
|
505 * Set the current 'current view' that is being built/rendered so that it is
|
Chris@0
|
506 * easy for other modules or items in drupal_eval to identify
|
Chris@0
|
507 *
|
Chris@0
|
508 * @return \Drupal\views\ViewExecutable
|
Chris@0
|
509 */
|
Chris@0
|
510 function &views_set_current_view($view = NULL) {
|
Chris@0
|
511 static $cache = NULL;
|
Chris@0
|
512 if (isset($view)) {
|
Chris@0
|
513 $cache = $view;
|
Chris@0
|
514 }
|
Chris@0
|
515
|
Chris@0
|
516 return $cache;
|
Chris@0
|
517 }
|
Chris@0
|
518
|
Chris@0
|
519 /**
|
Chris@0
|
520 * Find out what, if any, current view is currently in use.
|
Chris@0
|
521 *
|
Chris@0
|
522 * Note that this returns a reference, so be careful! You can unintentionally
|
Chris@0
|
523 * modify the $view object.
|
Chris@0
|
524 *
|
Chris@0
|
525 * @return \Drupal\views\ViewExecutable
|
Chris@0
|
526 * The current view object.
|
Chris@0
|
527 */
|
Chris@0
|
528 function &views_get_current_view() {
|
Chris@0
|
529 return views_set_current_view();
|
Chris@0
|
530 }
|
Chris@0
|
531
|
Chris@0
|
532 /**
|
Chris@0
|
533 * Implements hook_hook_info().
|
Chris@0
|
534 */
|
Chris@0
|
535 function views_hook_info() {
|
Chris@0
|
536 $hooks = [];
|
Chris@0
|
537
|
Chris@0
|
538 $hooks += array_fill_keys([
|
Chris@0
|
539 'views_data',
|
Chris@0
|
540 'views_data_alter',
|
Chris@0
|
541 'views_analyze',
|
Chris@0
|
542 'views_invalidate_cache',
|
Chris@0
|
543 ], ['group' => 'views']);
|
Chris@0
|
544
|
Chris@0
|
545 // Register a views_plugins alter hook for all plugin types.
|
Chris@0
|
546 foreach (ViewExecutable::getPluginTypes() as $type) {
|
Chris@0
|
547 $hooks['views_plugins_' . $type . '_alter'] = [
|
Chris@0
|
548 'group' => 'views',
|
Chris@0
|
549 ];
|
Chris@0
|
550 }
|
Chris@0
|
551
|
Chris@0
|
552 $hooks += array_fill_keys([
|
Chris@0
|
553 'views_query_substitutions',
|
Chris@0
|
554 'views_form_substitutions',
|
Chris@0
|
555 'views_pre_view',
|
Chris@0
|
556 'views_pre_build',
|
Chris@0
|
557 'views_post_build',
|
Chris@0
|
558 'views_pre_execute',
|
Chris@0
|
559 'views_post_execute',
|
Chris@0
|
560 'views_pre_render',
|
Chris@0
|
561 'views_post_render',
|
Chris@0
|
562 'views_query_alter',
|
Chris@0
|
563 ], ['group' => 'views_execution']);
|
Chris@0
|
564
|
Chris@0
|
565 $hooks['field_views_data'] = [
|
Chris@0
|
566 'group' => 'views',
|
Chris@0
|
567 ];
|
Chris@0
|
568 $hooks['field_views_data_alter'] = [
|
Chris@0
|
569 'group' => 'views',
|
Chris@0
|
570 ];
|
Chris@0
|
571
|
Chris@0
|
572 return $hooks;
|
Chris@0
|
573 }
|
Chris@0
|
574
|
Chris@0
|
575 /**
|
Chris@0
|
576 * Returns whether the view is enabled.
|
Chris@0
|
577 *
|
Chris@0
|
578 * @param \Drupal\views\Entity\View $view
|
Chris@0
|
579 * The view object to check.
|
Chris@0
|
580 *
|
Chris@0
|
581 * @return bool
|
Chris@0
|
582 * Returns TRUE if a view is enabled, FALSE otherwise.
|
Chris@0
|
583 */
|
Chris@0
|
584 function views_view_is_enabled(View $view) {
|
Chris@0
|
585 return $view->status();
|
Chris@0
|
586 }
|
Chris@0
|
587
|
Chris@0
|
588 /**
|
Chris@0
|
589 * Returns whether the view is disabled.
|
Chris@0
|
590 *
|
Chris@0
|
591 * @param \Drupal\views\Entity\View $view
|
Chris@0
|
592 * The view object to check.
|
Chris@0
|
593 *
|
Chris@0
|
594 * @return bool
|
Chris@0
|
595 * Returns TRUE if a view is disabled, FALSE otherwise.
|
Chris@0
|
596 */
|
Chris@0
|
597 function views_view_is_disabled(View $view) {
|
Chris@0
|
598 return !$view->status();
|
Chris@0
|
599 }
|
Chris@0
|
600
|
Chris@0
|
601 /**
|
Chris@0
|
602 * Enables and saves a view.
|
Chris@0
|
603 *
|
Chris@0
|
604 * @param \Drupal\views\Entity\View $view
|
Chris@0
|
605 * The View object to disable.
|
Chris@0
|
606 */
|
Chris@0
|
607 function views_enable_view(View $view) {
|
Chris@0
|
608 $view->enable()->save();
|
Chris@0
|
609 }
|
Chris@0
|
610
|
Chris@0
|
611 /**
|
Chris@0
|
612 * Disables and saves a view.
|
Chris@0
|
613 *
|
Chris@0
|
614 * @param \Drupal\views\Entity\View $view
|
Chris@0
|
615 * The View object to disable.
|
Chris@0
|
616 */
|
Chris@0
|
617 function views_disable_view(View $view) {
|
Chris@0
|
618 $view->disable()->save();
|
Chris@0
|
619 }
|
Chris@0
|
620
|
Chris@0
|
621 /**
|
Chris@0
|
622 * Replaces views substitution placeholders.
|
Chris@0
|
623 *
|
Chris@0
|
624 * @param array $element
|
Chris@0
|
625 * An associative array containing the properties of the element.
|
Chris@0
|
626 * Properties used: #substitutions, #children.
|
Chris@0
|
627 * @return array
|
Chris@0
|
628 * The $element with prepared variables ready for #theme 'form'
|
Chris@0
|
629 * in views_form_views_form.
|
Chris@0
|
630 */
|
Chris@0
|
631 function views_pre_render_views_form_views_form($element) {
|
Chris@0
|
632 // Placeholders and their substitutions (usually rendered form elements).
|
Chris@0
|
633 $search = [];
|
Chris@0
|
634 $replace = [];
|
Chris@0
|
635
|
Chris@0
|
636 // Add in substitutions provided by the form.
|
Chris@0
|
637 foreach ($element['#substitutions']['#value'] as $substitution) {
|
Chris@0
|
638 $field_name = $substitution['field_name'];
|
Chris@0
|
639 $row_id = $substitution['row_id'];
|
Chris@0
|
640
|
Chris@0
|
641 $search[] = $substitution['placeholder'];
|
Chris@0
|
642 $replace[] = isset($element[$field_name][$row_id]) ? \Drupal::service('renderer')->render($element[$field_name][$row_id]) : '';
|
Chris@0
|
643 }
|
Chris@0
|
644 // Add in substitutions from hook_views_form_substitutions().
|
Chris@0
|
645 $substitutions = \Drupal::moduleHandler()->invokeAll('views_form_substitutions');
|
Chris@0
|
646 foreach ($substitutions as $placeholder => $substitution) {
|
Chris@0
|
647 $search[] = Html::escape($placeholder);
|
Chris@0
|
648 // Ensure that any replacements made are safe to make.
|
Chris@0
|
649 if (!($substitution instanceof MarkupInterface)) {
|
Chris@0
|
650 $substitution = Html::escape($substitution);
|
Chris@0
|
651 }
|
Chris@0
|
652 $replace[] = $substitution;
|
Chris@0
|
653 }
|
Chris@0
|
654
|
Chris@0
|
655 // Apply substitutions to the rendered output.
|
Chris@0
|
656 $output = str_replace($search, $replace, \Drupal::service('renderer')->render($element['output']));
|
Chris@0
|
657 $element['output'] = ['#markup' => ViewsRenderPipelineMarkup::create($output)];
|
Chris@0
|
658
|
Chris@0
|
659 return $element;
|
Chris@0
|
660 }
|
Chris@0
|
661
|
Chris@0
|
662 /**
|
Chris@0
|
663 * Implements hook_form_alter() for the exposed form.
|
Chris@0
|
664 *
|
Chris@0
|
665 * Since the exposed form is a GET form, we don't want it to send a wide
|
Chris@0
|
666 * variety of information.
|
Chris@0
|
667 */
|
Chris@0
|
668 function views_form_views_exposed_form_alter(&$form, FormStateInterface $form_state) {
|
Chris@0
|
669 $form['form_build_id']['#access'] = FALSE;
|
Chris@0
|
670 $form['form_token']['#access'] = FALSE;
|
Chris@0
|
671 $form['form_id']['#access'] = FALSE;
|
Chris@0
|
672 }
|
Chris@0
|
673
|
Chris@0
|
674 /**
|
Chris@0
|
675 * Implements hook_query_TAG_alter().
|
Chris@0
|
676 *
|
Chris@0
|
677 * This is the hook_query_alter() for queries tagged by Views and is used to
|
Chris@0
|
678 * add in substitutions from hook_views_query_substitutions().
|
Chris@0
|
679 */
|
Chris@0
|
680 function views_query_views_alter(AlterableInterface $query) {
|
Chris@0
|
681 $substitutions = $query->getMetaData('views_substitutions');
|
Chris@0
|
682 $tables = &$query->getTables();
|
Chris@0
|
683 $where = &$query->conditions();
|
Chris@0
|
684
|
Chris@0
|
685 // Replaces substitutions in tables.
|
Chris@0
|
686 foreach ($tables as $table_name => $table_metadata) {
|
Chris@0
|
687 foreach ($table_metadata['arguments'] as $replacement_key => $value) {
|
Chris@0
|
688 if (!is_array($value)) {
|
Chris@0
|
689 if (isset($substitutions[$value])) {
|
Chris@0
|
690 $tables[$table_name]['arguments'][$replacement_key] = $substitutions[$value];
|
Chris@0
|
691 }
|
Chris@0
|
692 }
|
Chris@0
|
693 else {
|
Chris@0
|
694 foreach ($value as $sub_key => $sub_value) {
|
Chris@0
|
695 if (isset($substitutions[$sub_value])) {
|
Chris@0
|
696 $tables[$table_name]['arguments'][$replacement_key][$sub_key] = $substitutions[$sub_value];
|
Chris@0
|
697 }
|
Chris@0
|
698 }
|
Chris@0
|
699 }
|
Chris@0
|
700 }
|
Chris@0
|
701 }
|
Chris@0
|
702
|
Chris@0
|
703 // Replaces substitutions in filter criteria.
|
Chris@0
|
704 _views_query_tag_alter_condition($query, $where, $substitutions);
|
Chris@0
|
705 }
|
Chris@0
|
706
|
Chris@0
|
707 /**
|
Chris@0
|
708 * Replaces the substitutions recursive foreach condition.
|
Chris@0
|
709 */
|
Chris@0
|
710 function _views_query_tag_alter_condition(AlterableInterface $query, &$conditions, $substitutions) {
|
Chris@0
|
711 foreach ($conditions as $condition_id => &$condition) {
|
Chris@0
|
712 if (is_numeric($condition_id)) {
|
Chris@0
|
713 if (is_string($condition['field'])) {
|
Chris@0
|
714 $condition['field'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['field']);
|
Chris@0
|
715 }
|
Chris@0
|
716 elseif (is_object($condition['field'])) {
|
Chris@0
|
717 $sub_conditions = &$condition['field']->conditions();
|
Chris@0
|
718 _views_query_tag_alter_condition($query, $sub_conditions, $substitutions);
|
Chris@0
|
719 }
|
Chris@0
|
720 // $condition['value'] is a subquery so alter the subquery recursive.
|
Chris@0
|
721 // Therefore make sure to get the metadata of the main query.
|
Chris@0
|
722 if (is_object($condition['value'])) {
|
Chris@0
|
723 $subquery = $condition['value'];
|
Chris@0
|
724 $subquery->addMetaData('views_substitutions', $query->getMetaData('views_substitutions'));
|
Chris@0
|
725 views_query_views_alter($condition['value']);
|
Chris@0
|
726 }
|
Chris@0
|
727 elseif (isset($condition['value'])) {
|
Chris@0
|
728 // We can not use a simple str_replace() here because it always returns
|
Chris@0
|
729 // a string and we have to keep the type of the condition value intact.
|
Chris@0
|
730 if (is_array($condition['value'])) {
|
Chris@0
|
731 foreach ($condition['value'] as &$value) {
|
Chris@0
|
732 if (is_string($value)) {
|
Chris@0
|
733 $value = str_replace(array_keys($substitutions), array_values($substitutions), $value);
|
Chris@0
|
734 }
|
Chris@0
|
735 }
|
Chris@0
|
736 }
|
Chris@0
|
737 elseif (is_string($condition['value'])) {
|
Chris@0
|
738 $condition['value'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['value']);
|
Chris@0
|
739 }
|
Chris@0
|
740 }
|
Chris@0
|
741 }
|
Chris@0
|
742 }
|
Chris@0
|
743 }
|
Chris@0
|
744
|
Chris@0
|
745 /**
|
Chris@0
|
746 * Embed a view using a PHP snippet.
|
Chris@0
|
747 *
|
Chris@0
|
748 * This function is meant to be called from PHP snippets, should one wish to
|
Chris@0
|
749 * embed a view in a node or something. It's meant to provide the simplest
|
Chris@0
|
750 * solution and doesn't really offer a lot of options, but breaking the function
|
Chris@0
|
751 * apart is pretty easy, and this provides a worthwhile guide to doing so.
|
Chris@0
|
752 *
|
Chris@0
|
753 * Note that this function does NOT display the title of the view. If you want
|
Chris@0
|
754 * to do that, you will need to do what this function does manually, by
|
Chris@0
|
755 * loading the view, getting the preview and then getting $view->getTitle().
|
Chris@0
|
756 *
|
Chris@0
|
757 * @param $name
|
Chris@0
|
758 * The name of the view to embed.
|
Chris@0
|
759 * @param $display_id
|
Chris@0
|
760 * The display id to embed. If unsure, use 'default', as it will always be
|
Chris@0
|
761 * valid. But things like 'page' or 'block' should work here.
|
Chris@0
|
762 * @param ...
|
Chris@0
|
763 * Any additional parameters will be passed as arguments.
|
Chris@0
|
764 *
|
Chris@0
|
765 * @return array|null
|
Chris@0
|
766 * A renderable array containing the view output or NULL if the display ID
|
Chris@0
|
767 * of the view to be executed doesn't exist.
|
Chris@0
|
768 */
|
Chris@0
|
769 function views_embed_view($name, $display_id = 'default') {
|
Chris@0
|
770 $args = func_get_args();
|
Chris@0
|
771 // Remove $name and $display_id from the arguments.
|
Chris@0
|
772 unset($args[0], $args[1]);
|
Chris@0
|
773
|
Chris@0
|
774 $view = Views::getView($name);
|
Chris@0
|
775 if (!$view || !$view->access($display_id)) {
|
Chris@0
|
776 return;
|
Chris@0
|
777 }
|
Chris@0
|
778
|
Chris@0
|
779 return [
|
Chris@0
|
780 '#type' => 'view',
|
Chris@0
|
781 '#name' => $name,
|
Chris@0
|
782 '#display_id' => $display_id,
|
Chris@0
|
783 '#arguments' => $args,
|
Chris@0
|
784 ];
|
Chris@0
|
785 }
|
Chris@0
|
786
|
Chris@0
|
787 /**
|
Chris@0
|
788 * Get the result of a view.
|
Chris@0
|
789 *
|
Chris@0
|
790 * @param string $name
|
Chris@0
|
791 * The name of the view to retrieve the data from.
|
Chris@0
|
792 * @param string $display_id
|
Chris@0
|
793 * The display id. On the edit page for the view in question, you'll find
|
Chris@0
|
794 * a list of displays at the left side of the control area. "Master"
|
Chris@0
|
795 * will be at the top of that list. Hover your cursor over the name of the
|
Chris@0
|
796 * display you want to use. A URL will appear in the status bar of your
|
Chris@0
|
797 * browser. This is usually at the bottom of the window, in the chrome.
|
Chris@0
|
798 * Everything after #views-tab- is the display ID, e.g. page_1.
|
Chris@0
|
799 * @param ...
|
Chris@0
|
800 * Any additional parameters will be passed as arguments.
|
Chris@0
|
801 * @return array
|
Chris@0
|
802 * An array containing an object for each view item.
|
Chris@0
|
803 */
|
Chris@0
|
804 function views_get_view_result($name, $display_id = NULL) {
|
Chris@0
|
805 $args = func_get_args();
|
Chris@0
|
806 // Remove $name and $display_id from the arguments.
|
Chris@0
|
807 unset($args[0], $args[1]);
|
Chris@0
|
808
|
Chris@0
|
809 $view = Views::getView($name);
|
Chris@0
|
810 if (is_object($view)) {
|
Chris@0
|
811 if (is_array($args)) {
|
Chris@0
|
812 $view->setArguments($args);
|
Chris@0
|
813 }
|
Chris@0
|
814 if (is_string($display_id)) {
|
Chris@0
|
815 $view->setDisplay($display_id);
|
Chris@0
|
816 }
|
Chris@0
|
817 else {
|
Chris@0
|
818 $view->initDisplay();
|
Chris@0
|
819 }
|
Chris@0
|
820 $view->preExecute();
|
Chris@0
|
821 $view->execute();
|
Chris@0
|
822 return $view->result;
|
Chris@0
|
823 }
|
Chris@0
|
824 else {
|
Chris@0
|
825 return [];
|
Chris@0
|
826 }
|
Chris@0
|
827 }
|
Chris@0
|
828
|
Chris@0
|
829 /**
|
Chris@0
|
830 * Validation callback for query tags.
|
Chris@0
|
831 */
|
Chris@0
|
832 function views_element_validate_tags($element, FormStateInterface $form_state) {
|
Chris@0
|
833 $values = array_map('trim', explode(',', $element['#value']));
|
Chris@0
|
834 foreach ($values as $value) {
|
Chris@0
|
835 if (preg_match("/[^a-z_]/", $value)) {
|
Chris@0
|
836 $form_state->setError($element, t('The query tags may only contain lower-case alphabetical characters and underscores.'));
|
Chris@0
|
837 return;
|
Chris@0
|
838 }
|
Chris@0
|
839 }
|
Chris@0
|
840 }
|
Chris@0
|
841
|
Chris@0
|
842 /**
|
Chris@0
|
843 * Implements hook_local_tasks_alter().
|
Chris@0
|
844 */
|
Chris@0
|
845 function views_local_tasks_alter(&$local_tasks) {
|
Chris@0
|
846 $container = \Drupal::getContainer();
|
Chris@0
|
847 $local_task = ViewsLocalTask::create($container, 'views_view');
|
Chris@0
|
848 $local_task->alterLocalTasks($local_tasks);
|
Chris@0
|
849 }
|
Chris@0
|
850
|
Chris@0
|
851 /**
|
Chris@0
|
852 * Implements hook_ENTITY_TYPE_delete().
|
Chris@0
|
853 */
|
Chris@0
|
854 function views_view_delete(EntityInterface $entity) {
|
Chris@0
|
855 // Rebuild the routes in case there is a routed display.
|
Chris@0
|
856 $executable = Views::executableFactory()->get($entity);
|
Chris@0
|
857 $executable->initDisplay();
|
Chris@0
|
858 foreach ($executable->displayHandlers as $display) {
|
Chris@0
|
859 if ($display->getRoutedDisplay()) {
|
Chris@0
|
860 \Drupal::service('router.builder')->setRebuildNeeded();
|
Chris@0
|
861 break;
|
Chris@0
|
862 }
|
Chris@0
|
863 }
|
Chris@0
|
864 }
|
Chris@14
|
865
|
Chris@14
|
866 /**
|
Chris@18
|
867 * Implements hook_ENTITY_TYPE_presave().
|
Chris@14
|
868 *
|
Chris@14
|
869 * Provides a BC layer for modules providing old configurations.
|
Chris@14
|
870 */
|
Chris@14
|
871 function views_view_presave(ViewEntityInterface $view) {
|
Chris@14
|
872 $displays = $view->get('display');
|
Chris@14
|
873 $changed = FALSE;
|
Chris@14
|
874 foreach ($displays as $display_name => &$display) {
|
Chris@14
|
875 if (isset($display['display_options']['fields'])) {
|
Chris@14
|
876 foreach ($display['display_options']['fields'] as $field_name => &$field) {
|
Chris@14
|
877 if (isset($field['plugin_id']) && $field['plugin_id'] === 'entity_link') {
|
Chris@14
|
878 // Add any missing settings for entity_link.
|
Chris@14
|
879 if (!isset($field['output_url_as_text'])) {
|
Chris@14
|
880 $field['output_url_as_text'] = FALSE;
|
Chris@14
|
881 $changed = TRUE;
|
Chris@14
|
882 }
|
Chris@14
|
883 if (!isset($field['absolute'])) {
|
Chris@14
|
884 $field['absolute'] = FALSE;
|
Chris@14
|
885 $changed = TRUE;
|
Chris@14
|
886 }
|
Chris@14
|
887 }
|
Chris@14
|
888 elseif (isset($field['plugin_id']) && $field['plugin_id'] === 'node_path') {
|
Chris@14
|
889 // Convert the use of node_path to entity_link.
|
Chris@14
|
890 $field['plugin_id'] = 'entity_link';
|
Chris@14
|
891 $field['field'] = 'view_node';
|
Chris@14
|
892 $field['output_url_as_text'] = TRUE;
|
Chris@14
|
893 $changed = TRUE;
|
Chris@14
|
894 }
|
Chris@14
|
895 }
|
Chris@14
|
896 }
|
Chris@14
|
897 }
|
Chris@14
|
898 if ($changed) {
|
Chris@14
|
899 $view->set('display', $displays);
|
Chris@14
|
900 }
|
Chris@14
|
901 }
|