Projet

Général

Profil

Paste
Télécharger (6,78 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / mayo / inc / plugins.inc @ d7f58da2

1
<?php
2

    
3
/**
4
 * @file
5
 * MAYO plugin sub-system.
6
 */
7

    
8
/**
9
 * Find and return all plugins.
10
 *
11
 * This will search all base themes and the active theme for "plugins" in their
12
 * info files, and return all plugins directories.
13
 * MAYO page layouts uses:
14
 * - "plugins[page_layout][layout] = layouts/core"
15
 *
16
 * @param $theme_name, usually the active theme.
17
 */
18
function mayo_get_plugins($theme_name) {
19
  $plugins = drupal_static(__FUNCTION__, array());
20
  if (empty($plugins)) {
21
      $plugins_list = array();
22
      $themes_info = mayo_get_info_trail($theme_name);
23
      // Look for and get all the plugins
24
      if (!empty($themes_info)) {
25
        foreach ($themes_info as $this_theme => $theme_info) {
26
          foreach ($theme_info as $info) {
27
            if (array_key_exists('plugins', $info)) {
28
              foreach ($info['plugins'] as $plugin_type => $types) {
29
                $plugins_list[$this_theme][$plugin_type] = $types;
30
              }
31
            }
32
          }
33
        }
34
        array_unique($plugins_list);
35
        $plugins = $plugins_list;
36
      }
37
  }
38
  return $plugins;
39
}
40

    
41
/**
42
 * Return the paths to all plugin providers plugin directories, this usually
43
 * means themes - both base themes and sub-themes that include plugin directory
44
 * declarations in their info files.
45
 *
46
 * @param $theme_name, ususally the active theme.
47
 */
48
function mayo_get_plugins_paths($theme_name) {
49
  $provider_paths = array();
50
  $plugins_list = mayo_get_plugins($theme_name);
51

    
52
  foreach ($plugins_list as $plugin_provider => $provider) {
53
    foreach ($provider as $plugin_type => $types) {
54
      foreach ($types as $type => $path) {
55
        $provider_path = drupal_get_path('theme', $plugin_provider) . '/' . $path;
56
        $provider_paths[$plugin_provider][$plugin_type][$type] = $provider_path;
57
      }
58
    }
59
  }
60

    
61
  return $provider_paths;
62
}
63

    
64
/**
65
 * Returns all files for plugins of a particular type.
66
 * This is called from mayo_load_plugins(), cannot be cached else it will return
67
 * stale data at some point.
68
 *
69
 * @param $theme_name
70
 */
71
function mayo_get_plugins_files($theme_name) {
72
  $plugins_files = array();
73
  $plugins_list = mayo_get_plugins($theme_name);
74

    
75
  $extension = 'inc';
76
  foreach ($plugins_list as $plugin_provider => $provider) {
77
    foreach ($provider as $plugin_type => $types) {
78
      foreach ($types as $type => $path) {
79
        $provider_path = drupal_get_path('theme', $plugin_provider) . '/' . $path;
80
        $plugins_files[$plugin_provider][$plugin_type][$type] = file_scan_directory($provider_path, '/\.' . $extension . '$/', array('key' => 'name'));
81
      }
82
    }
83
  }
84

    
85
  return $plugins_files;
86
}
87

    
88
/**
89
 * Extract plugin data structures.
90
 *
91
 * In essence what this does is return the data strutures (arrays) for all
92
 * plugins of a particular type. MAYO only uses the "page_layout" type.
93
 * This is hard to cache because it takes the
94
 * $plugin_type parameter, so everything else that calls this is heavily cached
95
 * instead. It does support an "everything else" plugin type, whatever that is.
96
 *
97
 * @param $theme_name, usually the active theme.
98
 * @param $plugin_type, the plugin type you need to return, usually one of
99
 * "panels" or "page_layout".
100
 */
101
function mayo_load_plugins($theme_name, $plugin_type) {
102
  $plugin_data_structures = array();
103
  $plugins_list = mayo_get_plugins_files($theme_name);
104
  $plugins_array = array();
105
  foreach ($plugins_list as $plugin_provider => $plugin_types) {
106
    $plugin_providers[] = $plugin_provider;
107
    foreach ($plugin_types as $type => $plugins) {
108
      if ($type === $plugin_type) {
109
        foreach ($plugins as $ptypes => $plugin) {
110
          $plugins_array[$plugin_provider][$type] = $plugin;
111
        }
112
      }
113
    }
114
  }
115
  $plugin_files = array();
116
  foreach ($plugins_array as $provider => $types) {
117
    foreach ($types as $key => $value) {
118
      $plugin_files = array_merge_recursive($plugin_files, $value);
119
    }
120
  }
121

    
122
  foreach ($plugin_files as $file_data) {
123

    
124
    include_once(DRUPAL_ROOT . '/' . $file_data->uri);
125

    
126
    // page_layout
127
    if ($plugin_type === 'page_layout') {
128
      $identifier = $file_data->name;
129
      $page_layout_function = $identifier;
130
      if (function_exists($page_layout_function)) {
131
        $plugin_data_structures[] = $page_layout_function();
132
      }
133
    }
134

    
135
    // everything else
136
    elseif ($plugin_type !== 'panels' && $plugin_type !== 'page_layout') {
137
      $identifier = $file_data->name;
138
      $function = $identifier;
139
      if (function_exists($function)) {
140
        $plugin_data_structures[] = $function();
141
      }
142
      else {
143
        drupal_set_message(t('You defined an existing plugin type but no functions exists that match. If you are using Panels then you must use the "function method" in your plugins.'), 'error');
144
      }
145
    }
146
  }
147

    
148
  if (empty($plugin_data_structures)) {
149
    return;
150
  }
151
  return $plugin_data_structures;
152
}
153

    
154
/**
155
 * Return Page layout data structures.
156
 * This returns the full data structures for all page layout plugins. Because
157
 * this can be a lot of data and appears to be computationally expensive to get
158
 * it is cached in the cache table.
159
 *
160
 * @param $theme_name, the active theme.
161
 */
162
function page_layouts_data_structure($theme_name = NULL) {
163
  // Use the passed in theme_name, else grab it from the global variable
164
  if ($theme_name == NULL) {
165
    global $theme_key;
166
    $theme_name = $theme_key;
167
  }
168

    
169
  $page_data_structure = drupal_static(__FUNCTION__, array());
170
  if (empty($page_data_structure)) {
171
      $data_structure = mayo_load_plugins($theme_name, $plugin_type = 'page_layout');
172
      foreach ($data_structure as $plugin => $datum) {
173
        foreach ($datum as $method => $layout) {
174
          $page_data_structure[$method] = $layout;
175
        }
176
      }
177
  }
178
  return $page_data_structure;
179
}
180

    
181
/**
182
 * Return option arrays for forms.
183
 * Returns the options for radio lists in the page layout settings in the
184
 * appearance theme settings.
185
 *
186
 * @param $theme_name
187
 */
188
function page_layouts_device_group_options($theme_name) {
189
  $device_group_options = drupal_static(__FUNCTION__, array());
190
  if (empty($device_group_options)) {
191
    $layout_methods = page_layouts_data_structure();
192
    foreach ($layout_methods as $method => $values) {
193
      foreach ($values as $key => $value) {
194
        if ($key == 'device_groups') {
195
          $method_values[$method] = $value;
196
        }
197
      }
198
    }
199
    foreach ($method_values as $this_method => $these_values) {
200
      foreach ($these_values as $k => $dv) {
201
        $device_group_options[$dv][] = $this_method;
202
      }
203
    }
204
  }
205

    
206
  return $device_group_options;
207
}
208

    
209
/**
210
 * Base config for page layout builder.
211
 * This is used in mayo_core.submit.responsive.inc to help retrieve the form
212
 * values for each device groups layout.
213
 */
214
function assemble_page_layout() {
215
  $variables_array = array(
216
    'layout',
217
    'media_query',
218
    'page_width',
219
    'page_unit',
220
    'sidebar_first',
221
    'sidebar_second',
222
    'sidebar_unit',
223
  );
224
  return $variables_array;
225
}