-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrategy_field.module
More file actions
298 lines (269 loc) · 8.93 KB
/
Copy pathstrategy_field.module
File metadata and controls
298 lines (269 loc) · 8.93 KB
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
<?php
/**
* Implements hook_field_info().
*/
function strategy_field_field_info() {
$field_info['strategy_field'] = array(
'label' => t('Strategy Field'),
'description' => t('This field implements the Strategy pattern for dynamic processing at runtime.'),
'default_widget' => 'strategy_field_widget',
'default_formatter' => 'strategy_field_formatter',
);
return $field_info;
}
/**
* Implements hook_field_widget_info().
*/
function strategy_field_field_widget_info() {
return array(
'strategy_field_widget' => array(
'label' => t('Default Strategy Field'),
'field types' => array('strategy_field'),
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function strategy_field_field_formatter_info() {
return array(
'strategy_field_formatter' => array(
'label' => t('Default Strategy Field'),
'field types' => array('strategy_field'),
),
);
}
/**
* Implements hook_field_is_empty().
*/
function strategy_field_field_is_empty($item, $field) {
return empty($item['class']);
}
/**
* Implements hook_field_widget_form().
*/
function strategy_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$class_options = array();
if (!$instance['required']) {
$class_options[''] = t('None');
}
$defined_class = NULL;
// Get Class options, for each class option, get form field
// Get all strategy field base classes
$strategy_type = $field['settings']['strategy_type'];
$strategy_field_info = module_invoke_all('strategy_field_info');
$classes = $strategy_field_info[$strategy_type];
if (count($classes)){
foreach ($classes as $class_name) {
$class_options[$class_name] = $class_name::getLabel();
}
$field_name = $instance['field_name'];
}
if (array_key_exists($delta, $items) && $items[$delta]['class'] && $field['cardinality'] != 1){
$defined_class = $items[$delta]['class'];
$element['class'] = array(
'#type' => 'hidden',
'#default_value' => $items[$delta]['class'],
'#attributes' => array('class' => array('strategy-class')),
);
$element['remove_link'] = array('#markup' => '<a class="strategy_field_remove" data-class="' . $class_options[$defined_class] .'" data-delta="' . $delta . '">X</a>');
$element['class_name'] = array(
'#markup' => '<h4>' . $class_options[$defined_class] . '</h4>',
);
drupal_add_js(drupal_get_path('module', 'strategy_field') . '/strategy_field.js', array('weight' => 10));
drupal_add_css(drupal_get_path('module', 'strategy_field') . '/strategy_field.css');
} else {
$element['class'] = array(
'#type' => 'select',
'#default_value' => empty($items[$delta]['class']) ? '' : $items[$delta]['class'],
'#options' => $class_options,
'#title' => $strategy_type,
'#attributes' => array('class' => array('chosen-widget')),
'#suffix' => '<br/>',
);
}
$element['strategy_settings'] = array(
'#type' => 'container',
);
if (is_array($classes)){
foreach ($classes as $class_name) {
if (isset($defined_class) && $class_name != $defined_class){
continue;
}
$element['strategy_settings'][$class_name] = array(
'#type' => 'fieldset',
'#title' => '<strong>' . t(
'%class_label settings',
array('%class_label' => $class_name::getLabel())
) . '</strong>',
'#description' => $class_name::getDescription(),
'#states' => array(
'visible' => array(
':input[name="' . $field_name . '[und][' . $delta . '][class]"]' => array('value' => $class_name)
),
),
);
$strategy_settings = array();
if (!empty($items[$delta]['class']) && !strcmp($items[$delta]['class'], $class_name)) {
$strategy_settings = $items[$delta]['strategy_settings'];
}
$class_object = new $class_name($strategy_settings);
$element['strategy_settings'][$class_name] += $class_object->getForm();
}
}
return $element;
}
/**
* Implements hook_field_settings_form().
*/
function strategy_field_field_settings_form($field, $instance, $has_data) {
// Get all strategy field base classes
$strategy_field_info = module_invoke_all('strategy_field_info');
$settings = $field['settings'];
$form = array();
$form['strategy_type'] = array(
'#type' => 'select',
'#title' => t('Strategy Field Type'),
'#default_value' => $settings['strategy_type'],
'#required' => TRUE,
'#description' => t('The type of Strategy.'),
'#disabled' => $has_data,
'#options' => array_combine(array_keys($strategy_field_info), array_keys($strategy_field_info)),
);
return $form;
}
/**
* Implements hook_field_presave().
*/
function strategy_field_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach($items as $i => $item) {
$strategy_settings = isset($item['strategy_settings'][$item['class']]) ? $item['strategy_settings'][$item['class']] : $item['strategy_settings'];
$items[$i]['strategy_settings'] = serialize($strategy_settings);
}
}
/**
* Implements hook_field_load().
*/
function strategy_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
if (!is_array($item['strategy_settings'])) {
$items[$id][$delta]['strategy_settings'] = unserialize($item['strategy_settings']);
$items[$id][$delta]['object'] = new $item['class']($items[$id][$delta]['strategy_settings']);
}
}
}
}
/**
* Implements hook_field_formatter_view().
*/
function strategy_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
if (!is_array($item['strategy_settings'])) {
$item['strategy_settings'] = unserialize($item['strategy_settings']);
}
$item['display'] = $display;
$element[$delta] = array(
'#markup' => theme('strategy_field_formatter_default', $item),
);
}
return $element;
}
/**
* Implements hook_hook_info().
*/
function strategy_field_hook_info() {
return array(
'strategy_field_info' => array(
'group' => 'strategy_field',
),
);
}
/**
* Implements hook_theme().
*/
function strategy_field_theme() {
return array(
'strategy_field_formatter_default' => array(
'variables' => array(
'class' => NULL,
'values' => array(),
'object' => NULL,
'display' => NULL,
),
),
);
}
/**
* Formats a link.
*/
function theme_strategy_field_formatter_default($vars) {
$formatted_strategy_field = $vars['object']->formatter($vars['display']);
$output = array(
'#theme' => 'html_tag',
'#tag' => 'fieldset',
'#value_prefix' => '<legend><strong>' . $vars['class']::getLabel() . '</strong></legend>',
'#value' => drupal_render($formatted_strategy_field),
);
return drupal_render($output);
}
/**
* Implements hook_field_field_validate().
*/
function strategy_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
// Check which class is set, then call validate for that class
if (isset($item['class']) && !empty($item['class'])) {
$strategy_obj = new $item['class']($item['strategy_settings'][$item['class']]);
$validation_errors = $strategy_obj->validate();
foreach ($validation_errors as $error_msg) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'strategy_field_error',
'message' => t(
'%title: @class: !error_msg',
array(
'%title' => $instance['label'],
'@class' => $item['class'],
'!error_msg' => $error_msg,
)
),
);
}
}
}
}
/**
* Implements hook_field_widget_error().
*/
function strategy_field_field_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'strategy_field_error':
form_error($element, $error['message']);
break;
}
}
/*
* Helper function to return form element items for a strategy field widget (for use in hook_form_alter()).
*/
function strategy_field_get_all_widget_forms(&$form_field) {
$langcode = $form_field['#language'];
$widget_forms = array();
foreach ($form_field[$langcode] as $delta => &$strategy) {
if (is_numeric($delta)) {
if ($class_name = $strategy['class']['#default_value']) {
$widget_forms[$delta][$class_name] =& $strategy['strategy_settings'][$class_name];
}
else {
if (isset($strategy['class']['#options'])) {
foreach (array_keys($strategy['class']['#options']) as $class_name) {
if (isset($strategy['strategy_settings'][$class_name])) {
$widget_forms[$delta][$class_name] =& $strategy['strategy_settings'][$class_name];
}
}
}
}
}
}
return $widget_forms;
}