This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite_reference.module
135 lines (121 loc) · 5.19 KB
/
composite_reference.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
<?php
/**
* @file
* The Composite reference module.
*/
declare(strict_types=1);
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\FieldConfigInterface;
/**
* Implements hook_form_FORM_ID_alter().
*
* Add the composite field option to entity reference fields.
*/
function composite_reference_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state): void {
/** @var \Drupal\field\FieldConfigInterface $field_config */
$field_config = $form_state->getFormObject()->getEntity();
$applicable_field_types = ['entity_reference', 'entity_reference_revisions'];
if (!in_array($field_config->getType(), $applicable_field_types)) {
return;
}
$form['composite_reference'] = [
'#type' => 'details',
'#title' => t('Composite reference'),
'#open' => TRUE,
];
$form['composite_reference']['composite'] = [
'#type' => 'checkbox',
'#title' => t('Composite field'),
'#description' => t('Composite reference fields ensure that referenced entities get deleted when the referencing entity is deleted.'),
'#default_value' => $field_config->getThirdPartySetting('composite_reference', 'composite') ?? FALSE,
];
if ($field_config->getType() === 'entity_reference_revisions') {
$form['composite_reference']['composite_revisions'] = [
'#type' => 'checkbox',
'#title' => t('Include past revisions'),
'#description' => t('If you include past revisions, reference entities will be deleted even if they are no longer referenced but have been in older revisions.'),
'#default_value' => $field_config->getThirdPartySetting('composite_reference', 'composite_revisions') ?? FALSE,
'#states' => [
'visible' => [
':input[name="composite"]' => ['checked' => TRUE],
],
],
];
}
$form['#entity_builders'][] = 'composite_reference_form_field_config_form_builder';
}
/**
* Entity form builder for the field_config form.
*
* Saves the composite field settings into the field config settings.
*
* @param string $entity_type
* The name of the entity type.
* @param \Drupal\field\FieldConfigInterface $field_config
* The field config.
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function composite_reference_form_field_config_form_builder(string $entity_type, FieldConfigInterface $field_config, array &$form, FormStateInterface $form_state): void {
$composite = $form_state->getValue('composite');
$field_config->setThirdPartySetting('composite_reference', 'composite', $composite);
if (!$composite) {
// Ensure we do not allow composite revisions if the composite field is not
// checked.
$field_config->setThirdPartySetting('composite_reference', 'composite_revisions', FALSE);
}
else {
$field_config->setThirdPartySetting('composite_reference', 'composite_revisions', $form_state->getValue('composite_revisions'));
}
}
/**
* Implements hook_entity_predelete().
*
* Delete any composite referenced entities when deleting an entity.
*/
function composite_reference_entity_predelete(EntityInterface $entity): void {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions */
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
$applicable_field_types = ['entity_reference', 'entity_reference_revisions'];
/** @var \Drupal\composite_reference\CompositeReferenceFieldManagerInterface $composite_reference_field_manager */
$composite_reference_field_manager = \Drupal::service('composite_reference.composite_reference_field_manager');
foreach ($field_definitions as $key => $definition) {
if (!in_array($definition->getType(), $applicable_field_types)) {
continue;
}
$composite_reference_field_manager->entityDelete($entity, $definition);
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*
* Whenever a BaseFieldOverride entity is saved, we include the composite
* reference setting into the exported third party settings.
*/
function composite_reference_base_field_override_presave(EntityInterface $entity) {
/** @var \Drupal\Core\Field\Entity\BaseFieldOverride $entity */
$setting = $entity->getItemDefinition()->getSetting('composite_reference');
if (empty($setting)) {
return;
}
if (isset($setting['composite'])) {
$entity->setThirdPartySetting('composite_reference', 'composite', $setting['composite']);
if ($setting['composite'] && isset($setting['composite_revisions'])) {
// Composite revisions can be used only for entity_reference_revisions
// field types.
$composite_revisions = $entity->get('field_type') === 'entity_reference_revisions' ? $setting['composite_revisions'] : FALSE;
$entity->setThirdPartySetting('composite_reference', 'composite_revisions', $composite_revisions);
}
elseif ($setting['composite'] === FALSE) {
// Composite revisions setting cannot be set without composite being TRUE.
$entity->setThirdPartySetting('composite_reference', 'composite_revisions', FALSE);
}
}
}