Projet

Général

Profil

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

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

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
        $plugins = $plugins_list;
35
      }
36
  }
37
  return $plugins;
38
}
39

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

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

    
60
  return $provider_paths;
61
}
62

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

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

    
84
  return $plugins_files;
85
}
86

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

    
121
  foreach ($plugin_files as $file_data) {
122

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

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

    
134
    // everything else
135
    elseif ($plugin_type !== 'panels' && $plugin_type !== 'page_layout') {
136
      $identifier = $file_data->name;
137
      $function = $identifier;
138
      if (function_exists($function)) {
139
        $plugin_data_structures[] = $function();
140
      }
141
      else {
142
        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');
143
      }
144
    }
145
  }
146

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

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

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

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

    
205
  return $device_group_options;
206
}
207

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