-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos2web_events.install
125 lines (98 loc) · 4.65 KB
/
os2web_events.install
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
<?php
/**
* @file
* Install, uninstall and update hooks for page builder module.
*/
use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldStorageConfig;
use Symfony\Component\Yaml\Yaml;
use Drupal\field\Entity\FieldConfig;
function os2web_events_read_in_new_config($config_name) {
$path = drupal_get_path('module', 'os2web_events');
/** @var \Drupal\Core\Config\StorageInterface $active_storage */
$active_storage = \Drupal::service('config.storage');
$active_storage->write($config_name, Yaml::parse(file_get_contents($path . '/config/install/' . $config_name . '.yml')));
}
/**
* Creating paragraph os2web_events.
*/
function os2web_events_update_8001() {
$path = drupal_get_path('module', 'os2web_events');
/** @var \Drupal\Core\Config\StorageInterface $active_storage */
$active_storage = \Drupal::service('config.storage');
// Creating view.
$active_storage->write('views.view.os2web_events', Yaml::parse(file_get_contents($path . '/config/install/views.view.os2web_events.yml')));
// Updating view.
$active_storage->write('views.view.os2web_events_search', Yaml::parse(file_get_contents($path . '/config/install/views.view.os2web_events_search.yml')));
// Creating paragraph.
$active_storage->write('paragraphs.paragraphs_type.os2web_events', Yaml::parse(file_get_contents($path . '/config/install/paragraphs.paragraphs_type.os2web_events.yml')));
// Create field storage and field storage.
$module_handler = \Drupal::moduleHandler();
$config_storage = new FileStorage($module_handler->getModule('os2web_events')->getPath() . '/config/install');
foreach (['field.storage.paragraph.field_os2web_events_par_category', 'field.field.paragraph.os2web_events.field_os2web_events_par_category'] as $config_name) {
$config_record = $config_storage->read($config_name);
$entity_type = \Drupal::service('config.manager')->getEntityTypeIdByName($config_name);
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$entity = $storage->createFromStorageRecord($config_record);
$entity->save();
}
// Updating paragraph view and form displays.
$active_storage->write('core.entity_form_display.paragraph.os2web_events.default', Yaml::parse(file_get_contents($path . '/config/install/core.entity_form_display.paragraph.os2web_events.default.yml')));
$active_storage->write('core.entity_view_display.paragraph.os2web_events.default', Yaml::parse(file_get_contents($path . '/config/install/core.entity_view_display.paragraph.os2web_events.default.yml')));
// Updating node view display.
$active_storage->write('core.entity_view_display.node.os2web_event.teaser', Yaml::parse(file_get_contents($path . '/config/install/core.entity_view_display.node.os2web_event.teaser.yml')));
}
/**
* Allowing to list events by multiple categories.
*/
function os2web_events_update_8002() {
// Creating view.
os2web_events_read_in_new_config('views.view.os2web_events');
// Updating field storage.
$storegeConfig = FieldStorageConfig::loadByName('paragraph', 'field_os2web_events_par_category');
$storegeConfig->setCardinality(-1);
$storegeConfig->save();
}
/**
* Add time to event start and end date
*
*/
function os2web_events_update_9500() {
date_default_timezone_set('UTC');
// get the data
$nids = \Drupal::entityQuery('node')
->condition('type', 'os2web_event')
->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
// change the database
$start_field = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->load('node.field_os2web_event_start_date');
$start_field->setSetting('datetime_type', 'datetime');
$start_field->save();
$end_field = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->load('node.field_os2web_event_end_date');
$end_field->setSetting('datetime_type', 'datetime');
$end_field->save();
// put the data back
foreach ($nodes as $node) {
if ($node->get('field_os2web_event_start_date')->value) {
$raw = $node->get('field_os2web_event_start_date')->value;
$stamp = strtotime($raw);
if (strlen($raw) == 10) $stamp = strtotime("-2 hour", $stamp);
$date = date('Y-m-d\TH:i:s', $stamp);
$node->set('field_os2web_event_start_date', $date);
$node->save();
}
if ($node->get('field_os2web_event_end_date')->value) {
$raw = $node->get('field_os2web_event_end_date')->value;
$stamp = strtotime($raw);
if (strlen($raw) == 10) $stamp = strtotime("-2 hour", $stamp);
$date = date('Y-m-d\TH:i:s', $stamp);
$node->set('field_os2web_event_end_date', $date);
$node->save();
}
}
}