Completed
Push — 8.x-1.x ( 70f2d6...17b03a )
by Janez
02:51
created

entity_browser.module::entity_browser_theme()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 18
c 3
b 0
f 1
nc 1
nop 0
dl 0
loc 24
rs 8.9713
1
<?php
2
3
/**
4
 * @file
5
 * Allows to flexibly create, browse and select entities.
6
 */
7
8
use \Drupal\Core\Form\FormStateInterface;
9
use \Drupal\Core\Render\Element;
10
use Drupal\Core\Url;
11
12
/**
13
 * Implements hook_theme().
14
 *
15
 * Overrides the core html theme to use a custom template for iframes.
16
 */
17
function entity_browser_theme() {
18
  return [
19
    'html__entity_browser__iframe' => [
20
      'template' => 'html--entity-browser--iframe',
21
      'render element' => 'html',
22
      'preprocess functions' => ['template_preprocess_html']
23
    ],
24
    'html__entity_browser__modal' => [
25
      'template' => 'html--entity-browser--iframe',
26
      'render element' => 'html',
27
      'preprocess functions' => ['template_preprocess_html']
28
    ],
29
    'page__entity_browser__iframe' => [
30
      'template' => 'page--entity-browser--iframe',
31
      'render element' => 'html',
32
      'preprocess functions' => ['template_preprocess_page']
33
    ],
34
    'page__entity_browser__modal' => [
35
      'template' => 'page--entity-browser--iframe',
36
      'render element' => 'html',
37
      'preprocess functions' => ['template_preprocess_page']
38
    ],
39
  ];
40
}
41
42
/**
43
 * Implements hook_form_alter().
44
 */
45
function entity_browser_form_alter(&$form, FormStateInterface &$form_state) {
46
  $entity_browser_dialog_edit = \Drupal::service('request_stack')->getCurrentRequest()->get('_route');
47
  if ($entity_browser_dialog_edit == 'entity_browser.edit_form') {
48
    // Let's allow the save button only.
49
    foreach (Element::children($form['actions']) as $key) {
50
      $form['actions'][$key]['#access'] = $key == 'submit';
51
    }
52
    // Use Ajax.
53
    $form['actions']['submit']['#ajax'] = [
54
      'url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $form_state->getFormObject()->getEntity()->getEntityTypeId(), 'entity' => $form_state->getFormObject()->getEntity()->id()])
55
    ];
56
  }
57
}
58
59
/**
60
 * Implements hook_preprocess_page__entity_browser__iframe().
61
 *
62
 * Tries to figure out where messages block lives and display it separately.
63
 */
64
function entity_browser_preprocess_page__entity_browser__iframe(&$variables) {
65
  $variables['messages'] = '';
66
  $blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties([
67
    'theme' => \Drupal::theme()->getActiveTheme()->getName(),
68
    'plugin' => 'system_messages_block',
69
  ]);
70
71
  if (($messages = current($blocks)) && !empty($variables['page'][$messages->getRegion()][$messages->id()])) {
72
    $variables['messages'] = $variables['page'][$messages->getRegion()][$messages->id()];
73
  }
74
}
75
76
/**
77
 * Implements hook_preprocess_page__entity_browser__modal().
78
 *
79
 * Tries to figure out where messages block lives and display it separately.
80
 */
81
function entity_browser_preprocess_page__entity_browser__modal(&$variables) {
82
  entity_browser_preprocess_page__entity_browser__iframe($variables);
83
}
84