From 520c2516079ce32206e009b96605bc15df120949 Mon Sep 17 00:00:00 2001 From: William Hearn Date: Wed, 13 Jan 2016 10:35:30 -0500 Subject: [PATCH] Issue #2381549 by sylus: Linkit Title and UUID support. Signed-off-by: William Hearn --- linkit.module | 48 ++++++++++++++++++++++++++++++++++ plugins/export_ui/linkit_profiles.inc | 9 ++++++- plugins/linkit_search/entity.class.php | 46 +++++++++++++++++++++++++++----- 3 files changed, 95 insertions(+), 8 deletions(-) diff --git a/linkit.module b/linkit.module index 78f7704..942b75b 100644 --- a/linkit.module +++ b/linkit.module @@ -26,6 +26,11 @@ define('LINKIT_URL_METHOD_ALIAS', 3); define('LINKIT_BAC_QUERY_KEY', 's'); /** + * Define that paths should be constructed as an uuid. + */ +define('LINKIT_URL_METHOD_UUID', 4); + +/** * Define the minimum number of characters to perform server polling. */ define('LINKIT_CHAR_LIMIT', 3); @@ -1158,3 +1163,46 @@ function linkit_get_insert_plugin_processed_path(LinkitProfile $profile, $uri, $ return $path; } + +/** + * Implements hook_query_TAG_alter. + * + * Alter the query to also search title fields if "title" module is active. + * + * @param QueryAlterableInterface $query + */ +function linkit_query_linkit_entity_title_alter(QueryAlterableInterface $query) { + // Get some contextual data. + $entity_name = $query->getMetaData('linkit_base_entity'); + $search_string = $query->getMetaData('linkit_search_string'); + + $entity_info = entity_get_info($entity_name); + $base_table = $entity_info['base table']; + $identifier = $entity_info['entity keys']['id']; + $label = $entity_info['entity keys']['label']; + $label_field_name = $label . '_field'; + + // Drop the existing search condition. + $exisiting_conditions =& $query->conditions(); + foreach ($exisiting_conditions as $key => $exisiting_condition) { + if (isset($exisiting_condition['operator']) && $exisiting_condition['operator'] === 'LIKE') { + unset($exisiting_conditions[$key]); + } + } + + // Join the title field table. + $query->leftJoin( + 'field_data_' . $label_field_name, + 't', + "(t.deleted = 0 AND t.entity_type = :entity_type AND t.entity_id = $base_table.$identifier)", + array(':entity_type' => $entity_name ) + ); + + // Create a new OR condition set. The "OR" set is needed as some bundles might + // have the title replaced with a field and some not. + $or = db_or(); + $or->condition('t.' . $label_field_name . '_value', '%' . db_like($search_string) . '%', 'LIKE'); + $or->condition("$base_table.$label", '%' . db_like($search_string) . '%', 'LIKE'); + + $query->condition($or); +} diff --git a/plugins/export_ui/linkit_profiles.inc b/plugins/export_ui/linkit_profiles.inc index e49bd07..c1b522f 100644 --- a/plugins/export_ui/linkit_profiles.inc +++ b/plugins/export_ui/linkit_profiles.inc @@ -308,6 +308,13 @@ function _linkit_build_insert_plugin_form_fields(&$form, LinkitProfile $profile)
The paths will be run through the url() function.', array('%example_link' => 'node/123')), ); } + + if (module_exists('uuid')) { + $form['data']['insert_plugin']['url_method']['#options'][LINKIT_URL_METHOD_UUID] = t('UUID paths'); + $form['data']['insert_plugin']['url_method'][LINKIT_URL_METHOD_UUID] = array( + '#description' => t('This will insert the UUID for the result item.'), + ); + } } /** @@ -496,4 +503,4 @@ function linkit_isnumeric_validate($element, &$form_state, $form) { if (!empty($element['#value']) && !is_numeric($element['#value'])) { form_error($element, $element['#title'] . ' should only contains numbers.'); } -} \ No newline at end of file +} diff --git a/plugins/linkit_search/entity.class.php b/plugins/linkit_search/entity.class.php index 853f6f5..530fc63 100644 --- a/plugins/linkit_search/entity.class.php +++ b/plugins/linkit_search/entity.class.php @@ -136,13 +136,26 @@ class LinkitSearchPluginEntity extends LinkitSearchPlugin { $options = array(); // Handle multilingual sites. - if (isset($entity->language) && $entity->language != LANGUAGE_NONE && drupal_multilingual() && language_negotiation_get_any(LOCALE_LANGUAGE_NEGOTIATION_URL)) { - $languages = language_list('enabled'); - // Only use enabled languages. - $languages = $languages[1]; - - if ($languages && isset($languages[$entity->language])) { - $options['language'] = $languages[$entity->language]; + if (drupal_multilingual() && language_negotiation_get_any(LOCALE_LANGUAGE_NEGOTIATION_URL)) { + // Handle field translation with entity_translation. + if (function_exists('entity_translation_enabled') && entity_translation_enabled($this->plugin['entity_type'], $entity)) { + $options['language'] = $GLOBALS['language']; + } + // Handle content translation. + else if (isset($entity->language) && $entity->language != LANGUAGE_NONE) { + $languages = language_list('enabled'); + // Only use enabled languages. + $languages = $languages[1]; + if ($languages && isset($languages[$entity->language])) { + $options['language'] = $languages[$entity->language]; + } + } + } + // Handle uuid based sites. + if (module_exists('uuid') && $this->profile->data['insert_plugin']['url_method'] == LINKIT_URL_METHOD_UUID) { + if (isset($entity->uuid)) { + $path = '[uuid-link:' . $this->plugin['entity_type'] . ':' . $entity->uuid . ']'; + return $path; } } // Process the uri with the insert plugin. @@ -227,6 +240,25 @@ class LinkitSearchPluginEntity extends LinkitSearchPlugin { } } + // If "title" module is enabled and any of the containing bundles use it, + // add a special tag to the query. + if (module_exists('title')) { + $bundles = isset($this->conf['bundles']) ? $this->conf['bundles'] : field_info_bundles($this->plugin['entity_type']); + $title_in_use = FALSE; + foreach ($bundles as $bundle_name => $bundle) { + if (title_field_replacement_enabled($this->plugin['entity_type'], $bundle_name, $this->entity_field_label)) { + $title_in_use = TRUE; + break; + } + } + if ($title_in_use) { + $this->query->addTag('linkit_entity_title'); + $this->query->addMetaData('linkit_base_entity', $this->plugin['entity_type']); + //$this->query->addMetaData('linkit_base_entity_', $this->plugin['entity_type']); + $this->query->addMetaData('linkit_search_string', $search_string); + } + } + // Execute the query. $result = $this->query->execute(); -- 2.3.8 (Apple Git-58)