-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrapesjs_editor.module
71 lines (65 loc) · 2.34 KB
/
grapesjs_editor.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
<?php
/**
* @file
* Contains grapesjs_editor.module.
*/
use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_help().
*/
function grapesjs_editor_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.grapesjs_editor':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The GrapesJS Editor module provides a page builder.') . '</p>';
return ['#markup' => $output];
}
}
/**
* Implements hook_element_info_alter().
*/
function grapesjs_editor_element_info_alter(array &$info) {
if (!empty($info['text_format'])) {
$info['text_format']['#process'][] = '_grapesjs_editor_text_format_processor';
}
}
/**
* Removes GrapesJS format if is disabled for this node type.
*
* @param array $element
* The render element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param array $complete_form
* The form element.
*
* @return array
* The altered render element.
*/
function _grapesjs_editor_text_format_processor(array $element, FormStateInterface $form_state, array &$complete_form) {
if (!empty($element['format']) && isset($element['format']['format']['#options']['grapesjs_editor'])) {
// By default let's assume that GrapesJS Editor format is not allowed.
$grapesjs_editor_allowed = FALSE;
/** @var \Drupal\Core\Entity\ContentEntityForm $form */
$form = $form_state->getFormObject();
// Check whether the form that contains the element is an EntityForm.
if ($form instanceof EntityFormInterface) {
// Get the entity from the form object for further processing.
$entity = $form->getEntity();
// Check whether entity is of node type, because currently only them are
// supported.
$grapesjs_editor_allowed = $entity instanceof NodeInterface || $entity instanceof BlockContentInterface;
}
// If GrapesJS Editor is not enabled for the current form or
// current user is not allowed to use the format, disable the choice of
// GrapesJS Editor format for this element.
if (!$grapesjs_editor_allowed) {
unset($element['format']['format']['#options']['grapesjs_editor']);
}
}
return $element;
}