This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handsontable.module
129 lines (113 loc) · 3.23 KB
/
handsontable.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
<?php
module_load_include('inc', 'objective_forms', 'Form');
/**
* Implements hook_help().
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
* @return string
*/
function handsontable_help($path) {
switch ($path) {
case "admin/help#handsontable":
return '<p>' . t("Adding Handsontable widget to Islandora XML forms") . '</p>';
break;
}
return '';
}
/**
* Implements hook_element_info()
*
* Adds the handsontable element to the form elements
*/
function handsontable_element_info() {
$types['handsontable'] = array(
'#input' => TRUE,
'#process' => array('handsontable_tag_process'),
'#theme_wrappers' => array('handsontable', 'form_element'),
);
$types['handsontable_row'] = array(
'#input' => TRUE,
'#process' => array('handsontable_row_tag_process'),
'#theme' => array('handsontable_row'),
);
return $types;
}
/**
* Implements hook_theme().
* @param $existing
* @param $type
* @param $theme
* @param $path
* @return array
*/
function handsontable_theme($existing, $type, $theme, $path) {
return [
'handsontable' => [
'render element' => 'element',
'file' => 'Handsontable.inc',
],
'handsontable_row' => [
'render element' => 'element',
'file' => 'HandsontableRow.inc',
],
];
}
/**
* Process a handsontable tag
*/
function handsontable_tag_process($element, &$form_state, $complete_form) {
form_load_include($form_state, 'inc', 'handsontable', 'Handsontable');
return Handsontable::process($element, $form_state, $complete_form);
}
/**
* Process a handsontable row tag
*/
function handsontable_row_tag_process($element, &$form_state, $complete_form) {
form_load_include($form_state, 'inc', 'handsontable', 'HandsontableRow');
return HandsontableRow::process($element, $form_state, $complete_form);
}
/**
* This function is triggered from objective_forms Form::ajaxAlter() from the addRow callback
*
* @param FormElement $element
* @param array $form
* @param array $form_state
* @return bool
*/
function handsontable_form_element_handsontable_ajax_alter(FormElement $element, array &$form, array &$form_state) {
// Get the reported number of rows by handsontable in the POST request
$rowsLocation = $element->getParentsArray();
$rowsLocation[] = 'rows';
$rows = drupal_array_get_nested_value($form_state['values'], $rowsLocation);
// Get the number of children that we know right now
$children = count($element->children);
// Either remove or add rows
$diff = $rows - $children;
if ( $diff > 0 ) {
do {
$new = clone reset($element->children);
$new->eachDecendant(function($element) {
$element->default_value = NULL;
});
$element->adopt($new);
$form[] = $new->toArray();
$diff--;
} while ( $diff > 0);
}
elseif ($diff < 0) {
do {
// TODO: The removal of rows is not called at the moment. Because handsontable
// does not reduce the number of rows
$last = end($element->children);
$last->orphan();
unset($form[count($element->children)]);
$diff++;
} while ( $diff < 0);
}
return TRUE;
}