Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / draggableviews / draggableviews.module
1 <?php
2
3 /**
4  * @file
5  * Contains draggableviews.module.
6  */
7
8 /**
9  * Implements hook_views_data_alter().
10  */
11 function draggableviews_views_data_alter(&$data) {
12   $data['draggableviews_structure']['weight'] = array(
13     'title' => t('DraggableViews Weight'),
14     'group' => t('Global'),
15     'help' => t('Display the weight value.'),
16     'field' => array(
17       'id' => 'numeric',
18     ),
19     'sort' => array(
20       'id' => 'standard',
21     ),
22     'filter' => array(
23       'help' => t('Filter by the draggableviews weight value (Native handler only).'),
24       'id' => 'numeric',
25     ),
26   );
27   $data['draggableviews_structure']['parent'] = array(
28     'title' => t('Parent'),
29     'help' => t('The parent entity id.'),
30     'group' => t('Draggableviews'),
31     'field' => array(
32       'id' => 'numeric',
33     ),
34     'filter' => array(
35       'help' => t('Filter by the draggableviews parent\'s entity id (Native handler only).'),
36       'id' => 'numeric',
37     ),
38   );
39
40   foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
41     $base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
42     $entity_keys = $entity_type->getKeys();
43     if ($base_table && isset($data[$base_table]['table'])) {
44       $data[$base_table]['draggableviews'] = array(
45         'title' => $data[$base_table]['table']['group'],
46         'group' => t('Draggableviews'),
47         'help' => t('Provide a draggable functionality.'),
48         'entity field' => $entity_keys['id'],
49         'field' => array(
50           'id' => 'draggable_views_field',
51           'click sortable' => FALSE,
52         ),
53       );
54       // Explain to every entity how to join with draggableviews structure table.
55       $data['draggableviews_structure']['table']['join'][$base_table] = array(
56         'handler' => 'draggableviews_join_handler',
57         // Because this is a direct link it could be left out.
58         'left_table' => $base_table,
59         'left_field' => $entity_keys['id'],
60         'field' => 'entity_id',
61         'extra' => array(
62           array('field' => 'view_name', 'value' => '***VIEW_ID***'),
63           array('field' => 'view_display', 'value' => '***VIEW_DISPLAY***'),
64         ),
65       );
66     }
67   }
68 }
69
70 /**
71  * Implements hook_views_query_substitutions().
72  *
73  * Allow replacement of current userid so we can cache these queries.
74  */
75 function draggableviews_views_query_substitutions(\Drupal\views\ViewExecutable $view) {
76   return array('***VIEW_ID***' => $view->id(), '***VIEW_DISPLAY***' => $view->current_display);
77 }
78
79 /**
80  * Implements hook_preprocess_views_view_table().
81  */
82 function draggableviews_preprocess_views_view_table(&$variables) {
83   $view = $variables['view'];
84   if (!isset($view->field['draggableviews'])) {
85     return;
86   }
87
88   $draggableviews = new \Drupal\draggableviews\DraggableViews($variables['view']);
89
90   // Add hierarchy.
91   foreach ($variables['rows'] as $key => $row) {
92     $title = $row['columns']['title']['content'][0]['field_output']['#markup'];
93     $indent = [
94       '#theme' => 'indentation',
95       '#size' => $draggableviews->getDepth($key),
96     ];
97     $variables['rows'][$key]['columns']['title']['content'][0]['field_output']['#markup'] = render($indent) . $title;
98   }
99
100   // Add table attributes.
101   $variables['attributes']['id'] = $draggableviews->getHtmlId();
102
103   // Add rows attributes.
104   foreach ($variables['rows'] as &$row) {
105     $row['attributes']->addClass('draggable');
106   }
107   unset($row);
108 }
109
110 /**
111  * Implements hook_form_alter().
112  */
113 function draggableviews_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
114   // Filter the right form.
115   if (strpos($form_id, 'views_form_') === FALSE) {
116     return;
117   }
118
119   // Check whether the view is draggable.
120   $view = $form_state->getBuildInfo()['args'][0];
121   if (!isset($view->field['draggableviews'])) {
122     return;
123   }
124
125   // Remove default submit button.
126   $form['actions']['submit']['#access'] = FALSE;
127
128   if (\Drupal::currentUser()->hasPermission('access draggableviews')) {
129     // Create draggableviews save order button.
130     $form['actions']['save_order'] = array(
131       '#value' => t('Save order'),
132       '#type' => 'submit',
133     );
134   }
135
136   // If there is no results remove the save-order button.
137   if (!isset($form['draggableviews'][0])) {
138     $form['actions']['save_order']['#access'] = FALSE;
139     return;
140   }
141
142   $form['actions']['save_order']['#submit'][] = 'draggableviews_views_submit';
143 }
144
145 /**
146  * Submit handler.
147  */
148 function draggableviews_views_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
149   $input = $form_state->getUserInput();
150
151   /** @var \Drupal\views\ViewExecutable $view */
152   $view = $form_state->getBuildInfo()['args'][0];
153   $view_name = $view->id();
154   $view_display = $view->current_display;
155
156   $weight = 0;
157
158   $connection = \Drupal\Core\Database\Database::getConnection();
159   $transaction = $connection->startTransaction();
160   try {
161     foreach ($input['draggableviews'] as $item) {
162       // Remove old data.
163       $connection->delete('draggableviews_structure')
164         ->condition('view_name', $view_name)
165         ->condition('view_display', $view_display)
166         ->condition('entity_id', $item['id'])
167         ->execute();
168
169       // Add new data.
170       $record = [
171         'view_name' => $view_name,
172         'view_display' => $view_display,
173         'args' => '[]',
174         'entity_id' => $item['id'],
175         'weight' => $weight,
176       ];
177       // Save parent if exists.
178       if (isset($item['parent'])) {
179         $record['parent'] = $item['parent'];
180       }
181       $connection->insert('draggableviews_structure')->fields($record)->execute();
182       $weight++;
183     }
184     // We invalidate the entity list cache, so other views are also aware of the
185     // cache.
186     $views_entity_table_info = $view->query->getEntityTableInfo();
187     // Find the entity type used by the view.
188     $result = array_keys(array_filter($views_entity_table_info, function($info) {
189       return $info['relationship_id'] == 'none';
190     }));
191     $entity_type_id = reset($result);
192     $list_cache_tags = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getListCacheTags();
193     \Drupal\Core\Cache\Cache::invalidateTags($list_cache_tags);
194   }
195   catch (\Exception $e) {
196     $transaction->rollback();
197     \Drupal::logger('draggableviews')->error('Failed with @message', ['@message' => $e->getMessage()]);
198     drupal_set_message(t('There was an error while saving the data. Please, try gain.'), 'warning');
199   }
200 }