summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Borchert2013-01-18 23:43:03 (GMT)
committer Stefan Borchert2013-01-18 23:43:03 (GMT)
commit6efd1655bf3b1cd87eb5eb284fcb8ccea0fc6192 (patch)
treeb66bc17db587bd6923598023e96aec6568d74927
parent7a2dabf896d02d2bdd7884c869145903f652f1dd (diff)
Add value callback and remove this nasty form_alter to fake term values.7.x-1.1
Signed-off-by: Stefan Borchert <stefan.borchert@undpaul.de>
-rw-r--r--js/shs.js54
-rw-r--r--shs.module45
2 files changed, 50 insertions, 49 deletions
diff --git a/js/shs.js b/js/shs.js
index 91d0546..0987dc9 100644
--- a/js/shs.js
+++ b/js/shs.js
@@ -119,8 +119,12 @@
* Container for "Add new" elements.
* @param term
* The new term object.
+ * @param base_id
+ * ID of original field which is rewritten as "taxonomy_shs".
+ * @param level
+ * Current level in hierarchy.
*/
- termAddNew = function($triggering_element, $container, term) {
+ termAddNew = function($triggering_element, $container, term, base_id, level) {
$.ajax({
url: Drupal.settings.basePath + 'shs/json',
type: 'POST',
@@ -147,6 +151,8 @@
options[options.length] = new Option(data.data.name, data.data.tid);
// Set new default value.
$triggering_element.val(data.data.tid);
+ // Set value of original field.
+ updateFieldValue($triggering_element, base_id, level);
}
},
error: function(xhr, status, error) {
@@ -228,7 +234,7 @@
name: termName
};
if (termName.length > 0) {
- termAddNew($triggering_element, $container, term);
+ termAddNew($triggering_element, $container, term, base_id, level);
}
else {
// Remove container.
@@ -249,23 +255,10 @@
getTermChildren($element_new, settings, $triggering_element.val(), 0);
}
- // Reset value of original field.
- $field_orig = $('#' + base_id);
- $field_orig.val(0);
- // Set original field value.
- if ($triggering_element.val() == 0 || $triggering_element.val() == '_add_new_') {
- if (level > 1) {
- // Use value from parent level.
- $field_orig.val($triggering_element.prev('select').val());
- }
- }
- else {
- // Use value from current field.
- $field_orig.val($triggering_element.val());
- }
+ // Set value of original field.
+ updateFieldValue($triggering_element, base_id, level);
}
-
/**
* Create a new <select> element.
*
@@ -292,4 +285,31 @@
return $element;
}
+ /**
+ * Update value of original (hidden) field.
+ *
+ * @param $triggering_element
+ * Element which has been changed.
+ * @param base_id
+ * ID of original field which is rewritten as "taxonomy_shs".
+ * @param level
+ * Current level in hierarchy.
+ */
+ updateFieldValue = function($triggering_element, base_id, level) {
+ // Reset value of original field.
+ $field_orig = $('#' + base_id);
+ $field_orig.val(0);
+ // Set original field value.
+ if ($triggering_element.val() == 0 || $triggering_element.val() == '_add_new_') {
+ if (level > 1) {
+ // Use value from parent level.
+ $field_orig.val($triggering_element.prev('select').val());
+ }
+ }
+ else {
+ // Use value from current field.
+ $field_orig.val($triggering_element.val());
+ }
+ }
+
})(jQuery);
diff --git a/shs.module b/shs.module
index a1cff1a..6d48fa7 100644
--- a/shs.module
+++ b/shs.module
@@ -131,37 +131,6 @@ function shs_views_data_alter(&$data) {
}
/**
- * Implements hook_form_FORM_ID_alter().
- */
-function shs_form_node_form_alter(&$form, &$form_state, $form_id) {
- $instances = _shs_get_instances($form['#entity_type'], $form['#bundle']);
- if (count($instances)) {
- // Add custom submit callback.
- array_unshift($form['#submit'], 'shs_node_form_submit');
- }
-}
-
-/**
- * Custom submit callback for node forms with widgets of type "shs_taxonomy".
- *
- * @param <array> $form
- * Submitted form.
- * @param <array> $form_state
- * Current form state.
- */
-function shs_node_form_submit(&$form, &$form_state) {
- $instances = _shs_get_instances($form['#entity_type'], $form['#bundle']);
- foreach ($instances as $field_name => $instance) {
- if (!empty($form_state['values'][$field_name][$form[$field_name]['#language']])) {
- foreach ($form_state['values'][$field_name][$form[$field_name]['#language']] as $key => $value) {
- // Rewrite value of element to match structure of term fields.
- $form_state['values'][$field_name][$form[$field_name]['#language']][$key] = array('tid' => $value);
- }
- }
- }
-}
-
-/**
* Implements hook_field_widget_info().
*/
function shs_field_widget_info() {
@@ -284,16 +253,28 @@ function shs_field_widget_form(&$form, &$form_state, $field, $instance, $langcod
// Create (hidden) element.
$element += array(
'#type' => 'textfield',
- '#default_value' => empty($items[$delta]['tid']) ? 0 : $items[$delta]['tid'],
+ '#default_value' => empty($items[$delta]) ? array('tid' => 0) : $items[$delta],
'#attributes' => array(
'class' => array('shs-enabled'),
),
+ '#value_callback' => 'shs_field_widget_value',
+ '#maxlength' => NULL, // Prevent errors with drupal_strlen().
);
return $element;
}
/**
+ * Value callback for widgets of type "taxonomy_shs".
+ */
+function shs_field_widget_value($element, $input = FALSE, $form_state = array()) {
+ if ($input) {
+ return array('tid' => $input);
+ }
+ return empty($element['#default_value']) ? array('tid' => 0) : $element['#default_value'];
+}
+
+/**
* Implements hook_field_formatter_info().
*/
function shs_field_formatter_info() {