-
Notifications
You must be signed in to change notification settings - Fork 0
/
brown_d8react_integration.install
102 lines (85 loc) · 3.91 KB
/
brown_d8react_integration.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
<?php
/**
* @file
* Installation functions for Brown D8 React Integration module.
* @author James Morton <[email protected]>
*/
use \Drupal\Core\Config\FileStorage;
use Symfony\Component\Yaml\Yaml;
/**
* Installation Function
* Implements hook_install().
*
* Add react app paragraph type to basic page content components
*/
function brown_d8react_config_import_steps_alter(&$sync_steps, \Drupal\Core\Config\ConfigImporter $config_importer) {
$sync_steps[] = 'brown_d8react_additional_configuration_step';
}
function brown_d8react_additional_configuration_step(&$context) {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('field.field.node.basic_page.field_content_components');
$dependencies = $config->getOriginal('dependencies.config');
$bundles = $config->getOriginal('settings.handler_settings.target_bundles');
$drag_drop = $config->getOriginal('settings.handler_settings.target_bundles_drag_drop');
$dependencies[] = 'paragraphs.paragraphs_type.react_app';
$config->set('dependencies.config', $dependencies)->save();
$bundles['react_app'] = 'react_app';
$config->set('settings.handler_settings.target_bundles', $bundles)->save();
$drag_drop['react_app'] = array(
'enabled' => true,
'weight' => 192
);
$config->set('settings.handler_settings.target_bundles_drag_drop', $drag_drop)->save();
$context['finished'] = 1;
}
/**
* Uninstallation Function
* Implements hook_uninstall().
*
* Remove react app paragraph type to basic page content components
*/
function brown_d8react_integration_uninstall() {
$config_factory = \Drupal::configFactory();
$config_factory->getEditable('core.entity_form_display.paragraph.react_app.default')->delete();
$config_factory->getEditable('core.entity_view_display.paragraph.react_app.default')->delete();
$config_factory->getEditable('field.field.paragraph.react_app.field_react_apps')->delete();
$config_factory->getEditable('field.storage.paragraph.field_react_apps')->delete();
$config_factory->getEditable('paragraphs.paragraphs_type.react_app')->delete();
$config = $config_factory->getEditable('field.field.node.basic_page.field_content_components');
$dependencies = $config->getOriginal('dependencies.config');
$bundles = $config->getOriginal('settings.handler_settings.target_bundles');
$drag_drop = $config->getOriginal('settings.handler_settings.target_bundles_drag_drop');
$index = array_search('paragraphs.paragraphs_type.react_app', $dependencies);
unset($dependencies[$index]);
$config->set('dependencies.config', $dependencies)->save();
unset($bundles['react_app']);
$config->set('settings.handler_settings.target_bundles', $bundles)->save();
unset($drag_drop['react_app']);
$config->set('settings.handler_settings.target_bundles_drag_drop', $drag_drop)->save();
removeReactAppNodes();
}
/**
* Delete all node that contain React Apps
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function removeReactAppNodes() {
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple();
foreach ($nodes as $node) {
foreach ($node->getFields() as $key => $field) {
$field_type = $field->getFieldDefinition()->getType();
$field_settings = $field->getSettings();
if ($field_type == 'entity_reference_revisions' && $field_settings['target_type'] === 'paragraph') {
foreach ($field as $item) {
if ($item->entity instanceof \Drupal\paragraphs\ParagraphInterface) {
if ($item->entity->getType() === 'react_app') {
$node->delete();
}
}
}
}
}
}
}