-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathui_patterns_layouts.module
135 lines (121 loc) · 6.58 KB
/
ui_patterns_layouts.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* @file
* Contains module file.
*/
use Drupal\ui_patterns\Element\PatternContext;
use Drupal\Core\Layout\LayoutDefinition;
use Drupal\ui_patterns\UiPatterns;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
/**
* Implements hook_layout_alter().
*/
function ui_patterns_layouts_layout_alter(&$definitions) {
/** @var \Drupal\ui_patterns\Definition\PatternDefinition[] $pattern_definitions */
// @todo: Use layout deriver instead.
// @link https://github.com/nuvoleweb/ui_patterns/issues/94
foreach (UiPatterns::getPatternDefinitions() as $pattern_definition) {
$id = 'pattern_' . $pattern_definition->id();
$definition = [
'id' => $id,
'label' => $pattern_definition->getLabel(),
'theme' => $pattern_definition->getThemeHook(),
'provider' => $pattern_definition->getProvider(),
'category' => 'Patterns',
'class' => '\Drupal\ui_patterns_layouts\Plugin\Layout\PatternLayout',
'pattern' => $pattern_definition->id(),
'template' => 'pattern-' . $pattern_definition->id(),
];
foreach ($pattern_definition->getFields() as $field) {
$definition['regions'][$field->getName()]['label'] = $field->getLabel();
}
$definitions[$id] = new LayoutDefinition($definition);
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function ui_patterns_layouts_preprocess_ds_entity_view(&$variables) {
if (isset($variables['content']['#type']) && $variables['content']['#type'] == 'pattern') {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $variables['content']['#entity'];
// Allow default context values to not override those exposed elsewhere.
$variables['content']['#context']['type'] = 'layout';
$variables['content']['#context']['entity_type'] = $variables['content']['#entity_type'];
$variables['content']['#context']['bundle'] = $variables['content']['#bundle'];
$variables['content']['#context']['view_mode'] = $variables['content']['#view_mode'];
$variables['content']['#context']['entity_id'] = $entity->id();
$variables['content']['#context']['entity'] = $entity;
}
}
/**
* Implements hook_entity_view_alter().
*/
function ui_patterns_layouts_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($display instanceof EntityDisplayWithLayoutInterface && isset($build['_field_layout']['#type']) && $build['_field_layout']['#type'] == 'pattern') {
$build['_field_layout']['#context']['type'] = 'layout';
$build['_field_layout']['#context']['entity_type'] = $build['#entity_type'];
$build['_field_layout']['#context']['bundle'] = $entity->bundle();
$build['_field_layout']['#context']['view_mode'] = $build['#view_mode'];
$build['_field_layout']['#context']['entity_id'] = $entity->id();
$build['_field_layout']['#context']['entity'] = $entity;
}
}
/**
* Implements hook_ui_patterns_suggestions_alter().
*/
function ui_patterns_layouts_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('layout')) {
$hook = $variables['theme_hook_original'];
$variant = isset($variables["variant"]) ? $variables["variant"] : '';
$entity_type = $context->getProperty('entity_type');
$bundle = $context->getProperty('bundle');
$view_mode = $context->getProperty('view_mode');
$entity_id = $context->getProperty('entity_id');
$suggestions[] = $hook . '__layout';
$suggestions[] = $hook . '__layout__' . $entity_type;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $bundle;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $view_mode;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $bundle . '__' . $view_mode;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $entity_id;
if (!empty($variant)) {
$suggestions[] = $hook . '__variant_' . $variant . '__layout';
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $bundle;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $view_mode;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $bundle . '__' . $view_mode;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $entity_id;
}
}
}
/**
* Implements hook_ui_patterns_destination_suggestions_alter().
*/
function ui_patterns_layouts_ui_patterns_destination_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
if ($context->isOfType('layout')) {
$hook = $variables['theme_hook_original'];
$variant = isset($variables["variant"]) ? $variables["variant"] : '';
$entity_type = $context->getProperty('entity_type');
$bundle = $context->getProperty('bundle');
$view_mode = $context->getProperty('view_mode');
$entity_id = $context->getProperty('entity_id');
$pattern = $context->getProperty('pattern');
$field = $context->getProperty('field');
$suggestions[] = $hook . '__layout__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $bundle . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $view_mode . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $bundle . '__' . $view_mode . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__layout__' . $entity_type . '__' . $entity_id . '__' . $pattern . '__' . $field;
if (!empty($variant)) {
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $bundle . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $view_mode . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $bundle . '__' . $view_mode . '__' . $pattern . '__' . $field;
$suggestions[] = $hook . '__variant_' . $variant . '__layout__' . $entity_type . '__' . $entity_id . '__' . $pattern . '__' . $field;
}
}
}