-
Notifications
You must be signed in to change notification settings - Fork 5
/
oe_paragraphs.module
136 lines (118 loc) · 4.62 KB
/
oe_paragraphs.module
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
<?php
/**
* @file
* OE Paragraphs module.
*/
declare(strict_types=1);
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\oe_paragraphs\Event\FlagOptionsEvent;
use Drupal\oe_paragraphs\Event\IconOptionsEvent;
/**
* Implements hook_entity_base_field_info().
*/
function oe_paragraphs_entity_base_field_info(EntityTypeInterface $entity_type): array {
$fields = [];
if ($entity_type->id() !== 'paragraph') {
return $fields;
}
$fields['oe_paragraphs_variant'] = BaseFieldDefinition::create('string')
->setLabel(t('Variant'))
->setRequired(FALSE)
->setTranslatable(FALSE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
return $fields;
}
/**
* Sets dynamic allowed values for the icon field.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* The field definition.
* @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
* The entity being created if applicable.
* @param bool $cacheable
* Boolean indicating if the results are cacheable.
*
* @return array
* An array of possible key and value icon options.
*
* @see options_allowed_values()
*/
function _oe_paragraphs_allowed_values_icons(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
$event = new IconOptionsEvent();
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, IconOptionsEvent::class);
return $event->getIconOptions();
}
/**
* Sets allowed values for the "Flag" field.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* The field definition.
* @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
* The entity being created if applicable.
* @param bool $cacheable
* Boolean indicating if the results are cacheable.
*
* @return array
* An array of possible key and value flag options.
*
* @see options_allowed_values()
*/
function _oe_paragraphs_allowed_values_flags(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL, &$cacheable = TRUE): array {
$event = new FlagOptionsEvent();
\Drupal::service('event_dispatcher')->dispatch($event, FlagOptionsEvent::class);
return $event->getFlagOptions();
}
/**
* Loads a config array from storage, determines the entity type and imports it.
*
* @param string $name
* The config name.
* @param \Drupal\Core\Config\StorageInterface $storage
* The configuration storage where the file is located.
* @param bool $create_if_missing
* If the configuration entity should be created if not found. Defaults to
* TRUE.
* @param bool $update_if_exists
* If the configuration entity should be updated if found. Defaults to TRUE.
*/
function _oe_paragraphs_import_config_from_file(string $name, StorageInterface $storage, bool $create_if_missing = TRUE, bool $update_if_exists = TRUE): void {
$config_manager = \Drupal::service('config.manager');
$entity_type_manager = \Drupal::entityTypeManager();
$config = $storage->read($name);
if (!$config) {
throw new \LogicException(sprintf('The configuration value named %s was not found in the storage.', $name));
}
$entity_type = $config_manager->getEntityTypeIdByName($name);
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
$entity_storage = $entity_type_manager->getStorage($entity_type);
$id_key = $entity_storage->getEntityType()->getKey('id');
$entity = $entity_storage->load($config[$id_key]);
if (!$entity instanceof ConfigEntityInterface) {
if (!$create_if_missing) {
throw new \LogicException(sprintf('The configuration entity "%s" was not found.', $config[$id_key]));
}
// When we create a new config, it usually means that we are also shipping
// it in the config/install folder so we want to make sure it gets the hash
// so Drupal treats it as a shipped config. This means that it gets exposed
// to be translated via the locale system as well.
$config['_core']['default_config_hash'] = Crypt::hashBase64(serialize($config));
$entity = $entity_storage->createFromStorageRecord($config);
$entity->save();
return;
}
if (!$update_if_exists) {
return;
}
$entity = $entity_storage->updateFromStorageRecord($entity, $config);
$entity->save();
}