-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwmmeta.install
More file actions
122 lines (102 loc) · 3.79 KB
/
Copy pathwmmeta.install
File metadata and controls
122 lines (102 loc) · 3.79 KB
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
<?php
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\image\Entity\ImageStyle;
function wmmeta_uninstall(): void
{
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::getContainer()->get('entity_field.manager');
$fieldConfigStorage = $entityTypeManager->getStorage('field_config');
$fieldStorageConfigStorage = $entityTypeManager->getStorage('field_storage_config');
$metaStorage = $entityTypeManager->getStorage('meta');
$eckStorage = $entityTypeManager->getStorage('eck_entity_type');
/** @var EntityTypeInterface[] $entityTypes */
$entityTypes = array_filter(
$entityTypeManager->getDefinitions(),
function (EntityTypeInterface $entityType) use ($entityFieldManager) {
return $entityType->entityClassImplements(FieldableEntityInterface::class)
&& $entityType->hasKey('published')
&& isset($entityFieldManager->getFieldStorageDefinitions($entityType->id())['field_meta']);
}
);
foreach ($entityTypes as $entityType) {
// Delete fields
$ids = $fieldConfigStorage->getQuery()
->condition('field_name', 'field_meta')
->condition('entity_type', $entityType->id())
->accessCheck(false)
->execute();
if (empty($ids)) {
continue;
}
$fieldConfigs = $fieldConfigStorage->loadMultiple($ids);
$fieldConfigStorage->delete($fieldConfigs);
}
// Delete bundle
$ids = $metaStorage->getQuery()
->condition('type', 'meta')
->accessCheck(false)
->execute();
if (!empty($ids)) {
$metas = $metaStorage->loadMultiple($ids);
$metaStorage->delete($metas);
}
// Delete entity type
$eckStorage
->load('meta')
->delete();
// Clear cached definitions
$entityTypeManager->clearCachedDefinitions();
}
/**
* Install the OG image style
*/
function wmmeta_update_8001(): void
{
// Install the new image module dependency if not yet installed
\Drupal::getContainer()
->get('module_installer')
->install(['image']);
$storage = new FileStorage(__DIR__ . '/config/install');
$data = $storage->read('image.style.og');
if (!ImageStyle::load('og')) {
ImageStyle::create($data)->save();
}
}
/**
* Update the meta view & form display
*/
function wmmeta_update_8002(): void
{
$formDisplay = EntityFormDisplay::load('media.image.default');
if ($formDisplay instanceof EntityFormDisplay && $component = $formDisplay->getComponent('field_meta_image')) {
$component['settings']['image_style'] = 'og';
$formDisplay->setComponent('field_meta_image', $component);
$formDisplay->save();
}
$viewDisplay = EntityViewDisplay::load('meta.meta.default');
if ($viewDisplay instanceof EntityViewDisplay && $component = $viewDisplay->getComponent('field_meta_image')) {
$component['type'] = 'wmmedia_media_image_default';
$component['settings'] = [
'image_style' => 'og',
];
$viewDisplay->setComponent('field_meta_image', $component);
$viewDisplay->save();
}
}
/**
* Allow all roles to access wmmeta preview except anonymous and authenticated
*/
function wmmeta_update_8003(): void
{
$storage = \Drupal::entityTypeManager()->getStorage('user_role');
$ids = $storage->getQuery()->condition('id', ['anonymous', 'authenticated'], 'NOT IN')->accessCheck(false)->execute();
$roles = $storage->loadMultiple($ids);
foreach ($roles as $role) {
$role->grantPermission('access wmmeta preview');
$role->save();
}
}