|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\paragraphs_editor\Utility; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Field\FieldItemListInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Helper methods for rendering paragraphs editor paragraphs. |
|
9
|
|
|
* |
|
10
|
|
|
* In most cases you shouldn't need to use these methods as the render array |
|
11
|
|
|
* will be set up with the correct attributes and properties during |
|
12
|
|
|
* preprocessing. |
|
13
|
|
|
* |
|
14
|
|
|
* Using this class may become necessary if you decide not to use Drupal's |
|
15
|
|
|
* default field rendering and instead render data directly from the paragraph |
|
16
|
|
|
* entity. You may also use it to access any data that was created during widget |
|
17
|
|
|
* data compilation. |
|
18
|
|
|
*/ |
|
19
|
|
|
class RenderUtility { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Determines whether a template is being rendered for editor consumption. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $variables |
|
25
|
|
|
* An array of variables passed to a preprocess function. |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
* TRUE if the template is being rendered within the editor, FALSE |
|
29
|
|
|
* otherwise. |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function inEditor(array $variables) { |
|
32
|
|
|
return !!static::getCompilerState($variables); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Gets the editor data compiler state object in a preprocess function. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $variables |
|
39
|
|
|
* An array of variables passed to a preprocess function. |
|
40
|
|
|
* |
|
41
|
|
|
* @return \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState |
|
42
|
|
|
* The state object containing all the editor metadata associated with the |
|
43
|
|
|
* template. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getCompilerState(array $variables) { |
|
46
|
|
|
if (!empty($variables['element']['#paragraphs_editor_state'])) { |
|
47
|
|
|
return $variables['element']['#paragraphs_editor_state']; |
|
48
|
|
|
} |
|
49
|
|
|
if (!empty($variables['elements']['#paragraphs_editor_state'])) { |
|
50
|
|
|
return $variables['elements']['#paragraphs_editor_state']; |
|
51
|
|
|
} |
|
52
|
|
|
else { |
|
53
|
|
|
return NULL; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Preprocesses field items to attach inline editing information. |
|
59
|
|
|
* |
|
60
|
|
|
* This should be called from hook_preprocess_field(). |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $variables |
|
63
|
|
|
* An array of variables passed to a preprocess function. |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function preprocessField(array &$variables) { |
|
66
|
|
|
$editable = static::getEditableData($variables, $variables['element']['#items']); |
|
67
|
|
|
if ($editable && !empty($variables['items'])) { |
|
68
|
|
|
foreach ($variables['items'] as $delta => $value) { |
|
69
|
|
|
if (!$delta) { |
|
70
|
|
|
$editable->preprocessField($variables['items'][$delta]); |
|
71
|
|
|
} |
|
72
|
|
|
else { |
|
73
|
|
|
unset($variables['items'][$delta]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Gets the inline editing information for a template. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $variables |
|
83
|
|
|
* An array of variables passed to a preprocess function. |
|
84
|
|
|
* @param \Drupal\Core\Field\FieldItemListInterface|null $items |
|
85
|
|
|
* An optional field object to get the information for. If this is not |
|
86
|
|
|
* passed the field items will be extracted from the render array. Passing |
|
87
|
|
|
* NULL is only supported when this is called from hook_preprocess_field(). |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Drupal\paragraphs_editor\WidgetBinder\EditableField |
|
90
|
|
|
* An object containing the inline editing information, or NULL if no such |
|
91
|
|
|
* information was compiled for this template. |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function getEditableData(array $variables, FieldItemListInterface $items = NULL) { |
|
94
|
|
|
if (!$items && isset($variables['element']['#items'])) { |
|
95
|
|
|
$items = $variables['element']['#items']; |
|
96
|
|
|
} |
|
97
|
|
|
$state = static::getCompilerState($variables); |
|
98
|
|
|
if ($state) { |
|
99
|
|
|
return $state->getGenerator('editable')->getEditable($state, $items); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|