This repository was archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathContextLayoutManager.php
More file actions
109 lines (101 loc) · 3 KB
/
ContextLayoutManager.php
File metadata and controls
109 lines (101 loc) · 3 KB
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
<?php
namespace Drupal\context_layout\Plugin\ContextLayout;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager;
/**
* Provides an interface for the discovery and instantiation of context layouts.
*/
class ContextLayoutManager extends LayoutPluginManager {
/**
* {@inheritdoc}
*/
public function getLayoutOptions(array $params = []) {
$type = 'full';
$group_by_category = !empty($params['group_by_category']);
$plugins = $this->getDefinitions();
$options = array();
// Sort the plugins first by category, then by label.
foreach ($plugins as $id => $plugin) {
// Only layouts of type 'full' are allowed.
if ($type != $plugin['type']) {
continue;
}
if ($group_by_category) {
$category = isset($plugin['category']) ? (string) $plugin['category'] : 'default';
if (!isset($options[$category])) {
$options[$category] = array();
}
$options[$category][$id] = $plugin['label'];
}
else {
$options[$id] = $plugin['label'];
}
}
return $options;
}
/**
* Returns a Drupal\layout_plugin\Layout instance.
*
* @param string $layout
* Layout ID (machine name).
* @param bool|false $fallback
* Whether to return a fallback layout if default doesn't exist.
*
* @return object
* Drupal\layout_plugin\Layout instance.
*/
public function loadLayout($layout, $fallback = FALSE) {
// We want to return the correct layout if 'default' is passed.
if ('default' == $layout) {
$layout = $this->createInstance($this->getDefaultLayout($fallback));
}
else {
$layout = $this->createInstance($layout);
}
return $layout;
}
/**
* Returns default Drupal\layout_plugin\Layout instance.
*
* @param bool|false $fallback
* Whether to return a fallback layout if default doesn't exist.
*
* @return string
* Layout ID (machine name).
*/
public function getDefaultLayout($fallback = FALSE) {
$layout = \Drupal::config('context_layout.settings')
->get('default_layout');
if ($fallback && !$layout) {
// Get the first available layout.
$layout = array_keys(
$this->getLayoutOptions()
)[0];
}
return $layout;
}
/**
* Return available layout regions.
*
* @param array $regions
* Region ID's.
* @param string $layout_id
* Layout ID (machine name).
*
* @return array
* Available layout region ID's.
*/
public function filterLayoutRegions($regions, $layout_id) {
$layout = \Drupal::service('plugin.manager.context_layout')
->loadLayout($layout_id);
$layout_regions = array_keys($layout->getRegionDefinitions());
foreach ($regions as $region_id => $region) {
if (!in_array($region_id, $layout_regions)) {
unset($regions[$region_id]);
}
}
return $regions;
}
}