cdb99cc100f667049695c80cafd7eb972a292d46
[yaffs-website] / web / core / modules / views / tests / modules / views_test_data / views_test_data.module
1 <?php
2
3 /**
4  * @file
5  * Helper module for Views tests.
6  */
7
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * Access callback for the generic handler test.
12  *
13  * @return bool
14  *   Returns views_test_data.tests->handler_access_callback config. so the user
15  *   has access to the handler.
16  *
17  * @see \Drupal\views\Tests\Handler\HandlerTest
18  */
19 function views_test_data_handler_test_access_callback() {
20   return \Drupal::config('views_test_data.tests')->get('handler_access_callback');
21 }
22
23 /**
24  * Access callback with an argument for the generic handler test.
25  *
26  * @param bool $argument
27  *   A parameter to test that an argument got passed.
28  *
29  * @return bool
30  *   Returns views_test_data.tests->handler_access_callback_argument, so the
31  *   use has access to the handler.
32  *
33  * @see \Drupal\views\Tests\Handler\HandlerTest
34  */
35 function views_test_data_handler_test_access_callback_argument($argument = FALSE) {
36   // Check the argument to be sure that access arguments are passed into the
37   // callback.
38   if ($argument) {
39     return \Drupal::config('views_test_data.tests')->get('handler_access_callback_argument');
40   }
41   else {
42     return FALSE;
43   }
44 }
45
46 /**
47  * Implements hook_preprocess_HOOK() for views table templates.
48  */
49 function views_test_data_preprocess_views_view_table(&$variables) {
50   if ($variables['view']->storage->id() == 'test_view_render') {
51     $views_render_test = \Drupal::state()->get('views_render.test');
52     $views_render_test++;
53     \Drupal::state()->set('views_render.test', $views_render_test);
54   }
55 }
56
57 /**
58  * Prepares variables for the mapping row style test templates.
59  *
60  * Default template: views-view-mapping-test.html.twig.
61  *
62  * @param array $variables
63  *   An associative array containing:
64  *   - rows: A list of view rows.
65  *   - options: Various view options, including the row style mapping.
66  *   - view: The view object.
67  */
68 function template_preprocess_views_view_mapping_test(&$variables) {
69   $variables['element'] = [];
70
71   foreach ($variables['rows'] as $delta => $row) {
72     $fields = [];
73     foreach ($variables['options']['mapping'] as $type => $field_names) {
74       if (!is_array($field_names)) {
75         $field_names = [$field_names];
76       }
77       foreach ($field_names as $field_name) {
78         if ($value = $variables['view']->style_plugin->getField($delta, $field_name)) {
79           $fields[$type . '-' . $field_name] = $type . ':' . $value;
80         }
81       }
82     }
83
84     // If there are no fields in this row, skip to the next one.
85     if (empty($fields)) {
86       continue;
87     }
88
89     // Build a container for the row.
90     $variables['element'][$delta] = [
91       '#type' => 'container',
92       '#attributes' => [
93         'class' => [
94           'views-row-mapping-test',
95         ],
96       ],
97     ];
98
99     // Add each field to the row.
100     foreach ($fields as $key => $render) {
101       $variables['element'][$delta][$key] = [
102         '#children' => $render,
103         '#type' => 'container',
104         '#attributes' => [
105           'class' => [
106             $key,
107           ],
108         ],
109       ];
110     }
111   }
112 }
113
114 /**
115  * Test pre_render function.
116  *
117  * @param array $element
118  *   A render array
119  *
120  * @return array
121  *   The changed render array.
122  */
123 function views_test_data_test_pre_render_function($element) {
124   $element['#markup'] = 'views_test_data_test_pre_render_function executed';
125   return $element;
126 }
127
128 /**
129  * Implements hook_form_BASE_FORM_ID_alter().
130  */
131 function views_test_data_form_views_form_test_form_multiple_default_alter(&$form, FormStateInterface $form_state, $form_id) {
132   drupal_set_message(t('Test base form ID with Views forms and arguments.'));
133 }