forked from BurdaMagazinOrg/module-nexx_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexx_integration.drush.inc
181 lines (155 loc) · 5.14 KB
/
nexx_integration.drush.inc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @file
* Drush commands for the nexx_integration module.
*/
use Drupal\media_entity\MediaStorageInterface;
use Drupal\media_entity\Entity\Media;
/**
* Implements hook_drush_commands().
*/
function nexx_integration_drush_command() {
$items = [];
$items['nexx-export-taxonomy'] = [
'description' => "Export taxonomy terms to omnia. This inserts all terms into Omnia, it cannot check if they already exist.",
'arguments' => [
'source_vocabulary' => 'Drupal vocabulary to export.',
],
'examples' => [
'drush -l http://example.com nexx-export-taxonomy category' => 'Export drupal categories vocabulary to omnia',
],
];
$items['nexx-perform-unpublish-delete'] = [
'description' => "Delete and unpublish old videos which should be unpublished or deleted.",
];
return $items;
}
/**
* Implements the nexx-export-taxonomy drush command.
*
* @param string $source_vocabulary
* The drupal vocabulary to export.
*/
function drush_nexx_integration_nexx_export_taxonomy($source_vocabulary) {
$type_manager = \Drupal::entityTypeManager();
$vocabulary = $type_manager->getStorage("taxonomy_vocabulary")->load($source_vocabulary);
if (empty($vocabulary)) {
drush_set_error(dt("Given vocabulary !vocabulary does not exist.", ['!vocabulary' => $source_vocabulary]));
exit;
}
$terms = $type_manager
->getStorage("taxonomy_term")
->loadTree($source_vocabulary, 0, NULL, TRUE);
foreach ($terms as $term) {
nexx_integration_taxonomy_term_update($term);
}
}
/**
* Implements the nexx-perform-unpublish-delete drush command.
*
* @param int $per_batch
* Limit items processed per batch.
*/
function drush_nexx_integration_nexx_perform_unpublish_delete($per_batch = 20) {
$field_manager = \Drupal::service("entity_field.manager");
$type = "media";
if (!$bundle = \Drupal::config('nexx_integration.settings')->get('video_bundle')) {
drush_set_error(dt("There is no video bundle setup. Please configure module first."));
exit;
}
$field_definitions = $field_manager->getFieldDefinitions($type, $bundle);
foreach ($field_definitions as $field_name => $field_definition) {
if ($field_definition->getType() === 'nexx_video_data') {
$video_field = $field_name;
break;
}
}
if (empty($video_field)) {
drush_set_error(dt("No video data field defined."));
exit;
}
$storage = \Drupal::entityTypeManager()->getStorage($type);
/** @var \Drupal\Core\Entity\Query\QueryInterface $query */
$query = $storage->getQuery();
// Unpublish videos which should be because of active or isSSC attribute.
$group = $query->orConditionGroup()
->condition($video_field . '.active', 0)
->condition($video_field . '.isSSC', 0)
->condition($video_field . '.deleted', 1);
$ids = $query
->condition($group)
->condition("status", Media::PUBLISHED)
->execute();
$chunks = array_chunk(array_values($ids), $per_batch);
$operations = [];
foreach ($chunks as $chunk) {
$operations[] = [
"_nexx_integration_drush_batch_op",
[
$chunk,
$storage,
$video_field,
],
];
}
$batch = [
'operations' => $operations,
'title' => t('Process all old videos'),
'init_message' => t('Initializing'),
'error_message' => t('An error occurred'),
'finished' => '_nexx_integration_drush_batch_finished',
];
batch_set($batch);
$batch =& batch_get();
$batch['progressive'] = FALSE;
// Start processing.
drush_backend_batch_process();
}
/**
* Perform unpublish/delete on every video in batch.
*
* @param array $ids
* List of video ids.
* @param Drupal\media_entity\MediaStorageInterface $storage
* Media storage service.
* @param string $video_field
* Video field name.
* @param object $sandbox
* Sandbox array.
*/
function _nexx_integration_drush_batch_op(array $ids, MediaStorageInterface $storage, $video_field, &$sandbox) {
/** @var \Drupal\media_entity\MediaInterface $video */
foreach ($storage->loadMultiple($ids) as $video) {
/** @var \Drupal\Core\Field\FieldItemInterface $video_data */
$video_data = $video->get($video_field)->first();
if ($video_data->get("isDeleted")->getString() == 1) {
drush_log(dt('Deleting video ' . $video->id()), "ok");
$video->delete();
}
elseif ($video_data->get("active")->getString() == 0
|| $video_data->get("isSSC")->getString() == 0
) {
$video->set("status", Media::NOT_PUBLISHED);
$video->save();
drush_log(dt('Unpublishing video ' . $video->id()), "ok");
}
else {
drush_log(dt('No action video ' . $video->id()), "warning");
}
}
drush_log(dt('Batch finished...'), "ok");
}
/**
* Perform this function after all batches are done.
*
* @param bool $success
* Indicate that the batch API tasks were all completed successfully.
* @param array $results
* An array of all the results that were updated in update_do_one().
* @param array $operations
* A list of all the operations that had not been completed by the batch
* API.
*/
function _nexx_integration_drush_batch_finished($success, array $results, array $operations) {
drush_log(dt('Operation finished'), "ok");
}