summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Hutterer2013-01-21 11:58:45 (GMT)
committer Matthias Hutterer2013-01-21 11:58:45 (GMT)
commit906d77dbd1009bce1d16bf46438c31f6198fdc9c (patch)
tree70e091a38d9b6c1c0ee6f1f4660384e7e1c54839
parentf3ad6dc33629ec33db58aaee678fab26967349bb (diff)
Added option for selects lists with opt groups7.x-1.0-beta2
-rw-r--r--content_taxonomy.module73
1 files changed, 69 insertions, 4 deletions
diff --git a/content_taxonomy.module b/content_taxonomy.module
index 03b5e16..2f571ac 100644
--- a/content_taxonomy.module
+++ b/content_taxonomy.module
@@ -7,7 +7,7 @@ function content_taxonomy_form_field_ui_field_edit_form_alter(&$form, &$form_sta
$field = $form['#field'];
$instance = $form['#instance'];
- // Add parent selctor to term reference fields,
+ // Add parent selector to term reference fields,
// except to the autocomplete widget, as it ignores the parent setting.
if ($field['type'] == 'taxonomy_term_reference'
&& !($instance['widget']['type'] == 'taxonomy_autocomplete' || $instance['widget']['type'] == 'autocomplete_deluxe_taxonomy')) {
@@ -35,10 +35,19 @@ function content_taxonomy_form_field_ui_field_edit_form_alter(&$form, &$form_sta
'#description' => t('Set the depth of the tree. Leave empty to load all terms.'),
'#element_validate' => array('_element_validate_integer_positive'),
);
- }
+ }
}
-}
+ // Add opt group setting.
+ if ($field['type'] == 'taxonomy_term_reference' && $instance['widget']['type'] == 'options_select') {
+ $form['instance']['widget']['settings']['content_taxonomy_opt_groups'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Render parent terms as opt-groups'),
+ '#default_value' => isset($instance['widget']['settings']['content_taxonomy_opt_groups']) ? $instance['widget']['settings']['content_taxonomy_opt_groups'] : FALSE,
+ '#description' => t('This option only works if you have a 2-level hierarchy in your vocabulary. Then the parents in the first level get opt-groups and the child terms will be selectable.'),
+ );
+ }
+}
/**
* Implements hook_field_info_alter().
@@ -75,4 +84,60 @@ function content_taxonomy_allowed_values($field) {
}
}
return $options;
-} \ No newline at end of file
+}
+
+/**
+ * Implements hook_field_widget_info_alter().
+ */
+function content_taxonomy_field_widget_info_alter(&$info) {
+ if (isset($info['options_select']['settings'])) {
+ $info['options_select']['settings'] += array(
+ 'content_taxonomy_opt_groups' => FALSE,
+ );
+ }
+}
+
+/**
+ * Implements hook_field_widget_form_alter().
+ */
+function content_taxonomy_field_widget_form_alter(&$element, &$form_state, $context) {
+ $field = $context['field'];
+ $instance = $context['instance'];
+
+ if (!empty($instance['widget']['settings']['content_taxonomy_opt_groups'])) {
+ $options = content_taxonomy_allowed_values_opt_groups($field);
+ if (isset($element['#options']['_none'])) {
+ $options = array('_none' => $element['#options']['_none']) + $options;
+ }
+ $element['#options'] = $options;
+ }
+}
+
+/**
+ * Helper function for generating opt groups.
+ *
+ * Similar to content_taxonomy_allowed_values(), but unfortunately we cannot
+ * directly change content_taxonomy_allowed_values() as it only has the field
+ * variable and opt groups are settings on the instance level. Still, this is
+ * not a big performance issue, as taxonomy_get_tree statically caches some
+ * data.
+ */
+function content_taxonomy_allowed_values_opt_groups($field) {
+ $options = array();
+ foreach ($field['settings']['allowed_values'] as $tree) {
+ if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
+ if ($terms = taxonomy_get_tree($vocabulary->vid, 0, 2)) {
+ $current_group_term = NULL;
+ foreach ($terms as $term) {
+ if ($term->depth == 0) {
+ $current_group_term = $term;
+ }
+ elseif ($term->depth == 1 && !is_null($current_group_term)) {
+ $options[$current_group_term->name][$term->tid] = $term->name;
+ }
+ }
+ }
+ }
+ }
+ return $options;
+}