forked from drupal-media/entity_browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_browser.module
83 lines (76 loc) · 2.74 KB
/
entity_browser.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
<?php
/**
* @file
* Allows to flexibly create, browse and select entities.
*/
use \Drupal\Core\Form\FormStateInterface;
use \Drupal\Core\Render\Element;
use Drupal\Core\Url;
/**
* Implements hook_theme().
*
* Overrides the core html theme to use a custom template for iframes.
*/
function entity_browser_theme() {
return [
'html__entity_browser__iframe' => [
'template' => 'html--entity-browser--iframe',
'render element' => 'html',
'preprocess functions' => ['template_preprocess_html']
],
'html__entity_browser__modal' => [
'template' => 'html--entity-browser--iframe',
'render element' => 'html',
'preprocess functions' => ['template_preprocess_html']
],
'page__entity_browser__iframe' => [
'template' => 'page--entity-browser--iframe',
'render element' => 'html',
'preprocess functions' => ['template_preprocess_page']
],
'page__entity_browser__modal' => [
'template' => 'page--entity-browser--iframe',
'render element' => 'html',
'preprocess functions' => ['template_preprocess_page']
],
];
}
/**
* Implements hook_form_alter().
*/
function entity_browser_form_alter(&$form, FormStateInterface &$form_state) {
$entity_browser_dialog_edit = \Drupal::service('request_stack')->getCurrentRequest()->get('_route');
if ($entity_browser_dialog_edit == 'entity_browser.edit_form') {
// Let's allow the save button only.
foreach (Element::children($form['actions']) as $key) {
$form['actions'][$key]['#access'] = $key == 'submit';
}
// Use Ajax.
$form['actions']['submit']['#ajax'] = [
'url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $form_state->getFormObject()->getEntity()->getEntityTypeId(), 'entity' => $form_state->getFormObject()->getEntity()->id()])
];
}
}
/**
* Implements hook_preprocess_page__entity_browser__iframe().
*
* Tries to figure out where messages block lives and display it separately.
*/
function entity_browser_preprocess_page__entity_browser__iframe(&$variables) {
$variables['messages'] = '';
$blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties([
'theme' => \Drupal::theme()->getActiveTheme()->getName(),
'plugin' => 'system_messages_block',
]);
if (($messages = current($blocks)) && !empty($variables['page'][$messages->getRegion()][$messages->id()])) {
$variables['messages'] = $variables['page'][$messages->getRegion()][$messages->id()];
}
}
/**
* Implements hook_preprocess_page__entity_browser__modal().
*
* Tries to figure out where messages block lives and display it separately.
*/
function entity_browser_preprocess_page__entity_browser__modal(&$variables) {
entity_browser_preprocess_page__entity_browser__iframe($variables);
}