Projet

Général

Profil

Paste
Télécharger (7,37 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / plugins-admin.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains generic plugin administration functions.
6
 *
7
 * CTools includes the ability to (relatively) easily provide wizard based
8
 * configuration for plugins, meaning plugins that need configuration can
9
 * automatically allow multi-step forms.
10
 *
11
 * Implementing this
12
 */
13

    
14
/**
15
 * Get a plugin configuration form.
16
 *
17
 * The $form_info and $form_state need to be preconfigured with data you'll need
18
 * such as whether or not you're using ajax, or the modal. $form_info will need
19
 * your next/submit callbacks so that you can cache your data appropriately.
20
 *
21
 * @param array $form_info
22
 *   This form_info *must* contain at least the path. next and finish callbacks
23
 *   are advisable but not necessary if using wizard's auto caching. Setting
24
 *   a cache mechanism is advisable. If not using auto caching, next and finish
25
 *   callbacks will be necessary.
26
 *
27
 *   Also be sure to set up things such as AJAX settings and any temporary
28
 *   data you will need. Simply setting 'modal' => TRUE and
29
 *   'modal return' => TRUE should make this system work well in the modal.
30
 *
31
 *   In addition to standard wizard fields, this supports one extra field:
32
 *   - 'default form': A callback to a 'wrapper' that will be applied to either
33
 *     the first or a marked form. This is useful for adding global features that
34
 *     are applicable to all instances of a plugin, such as identifiers, or
35
 *     contexts, or the like.
36
 *
37
 * @param array &$form_state
38
 *   This is a standard form state array. This system imposes some requirements
39
 *   on what will be in the form state:
40
 *
41
 *   - 'plugin': The plugin definition being edited.
42
 *   - 'conf': The configuration that is being edited, presumed to be an array.
43
 *             Ultimately at the end, this is where the modified config will be
44
 *             found.
45
 *   - 'op': The operation, either 'add' or 'edit'. This is used to derive form
46
 *           names and can also be used to derive default values from the plugin.
47
 *   - 'step': The current step. May be null if it is the 'first' step, but
48
 *             generally needs to be defined in the path.
49
 *
50
 * @param string $default_form
51
 *   An optional default form that can be added.
52
 *
53
 * @return
54
 *   If this function returns false, no form exists.
55
 */
56
function ctools_plugin_configure_form($form_info, &$form_state) {
57
  // Turn the forms defined in the plugin into the format the wizard needs.
58
  _ctools_plugin_configure_create_form_info($form_info, $form_state['plugin'], $form_state['op']);
59

    
60
  if (empty($form_info['order'])) {
61
    return FALSE;
62
  }
63

    
64
  ctools_include('wizard');
65
  return ctools_wizard_multistep_form($form_info, $form_state['step'], $form_state);
66
}
67

    
68
function _ctools_plugin_configure_create_form_info(&$form_info, $plugin_definition, $op) {
69
  // Provide a few extra defaults.
70
  $form_info += array(
71
    'id' => 'ctools_plugin_configure_form',
72
    'show back' => TRUE,
73
  );
74

    
75
  $add_form = isset($form_info['add form name']) ? $form_info['add form name'] : 'add form';
76
  $edit_form = isset($form_info['edit form name']) ? $form_info['edit form name'] : 'edit form';
77

    
78
  // Figure out what the forms should actually be. Since they can be specified
79
  // in a couple of different ways (in order to support simple declarations for
80
  // the minimal forms but more complex declarations for powerful wizards).
81
  if ($op == 'add') {
82
    if (!empty($plugin_definition[$add_form])) {
83
      $info = $plugin_definition[$add_form];
84
    }
85
  }
86
  if (!isset($info) || $op == 'edit') {
87
    // Use the edit form for the add form if add form was completely left off.
88
    if (!empty($plugin_definition[$edit_form])) {
89
      $info = $plugin_definition[$edit_form];
90
    }
91
  }
92

    
93
  // If there is a default form wrapper, but no form is supplied,
94
  // use the wrapper as the form.
95
  if (empty($info) && !empty($form_info['default form'])) {
96
    $info = $form_info['default form'];
97
  }
98

    
99
  // @todo we may want to make these titles more settable?
100
  if (is_string($info)) {
101
    if (empty($plugin_definition['title'])) {
102
      $title = t('Configure');
103
    }
104
    elseif ($op == 'add') {
105
      $title = t('Configure new !plugin_title', array('!plugin_title' => $plugin_definition['title']));
106
    }
107
    else {
108
      $title = t('Configure !plugin_title', array('!plugin_title' => $plugin_definition['title']));
109
    }
110
    if (empty($form_info['order'])) {
111
      $form_info['order'] = array();
112
    }
113
    $form_info['order']['form'] = $title;
114

    
115
    if (empty($form_info['forms'])) {
116
      $form_info['forms'] = array();
117
    }
118
    $form_info['forms']['form'] = array(
119
      'title' => $title,
120
      'form id' => $info,
121
    );
122

    
123
    // Add the default form if one is specified.
124
    if (!empty($form_info['default form']) && $form_info['forms']['form']['form id'] != $form_info['default form']) {
125
      $form_info['forms']['form']['wrapper'] = $form_info['default form'];
126
    }
127

    
128
    // If no submit is supplied, supply the default submit which will do the
129
    // most obvious task.
130
    if (!function_exists($form_info['forms']['form']['form id'] . '_submit')) {
131
      // Store the original wrapper so we can chain it.
132
      if (!empty($form_info['forms']['form']['wrapper'])) {
133
        $form_info['forms']['form']['original wrapper'] = $form_info['forms']['form']['wrapper'];
134
      }
135
      $form_info['forms']['form']['wrapper'] = 'ctools_plugins_default_form_wrapper';
136
    }
137
  }
138
  elseif (is_array($info)) {
139
    if (empty($form_info['order'])) {
140
      $form_info['order'] = array();
141
    }
142
    if (empty($form_info['forms'])) {
143
      $form_info['forms'] = array();
144
    }
145
    $count = 0;
146
    $base = 'step';
147
    $wrapper = NULL;
148
    foreach ($info as $form_id => $title) {
149
      $step = $base . ++$count;
150
      if (empty($wrapper)) {
151
        $wrapper = $step;
152
      }
153

    
154
      if (is_array($title)) {
155
        if (!empty($title['default'])) {
156
          $wrapper = $step;
157
        }
158
        $title = $title['title'];
159
      }
160

    
161
      $form_info['order'][$step] = $title;
162
      $form_info['forms'][$step] = array(
163
        'title' => $title,
164
        'form id' => $form_id,
165
      );
166
    }
167
    if ($wrapper && !empty($form_info['default form'])) {
168
      $form_info['forms'][$wrapper]['wrapper'] = $form_info['default form'];
169
    }
170
  }
171
}
172

    
173
/**
174
 * A wrapper to provide a default submit so that plugins don't have to duplicate
175
 * a whole bunch of code to do what most of them want to do anyway.
176
 */
177
function ctools_plugins_default_form_wrapper($form, &$form_state) {
178
  $form_info = &$form_state['form_info'];
179
  $info = $form_info['forms'][$form_state['step']];
180

    
181
  if (isset($info['original wrapper']) && function_exists($info['original wrapper'])) {
182
    $form = $info['original wrapper']($form, $form_state);
183
  }
184

    
185
  if (isset($form['buttons']['next'])) {
186
    if (empty($form['buttons']['next']['#submit'])) {
187
      $form['buttons']['next']['#submit'] = $form['#submit'];
188
    }
189
    $form['buttons']['next']['#submit'][] = 'ctools_plugins_default_form_wrapper_submit';
190
  }
191
  if (isset($form['buttons']['return'])) {
192
    if (empty($form['buttons']['return']['#submit'])) {
193
      $form['buttons']['return']['#submit'] = $form['#submit'];
194
    }
195
    $form['buttons']['return']['#submit'][] = 'ctools_plugins_default_form_wrapper_submit';
196
  }
197
  return $form;
198
}
199

    
200
/**
201
 * Provide a default storage mechanism.
202
 */
203
function ctools_plugins_default_form_wrapper_submit(&$form, &$form_state) {
204
  foreach (array_keys($form_state['plugin']['defaults']) as $key) {
205
    if (isset($form_state['values'][$key])) {
206
      $form_state['conf'][$key] = $form_state['values'][$key];
207
    }
208
  }
209
}