-
Notifications
You must be signed in to change notification settings - Fork 0
/
drupal_document.module
165 lines (154 loc) · 6.27 KB
/
drupal_document.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
<?php
/**
* @file
* Basic module file for drupal_document module.
*/
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\drupal_document\Form\DownloadDocumentsForm;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\Field;
/**
* Implements hook_theme().
*/
function drupal_document_theme($existing, $type, $theme, $path) {
return [
'dropdown_file_language' => [
'template' => 'dropdown-file-language',
'variables' => ['items' => NULL, 'object' => NULL, 'attributes' => []],
],
'files_group_by_language' => [
'template' => 'files-group-by-language',
'variables' => [
'rows' => [],
'current_language' => [],
],
],
'download_modal' => [
'template' => 'download-modal',
'variables' => ['button' => NULL, 'object' => NULL, 'attributes' => []],
],
];
}
/**
* Do not index langcode when file is not displayed.
*
* Implements hook_search_api_index_items_alter().
*/
function drupal_document_search_api_index_items_alter(IndexInterface $index, array &$items) {
if (!\Drupal::moduleHandler()->moduleExists('search_api')) {
return;
}
$solrField = 'file_language';
/** @var \Drupal\search_api\Item\Item $item */
foreach ($items as &$item) {
/** @var \Drupal\search_api\Item\Field $languageField */
$languageField = $item->getField($solrField);
if (!$languageField instanceof Field) {
return;
}
$langcodes = $languageField->getValues();
$propertyPath = $item->getField($solrField)->getPropertyPath();
$property = explode(':', $propertyPath);
$fieldEntity = $property[0];
$entity = $item->getOriginalObject()->getValue();
if ($entity->hasField($fieldEntity)) {
$values = $entity->get($fieldEntity)->getValue();
foreach ($values as $value) {
if (!$value['display']) {
if (($key = array_search($value['language'], $langcodes)) !== FALSE) {
unset($langcodes[$key]);
}
}
}
$languageField->setValues($langcodes);
$item->setField($solrField, $languageField);
}
}
}
/**
* Implements hook_form_alter().
*/
function drupal_document_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$bulkFormId = isset($form['views_bulk_operations_bulk_form']) ?
'views_bulk_operations_bulk_form' : (isset($form['node_bulk_form']) ? 'node_bulk_form' : NULL);
switch ($bulkFormId) {
case 'views_bulk_operations_bulk_form':
$actionExist = array_search('node_download_documents_action', array_column($form['output'][0]['#view']->field['views_bulk_operations_bulk_form']->options['selected_actions'], 'action_id'));
if (!is_int($actionExist)) {
return;
}
$actions = array_keys($form['output'][0]['#view']->field['views_bulk_operations_bulk_form']->options['selected_actions']);
$key = $actions[$actionExist];
$form['header'][$bulkFormId]['actions'][$key]['#ajax']['callback'] = '_drupal_document_node_download_documents_action';
$form['actions'][$key]['#ajax']['callback'] = '_drupal_document_node_download_documents_action';
$form['header'][$bulkFormId]['actions'][$key]['#value'] = new TranslatableMarkup('No documents selected');
$form['header'][$bulkFormId]['actions'][$key]['#attributes']['disabled'] = 'disabled';
$form['actions'][$key]['#value'] = new TranslatableMarkup('No documents selected');
$form['actions'][$key]['#attributes']['disabled'] = 'disabled';
break;
case 'node_bulk_form':
$actionExist = array_search('node_download_documents_action', $form['output'][0]['#view']->field['node_bulk_form']->options['selected_actions']);
if (!is_int($actionExist)) {
return;
}
$form['header'][$bulkFormId]['actions']['submit']['#ajax']['callback'] = '_drupal_document_node_download_documents_action';
$form['actions']['submit']['#ajax']['callback'] = '_drupal_document_node_download_documents_action';
$form['header'][$bulkFormId]['actions']['submit']['#value'] = new TranslatableMarkup('No documents selected');
$form['header'][$bulkFormId]['actions']['submit']['#attributes']['disabled'] = 'disabled';
$form['actions']['submit']['#value'] = new TranslatableMarkup('No documents selected');
$form['actions']['submit']['#attributes']['disabled'] = 'disabled';
break;
default:
return;
}
$form['#attached']['library'][] = 'drupal_document/documents_form';
// Class used to count how many documents are selected.
$form['#attributes']['class'][] = 'documents-bulk-form';
// Button to clear the checked items.
$form['header']['node_bulk_form']['actions']['clear'] = [
'#type' => 'link',
'#url' => Url::fromUserInput('#'),
'#title' => t('clear'),
'#attributes' => [
'class' => ['download-documents-clear-button', 'hidden'],
],
'#weight' => -100,
];
}
/**
* Ajax callback to open a modal with files from all selected ids.
*/
function _drupal_document_node_download_documents_action(&$form, FormStateInterface $form_state) {
$id = isset($form['views_bulk_operations_bulk_form']) ? 'views_bulk_operations_bulk_form' : 'node_bulk_form';
$isSearchApi = isset($form['views_bulk_operations_bulk_form']);
$encodedIds = $form_state->getUserInput()[$id];
if ($encodedIds) {
$ids = [];
/** @var \Drupal\drupal_document\Services\DocumentsBulkManager $bulkManager */
$bulkManager = \Drupal::service('document.bulk_manager');
foreach (array_filter($form_state->getUserInput()[$id]) as $hash) {
$entity = $bulkManager->loadEntityFromBulkFormKey($hash, $isSearchApi);
$ids[] = $entity->id();
}
$form = \Drupal::formBuilder()->getForm(DownloadDocumentsForm::class, $ids, 'field_files');
$response = new AjaxResponse();
$response->addCommand(new OpenModalDialogCommand(NULL, $form, [
'height' => 600,
'width' => 900,
'dialogClass' => 'no-titlebar no-close',
]));
return $response;
}
}
/**
* Implements hook_cron().
*/
function drupal_document_clean_document_cron() {
/** @var \Drupal\drupal_document\Services\DocumentCronManager $cronManager */
$cronManager = \Drupal::service('document.cron_manager');
$cronManager->cleaupOldDirectories();
}