forked from ymcatwincities/openy_gated_content
-
Notifications
You must be signed in to change notification settings - Fork 10
/
openy_gated_content.post_update.php
175 lines (156 loc) · 6.13 KB
/
openy_gated_content.post_update.php
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
<?php
/**
* @file
* Contains hook_post_update_NAME() implementations.
*/
/**
* Find gated content pages and update new config params with URL's.
*/
function openy_gated_content_post_update_create_login_page(&$sandbox) {
$prgf_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
$path_alias_manager = \Drupal::service('path_alias.manager');
$gated_content_prgf = $prgf_storage->loadByProperties(['type' => 'gated_content']);
if (!$gated_content_prgf) {
// There no Virtual Y on this site.
return;
}
$gated_content_prgf = reset($gated_content_prgf);
$gated_content_page = $gated_content_prgf->getParentEntity();
if (!$gated_content_page) {
return;
}
$gated_content_page_alias = $path_alias_manager->getAliasByPath('/node/' . $gated_content_page->id());
$gated_content_login_prgf = $prgf_storage->loadByProperties(['type' => 'gated_content_login']);
if (!$gated_content_login_prgf) {
// Create Virtual Y login page and paragraph.
$gated_content_login_prgf = $prgf_storage->create([
'type' => 'gated_content_login',
]);
$gated_content_login_page = FALSE;
}
else {
$gated_content_login_prgf = reset($gated_content_login_prgf);
$gated_content_login_page = $gated_content_login_prgf->getParentEntity();
}
if (!$gated_content_login_page) {
$gated_content_login_page = \Drupal::entityTypeManager()
->getStorage('node')
->create([
'type' => 'landing_page',
'title' => 'VIRTUAL Y Login',
'field_content' => [$gated_content_login_prgf],
]);
$gated_content_login_page->save();
}
$gated_content_login_alias = $path_alias_manager->getAliasByPath('/node/' . $gated_content_login_page->id());
$gated_content_settings = \Drupal::configFactory()->getEditable('openy_gated_content.settings');
$gated_content_settings->set('virtual_y_url', $gated_content_page_alias);
$gated_content_settings->set('virtual_y_login_url', $gated_content_login_alias);
$gated_content_settings->save();
}
/**
* Helper function for post updates of permissions field.
*/
function _openy_gated_content_permissions(&$sandbox, string $entity_type, string $bundle, $id = 'nid') {
if (!isset($sandbox['progress'])) {
$sandbox['max'] = \Drupal::entityQuery($entity_type)
->condition('type', $bundle)
->notExists('field_vy_permission')
->count()
->execute();
$sandbox['ids'] = \Drupal::entityQuery($entity_type)
->condition('type', $bundle)
->notExists('field_vy_permission')
->execute();
}
$ids = array_slice($sandbox['ids'], 0, 5);
// Doublecheck that ids are int, not string.
$ids = array_map(
function ($value) {
return (int) $value;
},
$ids
);
$nodes = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadMultiple($ids);
$not_existed = array_diff($ids, array_keys($nodes));
if (!empty($not_existed)) {
$sandbox['ids'] = array_diff($sandbox['ids'], $not_existed);
}
foreach ($nodes as $node) {
$node->field_vy_permission->value = 'virtual_y,virtual_y_premium';
$node->save();
$sandbox['ids'] = array_diff($sandbox['ids'], [$node->id()]);
}
$sandbox['#finished'] = (count($sandbox['ids']) === 0) ? TRUE : count($sandbox['ids']) / $sandbox['max'];
if ($sandbox['#finished']) {
return t('Fields data were migrated for @count entities', ['@count' => $sandbox['max']]);
}
}
/**
* Update all existed Virtual Y Videos with default permissions field.
*/
function openy_gated_content_post_update_permissions_video(&$sandbox) {
_openy_gated_content_permissions($sandbox, 'node', 'gc_video');
}
/**
* Update all existed Virtual Y Blog Posts with default permissions field.
*/
function openy_gated_content_post_update_permissions_blog_posts(&$sandbox) {
_openy_gated_content_permissions($sandbox, 'node', 'vy_blog_post');
}
/**
* Update all existed Virtual Y Online streams with default permissions field.
*/
function openy_gated_content_post_update_eventseries_livestream(&$sandbox) {
_openy_gated_content_permissions($sandbox, 'eventseries', 'live_stream', 'id');
}
/**
* Update all existed Virtual Y Virtual meetings with default permissions field.
*/
function openy_gated_content_post_update_eventseries_meeting(&$sandbox) {
_openy_gated_content_permissions($sandbox, 'eventseries', 'virtual_meeting', 'id');
}
/**
* Set gated_content paragraphs title and description if it was empty.
*/
function openy_gated_content_post_update_paragraph_headline(&$sandbox) {
$prgf_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
$gated_content_prgf = $prgf_storage->loadByProperties(['type' => 'gated_content']);
$gated_content_prgf = end($gated_content_prgf);
if ($gated_content_prgf) {
$page_id = $gated_content_prgf->parent_id->value;
$header_prgf = $prgf_storage->loadByProperties([
'type' => ['small_banner', 'banner'],
'parent_id' => $page_id,
]
);
$header_prgf = end($header_prgf);
$title = 'Virtual YMCA';
$description = '<p>Find the newest Y classes and programs</p><p><a class="btn btn-primary" href="#/live-stream"><span class="text">Live Streams</span></a> <a class="btn btn-primary" href="#/categories/video"><span class="text">Videos</span></a></p>';
if ($header_prgf) {
if (!empty($header_prgf->field_prgf_headline->value)) {
$title = $header_prgf->field_prgf_headline->value;
}
if (!empty($header_prgf->field_prgf_image->first())) {
$image = $header_prgf->field_prgf_image->first();
}
if (!empty($header_prgf->field_prgf_description->value)) {
$description = $header_prgf->field_prgf_description->value;
}
$header_prgf->delete();
}
if (empty($gated_content_prgf->field_prgf_title->value)) {
$gated_content_prgf->field_prgf_title->value = $title;
}
if (empty($gated_content_prgf->field_prgf_description->value)) {
$gated_content_prgf->field_prgf_description->value = $description;
$gated_content_prgf->field_prgf_description->format = 'full_html';
}
if (empty($gated_content_prgf->field_prgf_image->first())) {
$gated_content_prgf->field_prgf_image->set(0, $image);
}
$gated_content_prgf->save();
}
}