Projet

Général

Profil

Paste
Télécharger (3,15 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / advanced_forum / includes / plugins.inc @ c169e7c4

1
<?php
2

    
3

    
4
/**
5
 * @file
6
 * Allows modules to contribute styles.
7
 */
8

    
9
// -----------------------------------------------------------------------
10
// REQUEST FUNCTIONS
11

    
12
/**
13
 * Fetch an advanced forum style plugin
14
 *
15
 * @param $style
16
 *   Name of the style plugin
17
 *
18
 * @return
19
 *   An array with information about the requested style plugin.
20
 */
21
function advanced_forum_get_style($style) {
22
  return advanced_forum_get_plugins('styles', 'advanced_forum_styles', $style);
23
}
24

    
25
/**
26
 * Collate information about all available advanced_forum styles.
27
 *
28
 * @return
29
 *   An array with information about the requested forum style.
30
 */
31
function advanced_forum_get_styles() {
32
  return advanced_forum_get_plugins('styles', 'advanced_forum_styles');
33
}
34

    
35
// -----------------------------------------------------------------------
36
// MASTER HANDLER
37

    
38
/**
39
 * Fetch a group of plugins by name.
40
 *
41
 * @param $plugin
42
 *   This is the name of the plugin, and also the name of the directory.
43
 * @param $hook
44
 *   This is the hook to call to get the info for the plugin.
45
 *
46
 * @return
47
 *   An array of information arrays about the plugins received.
48
 */
49
function advanced_forum_get_plugins($plugin, $hook, $id = NULL) {
50
  static $plugins = array();
51
  static $all_hooks = array();
52

    
53
  // Always load all hooks if we need them.
54
  if (!isset($all_hooks[$plugin])) {
55
    $all_hooks[$plugin] = TRUE;
56
    $plugins[$plugin] = advanced_forum_load_hooks($hook);
57
  }
58

    
59
  // If a specific plugin $id is being requested, return it.
60
  if ($id && array_key_exists($id, $plugins[$plugin])) {
61
    return $plugins[$plugin][$id];
62
  }
63

    
64
  // If no $id was requested, return the lot.
65
  if (!$id) {
66
    return $plugins[$plugin];
67
  }
68
}
69

    
70
// -----------------------------------------------------------------------
71
// WORKER/RETRIEVER FUNCTIONS
72

    
73
/**
74
 * Load plugin info for all hooks; this is handled separately from plugins
75
 * from files.
76
 *
77
 * @param $hook
78
 *   The hook being invoked.
79
 *
80
 * @return
81
 *   An array of info supplied by any hook implementations.
82
 */
83
function advanced_forum_load_hooks($hook) {
84
  $info = array();
85
  foreach (module_implements($hook) as $module) {
86
    $result = _advanced_forum_process_plugin($module, $module, drupal_get_path('module', $module), $hook);
87
    if (is_array($result)) {
88
      $info = array_merge($info, $result);
89
    }
90
  }
91
  return $info;
92
}
93

    
94
/**
95
 * Process a single hook implementation of a advanced_forum plugin.
96
 *
97
 * @param $module
98
 *   The module that owns the hook.
99
 * @param $identifier
100
 *   Either the module or 'advanced_forum_' . $file->name
101
 * @param $hook
102
 *   The name of the hook being invoked.
103
 */
104
function _advanced_forum_process_plugin($module, $identifier, $path, $hook) {
105
  $function = $identifier . '_' . $hook;
106
  if (!function_exists($function)) {
107
    return NULL;
108
  }
109
  $result = $function();
110
  if (!isset($result) || !is_array($result)) {
111
    return NULL;
112
  }
113

    
114
  // Fill in defaults.
115
  foreach ($result as $name => $plugin) {
116
    if (!is_dir($path . '/' . $plugin['directory'])) {
117
      unset($result[$name]);
118
      continue;
119
    }
120
    $result[$name] += array(
121
      'module' => $module,
122
      'name' => $name,
123
      'path' => $path . '/' . $plugin['directory'],
124
    );
125
  }
126

    
127
  return !empty($result) ? $result : NULL;
128
}