forked from drupal-media/entity_browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_browser.module
80 lines (76 loc) · 2.87 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
<?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 ($form['#form_id'] == 'views_exposed_form' && $form_state->getStorage()['display']['display_plugin'] == 'entity_browser') {
// Exposed form is not a "real" form when used in context of entitity browser.
// Let's just display form elements.
$dummy_form = array();
/** @var \Drupal\views\ViewExecutable $view */
$view = $form_state->get('view');
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
foreach ($view->filter as $filter) {
if ($filter->isExposed()) {
$exposed = $filter->exposedInfo();
$dummy_key = 'entity_browser_exposed_' . $exposed['value'];
$dummy_form[$dummy_key] = $form[$exposed['value']];
$dummy_form['#info']['filter-' . $dummy_key] = $form['#info']['filter-' . $filter->field];
$dummy_form['#info']['filter-' . $dummy_key]['value'] = $dummy_key;
}
}
$dummy_form['#id'] = '';
$dummy_form['#build_id'] = '';
$dummy_form['#array_parents'] = [];
$dummy_form['#tree'] = '';
$dummy_form['#theme'] = $form['#theme'];
$form = $dummy_form;
}
else 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()])
];
}
}