-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoe_oembed.module
134 lines (120 loc) · 4.78 KB
/
oe_oembed.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
<?php
/**
* @file
* OpenEuropa module.
*/
declare(strict_types=1);
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_ckeditor_css_alter().
*/
function oe_oembed_ckeditor_css_alter(&$css, $editor) {
$css[] = \Drupal::service('extension.list.module')->getPath('oe_oembed') . '/css/ckeditor_embed.css';
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters the entity view display edit form for entities to add an element
* that allows to select which view modes can be used for embedding in the
* WYSIWYG.
*/
function oe_oembed_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $entity */
$entity = $form_state->getFormObject()->getEntity();
$entity_type = \Drupal::entityTypeManager()->getDefinition($entity->getTargetEntityTypeId());
// We only care about content entities.
if (!$entity_type instanceof ContentEntityTypeInterface) {
return;
}
// We only add to the Custom Display Settings, if it exists.
if (!isset($form['modes'])) {
return;
}
// Gather which view displays are currently available.
$options = [];
$default_value = [];
foreach (_oe_oembed_get_enabled_display_options($entity) as $id => $details) {
$options[$id] = $details['label'];
$default_value[$id] = $details['embeddable'] ? $id : 0;
}
$form['modes']['embeddable_displays'] = [
'#type' => 'checkboxes',
'#title' => t('Embeddable view displays'),
'#description' => t('Select which of the view modes should be available when embedding an entity.'),
'#options' => $options,
'#default_value' => $default_value,
];
$form['actions']['submit']['#submit'][] = '_oe_oembed_save_embeddable_displays';
}
/**
* Custom submit method for entity view display edit forms.
*
* We store whether an entity view display should be embeddable or not.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function _oe_oembed_save_embeddable_displays(array &$form, FormStateInterface $form_state): void {
if (!$form_state->getValue('embeddable_displays')) {
return;
}
$display = $form_state->getFormObject()->getEntity();
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()->getStorage($display->getEntityTypeId());
foreach ($form_state->getValue('embeddable_displays') as $mode => $value) {
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
$view_display = $storage->load($display->getTargetEntityTypeId() . '.' . $display->getTargetBundle() . '.' . $mode);
// Only save if the value has changed.
if ($view_display->getThirdPartySetting('oe_oembed', 'embeddable') !== $value) {
$view_display->setThirdPartySetting('oe_oembed', 'embeddable', $value);
$view_display->save();
}
}
}
/**
* Returns the available entity view displays and if they are embeddable or not.
*
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display being edited.
*
* @return array
* A list of enabled view displays.
*/
function _oe_oembed_get_enabled_display_options(EntityViewDisplayInterface $display): array {
$available_options = [];
// Retrieve all available displays for the target entity type.
$available_display_modes = \Drupal::service('entity_display.repository')->getViewModeOptions($display->getTargetEntityTypeId());
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()->getStorage($display->getEntityTypeId());
foreach ($available_display_modes as $mode => $label) {
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
$view_display = $storage->load($display->getTargetEntityTypeId() . '.' . $display->getTargetBundle() . '.' . $mode);
if ($view_display && $view_display->status()) {
$available_options[$mode] = [
'label' => $label,
'embeddable' => $view_display->getThirdPartySetting('oe_oembed', 'embeddable'),
];
}
}
return $available_options;
}
/**
* Implements hook_ckeditor4to5upgrade_plugin_info_alter().
*/
function oe_oembed_ckeditor4to5upgrade_plugin_info_alter(array &$plugin_definitions): void {
// Alter the \Drupal\oe_oembed\Plugin\CKEditor4To5Upgrade\OeOembed definition
// to add the existing buttons.
$embed_buttons = \Drupal::entityTypeManager()
->getStorage('embed_button')
->loadByProperties([
'type_id' => 'oe_oembed_entities',
]);
$buttons = array_map(fn($button) => $button->id(), $embed_buttons);
if ($buttons) {
$plugin_definitions['oe_oembed']['cke4_buttons'] = $buttons;
}
}