From 0279b4ac298483712bdfbdd9c329862c230c84a0 Mon Sep 17 00:00:00 2001 From: that0n3guy Date: Thu, 29 Sep 2011 13:14:29 -0500 Subject: [PATCH] added entityreference compatability --- acdx_entityreference/acdx_entityreference.info | 16 +++ acdx_entityreference/acdx_entityreference.module | 128 ++++++++++++++++++++++ 2 files changed, 144 insertions(+), 0 deletions(-) create mode 100644 acdx_entityreference/acdx_entityreference.info create mode 100644 acdx_entityreference/acdx_entityreference.module diff --git a/acdx_entityreference/acdx_entityreference.info b/acdx_entityreference/acdx_entityreference.info new file mode 100644 index 0000000..c8a82bf --- /dev/null +++ b/acdx_entityreference/acdx_entityreference.info @@ -0,0 +1,16 @@ +; $Id$ +name = Autocomplete Deluxe Entity References Widget +description = Autocomplete Deluxe widget implementation for entity references. +package = User interface +project = acdx_references +version = VERSION +core = 7.x +files[] = acdx_entityreference.module +dependencies[] = autocomplete_deluxe +dependencies[] = entityreference +; Information added by drupal.org packaging script on 2011-08-11 +version = "7.x-1.0-beta3" +core = "7.x" +project = "acdx_references" +datestamp = "1313055415" + diff --git a/acdx_entityreference/acdx_entityreference.module b/acdx_entityreference/acdx_entityreference.module new file mode 100644 index 0000000..46fac99 --- /dev/null +++ b/acdx_entityreference/acdx_entityreference.module @@ -0,0 +1,128 @@ + t('Autocomplete Deluxe'), + 'description' => t('An autocomplete text field - with deluxe behavior.'), + 'field types' => array('entityreference'), + 'settings' => array( + 'match_operator' => 'CONTAINS', + 'size' => 60, + // We don't have a default here, because it's not the same between + // the two widgets, and the Field API doesn't update default + // settings when the widget changes. + 'path' => '', + ), + ); + + return $widgets; +} + +/** + * Implements hook_field_widget_info_alter(). + */ +function acdx_entityreference_field_widget_info_alter(&$info) { + $info['options_select']['field types'][] = 'entityreference'; + $info['options_buttons']['field types'][] = 'entityreference'; +} + + +/** + * Implements hook_field_widget_settings_form(). + */ +function acdx_entityreference_field_widget_settings_form($field, $instance) { + $widget = $instance['widget']; + $settings = $widget['settings'] + field_info_widget_settings($widget['type']); + + $form = array(); + + if ($widget['type'] == 'entityreference_autocomplete_deluxe') { + $form['match_operator'] = array( + '#type' => 'select', + '#title' => t('Autocomplete matching'), + '#default_value' => $settings['match_operator'], + '#options' => array( + 'STARTS_WITH' => t('Starts with'), + 'CONTAINS' => t('Contains'), + ), + '#description' => t('Select the method used to collect autocomplete suggestions. Note that Contains can cause performance issues on sites with thousands of nodes.'), + ); + $form['size'] = array( + '#type' => 'textfield', + '#title' => t('Size of textfield'), + '#default_value' => $settings['size'], + '#element_validate' => array('_element_validate_integer_positive'), + '#required' => TRUE, + ); + } + + return $form; +} + + +/** + * Implements hook_field_widget_form(). + */ +function acdx_entityreference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + $handler = entityreference_get_handler($field); + if ($instance['widget']['type'] == 'entityreference_autocomplete_deluxe') { + // We let the Field API handles multiple values for us, only take + // care of the one matching our delta. + if (isset($items[$delta])) { + $items = array($items[$delta]); + } + else { + $items = array(); + } + + $entity_ids = array(); + $entity_labels = array(); + + // Build an array of entities ID. + foreach ($items as $item) { + $entity_ids[] = $item['target_id']; + } + + // Load those entities and loop through them to extract their labels. + $entities = entity_load($field['settings']['target_type'], $entity_ids); + + foreach ($entities as $entity_id => $entity) { + $label = $handler->getLabel($entity); + $key = "$label ($entity_id)"; + // Labels containing commas or quotes must be wrapped in quotes. + if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { + $key = '"' . str_replace('"', '""', $key) . '"'; + } + $entity_labels[] = $key; + } + if ($instance['widget']['type'] == 'entityreference_autocomplete_deluxe') { + $path = !empty($instance['widget']['settings']['path']) ? $instance['widget']['settings']['path'] : 'entityreference/autocomplete/single'; + $element += array( + '#type' => 'autocomplete_deluxe', + '#maxlength' => 1024, + '#default_value' => implode(', ', $entity_labels), + '#autocomplete_deluxe_path' => url($path . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'], array('absolute' => TRUE)), + '#size' => $instance['widget']['settings']['size'], + '#element_validate' => array('_entityreference_autocomplete_validate'), + ); + return array('target_id' => $element); + } + } +} + +function _acdx_entityreference_autocomplete_validate($element, &$form_state, $form) { + // If a value was entered into the autocomplete... + $value = ''; + if (!empty($element['#value'])) { + // Take "label (entity id)', match the id from parenthesis. + if (preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) { + $value = $matches[1]; + } + } + // Update the value of this element so the field can validate the product IDs. + form_set_value($element, $value, $form_state); +} \ No newline at end of file -- 1.7.0.4