Chris@2
|
1 <?php
|
Chris@2
|
2
|
Chris@2
|
3 /**
|
Chris@2
|
4 * @file
|
Chris@2
|
5 * MAYO plugin sub-system.
|
Chris@2
|
6 */
|
Chris@2
|
7
|
Chris@2
|
8 /**
|
Chris@2
|
9 * Find and return all plugins.
|
Chris@2
|
10 *
|
Chris@2
|
11 * This will search all base themes and the active theme for "plugins" in their
|
Chris@2
|
12 * info files, and return all plugins directories.
|
Chris@2
|
13 * MAYO page layouts uses:
|
Chris@2
|
14 * - "plugins[page_layout][layout] = layouts/core"
|
Chris@2
|
15 *
|
Chris@2
|
16 * @param $theme_name, usually the active theme.
|
Chris@2
|
17 */
|
Chris@2
|
18 function mayo_get_plugins($theme_name) {
|
Chris@2
|
19 $plugins = &drupal_static(__FUNCTION__, array());
|
Chris@2
|
20 if (empty($plugins)) {
|
Chris@2
|
21 $plugins_list = array();
|
Chris@2
|
22 $themes_info = mayo_get_info_trail(\Drupal::theme()->getActiveTheme()->getName());
|
Chris@2
|
23 // Look for and get all the plugins
|
Chris@2
|
24 if (!empty($themes_info)) {
|
Chris@2
|
25 foreach ($themes_info as $this_theme => $theme_info) {
|
Chris@2
|
26 foreach ($theme_info as $info) {
|
Chris@2
|
27 if (array_key_exists('plugins', $info)) {
|
Chris@2
|
28 foreach ($info['plugins'] as $plugin_type => $types) {
|
Chris@2
|
29 $plugins_list[$this_theme][$plugin_type] = $types;
|
Chris@2
|
30 }
|
Chris@2
|
31 }
|
Chris@2
|
32 }
|
Chris@2
|
33 }
|
Chris@2
|
34 $plugins_list = ($plugins_list);
|
Chris@2
|
35 $plugins = $plugins_list;
|
Chris@2
|
36 }
|
Chris@2
|
37 }
|
Chris@2
|
38 return $plugins;
|
Chris@2
|
39 }
|
Chris@2
|
40
|
Chris@2
|
41 /**
|
Chris@2
|
42 * Return the paths to all plugin providers plugin directories, this usually
|
Chris@2
|
43 * means themes - both base themes and sub-themes that include plugin directory
|
Chris@2
|
44 * declarations in their info files.
|
Chris@2
|
45 *
|
Chris@2
|
46 * @param $theme_name, ususally the active theme.
|
Chris@2
|
47 */
|
Chris@2
|
48 function mayo_get_plugins_paths($theme_name) {
|
Chris@2
|
49 $provider_paths = array();
|
Chris@2
|
50 $plugins_list = mayo_get_plugins($theme_name);
|
Chris@2
|
51
|
Chris@2
|
52 foreach ($plugins_list as $plugin_provider => $provider) {
|
Chris@2
|
53 foreach ($provider as $plugin_type => $types) {
|
Chris@2
|
54 foreach ($types as $type => $path) {
|
Chris@2
|
55 $provider_path = drupal_get_path('theme', $plugin_provider) . '/' . $path;
|
Chris@2
|
56 $provider_paths[$plugin_provider][$plugin_type][$type] = $provider_path;
|
Chris@2
|
57 }
|
Chris@2
|
58 }
|
Chris@2
|
59 }
|
Chris@2
|
60
|
Chris@2
|
61 return $provider_paths;
|
Chris@2
|
62 }
|
Chris@2
|
63
|
Chris@2
|
64 /**
|
Chris@2
|
65 * Returns all files for plugins of a particular type.
|
Chris@2
|
66 * This is called from mayo_load_plugins(), cannot be cached else it will return
|
Chris@2
|
67 * stale data at some point.
|
Chris@2
|
68 *
|
Chris@2
|
69 * @param $theme_name
|
Chris@2
|
70 */
|
Chris@2
|
71 function mayo_get_plugins_files($theme_name) {
|
Chris@2
|
72 $plugins_files = array();
|
Chris@2
|
73 $plugins_list = mayo_get_plugins($theme_name);
|
Chris@2
|
74
|
Chris@2
|
75 $extension = 'inc';
|
Chris@2
|
76 foreach ($plugins_list as $plugin_provider => $provider) {
|
Chris@2
|
77 foreach ($provider as $plugin_type => $types) {
|
Chris@2
|
78 foreach ($types as $type => $path) {
|
Chris@2
|
79 foreach ($path as $foo => $goo) {
|
Chris@2
|
80 $provider_path = drupal_get_path('theme', $plugin_provider) . '/' . $goo;
|
Chris@2
|
81 $plugins_files[$plugin_provider][$plugin_type][$type] = file_scan_directory($provider_path, '/\.' . $extension . '$/', array('key' => 'name'));
|
Chris@2
|
82 }
|
Chris@2
|
83 }
|
Chris@2
|
84 }
|
Chris@2
|
85 }
|
Chris@2
|
86
|
Chris@2
|
87 return $plugins_files;
|
Chris@2
|
88 }
|
Chris@2
|
89
|
Chris@2
|
90 /**
|
Chris@2
|
91 * Extract plugin data structures.
|
Chris@2
|
92 *
|
Chris@2
|
93 * In essence what this does is return the data strutures (arrays) for all
|
Chris@2
|
94 * plugins of a particular type. MAYO only uses the "page_layout" type.
|
Chris@2
|
95 * This is hard to cache because it takes the
|
Chris@2
|
96 * $plugin_type parameter, so everything else that calls this is heavily cached
|
Chris@2
|
97 * instead. It does support an "everything else" plugin type, whatever that is.
|
Chris@2
|
98 *
|
Chris@2
|
99 * @param $theme_name, usually the active theme.
|
Chris@2
|
100 * @param $plugin_type, the plugin type you need to return, usually one of
|
Chris@2
|
101 * "panels" or "page_layout".
|
Chris@2
|
102 */
|
Chris@2
|
103 function mayo_load_plugins($theme_name, $plugin_type) {
|
Chris@2
|
104 $plugin_data_structures = array();
|
Chris@2
|
105 $plugins_list = mayo_get_plugins_files($theme_name);
|
Chris@2
|
106 $plugins_array = array();
|
Chris@2
|
107 foreach ($plugins_list as $plugin_provider => $plugin_types) {
|
Chris@2
|
108 $plugin_providers[] = $plugin_provider;
|
Chris@2
|
109 foreach ($plugin_types as $type => $plugins) {
|
Chris@2
|
110 if ($type === $plugin_type) {
|
Chris@2
|
111 foreach ($plugins as $ptypes => $plugin) {
|
Chris@2
|
112 $plugins_array[$plugin_provider][$type] = $plugin;
|
Chris@2
|
113 }
|
Chris@2
|
114 }
|
Chris@2
|
115 }
|
Chris@2
|
116 }
|
Chris@2
|
117 $plugin_files = array();
|
Chris@2
|
118 foreach ($plugins_array as $provider => $types) {
|
Chris@2
|
119 foreach ($types as $key => $value) {
|
Chris@2
|
120 $plugin_files = array_merge_recursive($plugin_files, $value);
|
Chris@2
|
121 }
|
Chris@2
|
122 }
|
Chris@2
|
123
|
Chris@2
|
124 foreach ($plugin_files as $file_data) {
|
Chris@2
|
125
|
Chris@2
|
126 include_once(\Drupal::root() . '/' . $file_data->uri);
|
Chris@2
|
127
|
Chris@2
|
128 // page_layout
|
Chris@2
|
129 if ($plugin_type === 'page_layout') {
|
Chris@2
|
130 $identifier = $file_data->name;
|
Chris@2
|
131 $page_layout_function = $identifier;
|
Chris@2
|
132 if (function_exists($page_layout_function)) {
|
Chris@2
|
133 $plugin_data_structures[] = $page_layout_function();
|
Chris@2
|
134 }
|
Chris@2
|
135 }
|
Chris@2
|
136 }
|
Chris@2
|
137
|
Chris@2
|
138 if (empty($plugin_data_structures)) {
|
Chris@2
|
139 return;
|
Chris@2
|
140 }
|
Chris@2
|
141 return $plugin_data_structures;
|
Chris@2
|
142 }
|
Chris@2
|
143
|
Chris@2
|
144 /**
|
Chris@2
|
145 * Return Page layout data structures.
|
Chris@2
|
146 * This returns the full data structures for all page layout plugins. Because
|
Chris@2
|
147 * this can be a lot of data and appears to be computationally expensive to get
|
Chris@2
|
148 * it is cached in the cache table.
|
Chris@2
|
149 *
|
Chris@2
|
150 * @param $theme_name, the active theme.
|
Chris@2
|
151 */
|
Chris@2
|
152 function page_layouts_data_structure($theme_name = NULL) {
|
Chris@2
|
153 // Use the passed in theme_name, else grab it from the global variable
|
Chris@2
|
154 if ($theme_name == NULL) {
|
Chris@2
|
155
|
Chris@2
|
156 $theme_name = \Drupal::theme()->getActiveTheme()->getName();
|
Chris@2
|
157 }
|
Chris@2
|
158
|
Chris@2
|
159 $page_data_structure = &drupal_static(__FUNCTION__, array());
|
Chris@2
|
160 if (empty($page_data_structure)) {
|
Chris@2
|
161 $data_structure = mayo_load_plugins($theme_name, $plugin_type = 'page_layout');
|
Chris@2
|
162 foreach ($data_structure as $plugin => $datum) {
|
Chris@2
|
163 foreach ($datum as $method => $layout) {
|
Chris@2
|
164 $page_data_structure[$method] = $layout;
|
Chris@2
|
165 }
|
Chris@2
|
166 }
|
Chris@2
|
167 }
|
Chris@2
|
168 return $page_data_structure;
|
Chris@2
|
169 }
|
Chris@2
|
170
|
Chris@2
|
171 /**
|
Chris@2
|
172 * Return option arrays for forms.
|
Chris@2
|
173 * Returns the options for radio lists in the page layout settings in the
|
Chris@2
|
174 * appearance theme settings.
|
Chris@2
|
175 *
|
Chris@2
|
176 * @param $theme_name
|
Chris@2
|
177 */
|
Chris@2
|
178 function page_layouts_device_group_options($theme_name) {
|
Chris@2
|
179 $device_group_options = &drupal_static(__FUNCTION__, array());
|
Chris@2
|
180 if (empty($device_group_options)) {
|
Chris@2
|
181 $layout_methods = page_layouts_data_structure($theme_name);
|
Chris@2
|
182 foreach ($layout_methods as $method => $values) {
|
Chris@2
|
183 foreach ($values as $key => $value) {
|
Chris@2
|
184 if ($key == 'device_groups') {
|
Chris@2
|
185 $method_values[$method] = $value;
|
Chris@2
|
186 }
|
Chris@2
|
187 }
|
Chris@2
|
188 }
|
Chris@2
|
189 foreach ($method_values as $this_method => $these_values) {
|
Chris@2
|
190 foreach ($these_values as $k => $dv) {
|
Chris@2
|
191 $device_group_options[$dv][] = $this_method;
|
Chris@2
|
192 }
|
Chris@2
|
193 }
|
Chris@2
|
194 }
|
Chris@2
|
195
|
Chris@2
|
196 return $device_group_options;
|
Chris@2
|
197 }
|
Chris@2
|
198
|
Chris@2
|
199 /**
|
Chris@2
|
200 * Base config for page layout builder.
|
Chris@2
|
201 * This is used in mayo_core.submit.responsive.inc to help retrieve the form
|
Chris@2
|
202 * values for each device groups layout.
|
Chris@2
|
203 */
|
Chris@2
|
204 function assemble_page_layout() {
|
Chris@2
|
205 $variables_array = array(
|
Chris@2
|
206 'layout',
|
Chris@2
|
207 'media_query',
|
Chris@2
|
208 'page_width',
|
Chris@2
|
209 'page_unit',
|
Chris@2
|
210 'sidebar_first',
|
Chris@2
|
211 'sidebar_second',
|
Chris@2
|
212 'sidebar_unit',
|
Chris@2
|
213 );
|
Chris@2
|
214 return $variables_array;
|
Chris@2
|
215 }
|