Projet

Général

Profil

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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