This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentityform_ctools.module
380 lines (343 loc) · 12.1 KB
/
entityform_ctools.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* @file
* Main code.
*
* @author
* Name: Nikita Malyshev
* Web: http://niklan.net
* Email: hello@niklan.net
*/
require_once 'entityform_ctools.filter.inc';
define('ENTITYFORM_CTOOLS_AJAX_DISABLED', -1);
define('ENTITYFORM_CTOOLS_AJAX_NOTLISTED', 0);
define('ENTITYFORM_CTOOLS_AJAX_LISTED', 1);
define('ENTITYFORM_CTOOLS_AJAX_OWN', 2);
/**
* Implements hook_menu().
*/
function entityform_ctools_menu() {
// @TODO handle to editing existing entityforms in ctools.
$items['entityform_ctools/%ctools_js/add/%'] = array(
'title' => 'Entityform Ctools Page',
'page callback' => 'entityform_ctools_page_callback',
'page arguments' => array(3, 1),
// @TODO add callback
'access callback' => TRUE,
'delivery callback' => 'ajax_deliver',
'theme callback' => 'ajax_base_page_theme',
);
$items['entityform_ctools/ajax'] = array(
'title' => 'Entityform Ajax Callback',
'page callback' => 'entityform_ctools_ajax_callback',
'access callback' => TRUE,
'delivery callback' => 'ajax_deliver',
'theme callback' => 'ajax_base_page_theme',
);
return $items;
}
/**
* Include modal libraries.
*/
function _entityform_ctools_include_modal() {
static $added = FALSE;
if ($added == FALSE) {
$added = TRUE;
// Include the CTools tools that we need.
ctools_include('modal');
ctools_include('ajax');
ctools_modal_add_js();
// @see http://cgit.drupalcode.org/ctools/tree/help/modal.html
$style = array(
'entityform' => array(
'modalSize' => array(
),
'modalTheme' => 'entityform_ctools_modal',
'throbberTheme' => 'entityform_ctools_throbber',
'modalClass' => 'entityform',
// This is css array will apply automatically.
'modalOptions' => array(),
// show, fadeIn or slideDown
'animation' => 'show',
// slow, medium or fast
'animationSpeed' => 'fast',
'closeText' => '',
'closeImage' => '',
// @TODO translate
'loadingText' => 'Секундочку, форма загружается…',
'throbber' => theme('image', array(
'path' => ctools_image_path('throbber.gif', 'entityform_ctools'),
'alt' => t('Loading...'),
'title' => t('Loading')
)),
),
);
drupal_add_js($style, 'setting');
ctools_add_js('entityform-modal', 'entityform_ctools');
}
}
/**
* @param null $js
* @return array
*/
function entityform_ctools_page_callback($entityform_name, $js = NULL) {
ctools_include('entityform.admin', 'entityform', '');
ctools_include('modal');
ctools_include('ajax');
$entityform_types_objects = entityform_get_types();
// If disabled JavaScript.
if (!$js) {
$entityform_info = $entityform_types_objects[$entityform_name];
if (isset($entityform_info->paths['submit']['alias'])) {
$redirect_path = $entityform_info->paths['submit']['alias'];
}
else {
$redirect_path = 'eform/submit/' . $entityform_name;
}
drupal_goto($redirect_path, array(), 301);
}
$title = $entityform_types_objects[$entityform_name]->label;
if (module_exists('entityform_i18n')) {
$title = $entityform_types_objects[$entityform_name]->getTranslation('label');
}
$form_state = array(
'title' => $title,
'ajax' => TRUE,
'is_modal' => TRUE,
'build_info' => array(
'args' => array(
entityform_empty_load($entityform_name),
'submit',
'embedded'
),
),
'query_parameters' => drupal_get_query_parameters(),
);
$commands = ctools_modal_form_wrapper($entityform_name . '_entityform_edit_form', $form_state);
if (!empty($form_state['executed'])) {
$commands = array();
$commands[] = ajax_command_prepend('#modal-content', theme('status_messages'));
}
// hook_entityform_ctools_FORM_ID_commands_alter().
drupal_alter('entityform_ctools_' . $entityform_name . '_commands', $commands, $form_state);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Creates ctools link form creating new entityform in modal.
*/
function entityform_ctools_add_link($text, $entityform_name, $theme = '', $options = array()) {
_entityform_ctools_include_modal();
drupal_add_library('system', 'drupal.ajax');
$default_options = array(
'html' => TRUE,
'attributes' => array(
'class' => array('ctools-use-modal', 'ctools-modal-' . $theme),
),
);
return l($text, 'entityform_ctools/nojs/add/' . $entityform_name, array_merge_recursive($default_options, $options));
}
/**
* Callback for ajax submissions of entityform.
*/
function entityform_ctools_ajax_callback() {
module_load_include('inc', 'entityform', 'entityform.admin');
list($form, $form_state) = ajax_get_form();
array_unshift($form['#after_build'], 'entityform_ctools_form_entityform_edit_form_after_build');
drupal_process_form($form['#form_id'], $form, $form_state);
$output = drupal_render($form);
$form_html_id = '#' . $form_state['complete form']['#id'];
$commands = array();
if ($messages = theme('status_messages')) {
$commands[] = ajax_command_html('#' . $form['#form_id'] . '-messages', $messages);
}
$commands[] = ajax_command_replace($form_html_id, $output);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Get entityform form with ajax submit handler.
*/
function entityform_ctools_get_ajax_form($entityform_name) {
module_load_include('inc', 'entityform', 'entityform.admin');
drupal_add_library('system', 'jquery.form');
drupal_add_library('system', 'drupal.form');
drupal_add_library('system', 'drupal.ajax');
drupal_add_library('system', 'drupal.progress');
$form_id = $entityform_name . '_entityform_edit_form';
$form_state = array(
'ajax' => TRUE,
'rebuild' => FALSE,
'cache' => TRUE,
'build_info' => array(
'args' => array(
entityform_empty_load($entityform_name),
'submit',
'embedded'
),
),
);
$form = drupal_build_form($form_id, $form_state);
$action_path = 'entityform_ctools/ajax';
$form['#action'] = '/' . $action_path;
$form['actions']['submit']['#ajax'] = array('path' => $action_path);
$form['actions']['submit']['#attributes']['class'][] = 'use-ajax-submit';
$form_id = $entityform_name . '_entityform_edit_form';
$messages = '<div id="' . $form_id . '-messages"></div>';
$form['#prefix'] = $messages;
$form['#after_build'][] = 'entityform_ctools_form_entityform_edit_form_after_build';
// Filed uploading to prevent "The response failed verification so will not be
// processed."
$form['#attached']['js'][] = array(
'type' => 'setting',
'data' => array(
'urlIsAjaxTrusted' => array(
'/entityform_ctools/ajax' => TRUE,
),
),
);
return $form;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function entityform_ctools_form_entityform_type_form_alter(&$form, &$form_state, $form_id) {
$type = $form['#entityform_type']->type;
$default_settings = array(
'visibility' => ENTITYFORM_CTOOLS_AJAX_DISABLED,
'paths' => '',
);
$settings = variable_get('entityform_ctools_ajax_settings_' . $type, $default_settings);
$form['entityform_ctools'] = array(
'#type' => 'fieldset',
'#title' => 'AJAX',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#weight' => 999,
'#tree' => TRUE,
);
$options = array(
ENTITYFORM_CTOOLS_AJAX_DISABLED => t('Disable'),
ENTITYFORM_CTOOLS_AJAX_LISTED => t('Only the listed pages'),
ENTITYFORM_CTOOLS_AJAX_NOTLISTED => t('All pages except those listed'),
ENTITYFORM_CTOOLS_AJAX_OWN => t('Only on default form page'),
);
$title = t('Pages or PHP code');
$description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>'
));
$form['entityform_ctools']['visibility'] = array(
'#type' => 'radios',
'#title' => t('Make this form AJAX on specific pages'),
'#options' => $options,
'#default_value' => $settings['visibility'],
);
$form['entityform_ctools']['paths'] = array(
'#type' => 'textarea',
'#title' => '<span class="element-invisible">' . $title . '</span>',
'#default_value' => $settings['paths'],
'#description' => $description,
);
$form['#submit'][] = 'entityform_ctools_form_entityform_type_form_submit';
}
/**
* Submit handler for entityform settings form for saving custom settings.
*/
function entityform_ctools_form_entityform_type_form_submit(&$form, &$form_state) {
$type = $form['#entityform_type']->type;
variable_set('entityform_ctools_ajax_settings_' . $type, $form_state['values']['entityform_ctools']);
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function entityform_ctools_form_entityform_edit_form_alter(&$form, &$form_state, $form_id) {
$type = $form['#entity']->type;
$entityform_object = entityform_type_load($type);
$default_settings = array(
'visibility' => ENTITYFORM_CTOOLS_AJAX_DISABLED,
'paths' => '',
);
$settings = variable_get('entityform_ctools_ajax_settings_' . $type, $default_settings);
switch ($settings['visibility']) {
case ENTITYFORM_CTOOLS_AJAX_DISABLED:
$page_match = FALSE;
break;
case ENTITYFORM_CTOOLS_AJAX_NOTLISTED:
case ENTITYFORM_CTOOLS_AJAX_LISTED:
if ($settings['paths']) {
$paths = drupal_strtolower($settings['paths']);
$current_path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
$page_match = drupal_match_path($current_path, $paths);
if ($current_path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $paths);
}
if ($settings['visibility'] == ENTITYFORM_CTOOLS_AJAX_NOTLISTED) {
$page_match = !$page_match;
}
}
else {
$page_match = FALSE;
}
break;
case ENTITYFORM_CTOOLS_AJAX_OWN:
$paths = $entityform_object->paths['submit']['source'];
if (!empty($entityform_object->paths['submit']['alias'])) {
$paths .= "\n\r" . $entityform_object->paths['submit']['alias'];
}
$current_path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
$page_match = drupal_match_path($current_path, $paths);
break;
default:
$page_match = FALSE;
break;
}
if ($page_match) {
array_unshift($form['#after_build'], 'entityform_ctools_form_entityform_edit_form_after_build');
}
}
/**
* After build callback.
*/
function entityform_ctools_form_entityform_edit_form_after_build($form, &$form_state) {
module_load_include('inc', 'entityform', 'entityform.admin');
drupal_add_library('system', 'jquery.form');
drupal_add_library('system', 'drupal.form');
drupal_add_library('system', 'drupal.ajax');
drupal_add_library('system', 'drupal.progress');
$type = $form['#entity']->type;
$entityform_type = entityform_type_load($type);
$form_state_new = array(
'ajax' => TRUE,
'rebuild' => FALSE,
'cache' => TRUE,
'form_id' => $type . '_entityform_edit_form',
'build_info' => array(
'args' => array(
entityform_empty_load($type),
'submit',
'page'
),
),
);
$form_state = array_merge($form_state, $form_state_new);
$action_path = 'entityform_ctools/ajax';
$form['#action'] = '/' . $action_path;
$form['actions']['submit']['#ajax'] = array('path' => $action_path);
$form['actions']['submit']['#attributes']['class'][] = 'use-ajax-submit';
// We must attach intro again, because after AJAX request this element for
// some reason is missing. This is restore it.
if (!empty($entityform_type->data['instruction_pre'])) {
$form['intro'] = array(
'#type' => 'markup',
'#markup' => "<div class='pre-instructions' >" . _entityform_format_text($entityform_type->data['instruction_pre'], array('entityform_type' => $entityform_type)) . "</div>",
'#weight' => -100,
);
}
if (!$form_state['process_input']) {
$form_id = $type . '_entityform_edit_form';
$messages = '<div id="' . $form_id . '-messages"></div>';
$form['#prefix'] = $messages;
}
return $form;
}