From b06c2a0b339528d41d9871cc70be2267dcc21988 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Wed, 19 Jan 2022 21:47:56 +0100 Subject: [PATCH 1/6] Finder: Add custom fields to search index --- .../components/com_fields/forms/field.xml | 14 +++++++ .../com_finder/src/Indexer/Helper.php | 40 ++++++++++++++++++- administrator/language/en-GB/com_fields.ini | 7 ++++ plugins/finder/contacts/contacts.php | 1 + plugins/finder/content/content.php | 1 + 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/administrator/components/com_fields/forms/field.xml b/administrator/components/com_fields/forms/field.xml index 113d027571c76..160db410d0b4d 100644 --- a/administrator/components/com_fields/forms/field.xml +++ b/administrator/components/com_fields/forms/field.xml @@ -342,6 +342,20 @@ +
+ + + + + + +
diff --git a/administrator/components/com_finder/src/Indexer/Helper.php b/administrator/components/com_finder/src/Indexer/Helper.php index fc3f13ab97794..2858c78d85a3e 100644 --- a/administrator/components/com_finder/src/Indexer/Helper.php +++ b/administrator/components/com_finder/src/Indexer/Helper.php @@ -17,6 +17,7 @@ use Joomla\CMS\Language\Multilanguage; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Table\Table; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; use Joomla\Registry\Registry; use Joomla\String\StringHelper; @@ -112,7 +113,7 @@ public static function tokenize($input, $lang, $phrase = false) $tokens = array(); $terms = $language->tokenise($input); - // @todo: array_filter removes any number 0's from the terms. Not sure this is entirely intended + // TODO: array_filter removes any number 0's from the terms. Not sure this is entirely intended $terms = array_filter($terms); $terms = array_values($terms); @@ -432,6 +433,43 @@ public static function getContentExtras(Result $item) return true; } + /** + * Add custom fields for the item to the Result object + * + * @param Result $item Result object to add the custom fields to + * @param string $context Context of the item in the custom fields + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public static function addCustomFields(Result $item, $context) + { + $obj = new \stdClass; + $obj->id = $item->id; + + $fields = FieldsHelper::getFields($context, $obj, true); + + foreach ($fields as $field) + { + $searchindex = $field->params->get('searchindex', 0); + + // We want to add this field to the search index + if ($searchindex == 1 || $searchindex == 3) + { + $name = 'jsfield_' . $field->name; + $item->$name = $field->value; + $item->addInstruction(Indexer::META_CONTEXT, $name); + } + + // We want to add this field as a taxonomy + if (($searchindex == 1 || $searchindex == 3) && $field->value) + { + $item->addTaxonomy($field->title, $field->value, $field->state, $field->access, $field->language); + } + } + } + /** * Method to process content text using the onContentPrepare event trigger. * diff --git a/administrator/language/en-GB/com_fields.ini b/administrator/language/en-GB/com_fields.ini index 1d65e31a4445c..a07107ef59f8d 100644 --- a/administrator/language/en-GB/com_fields.ini +++ b/administrator/language/en-GB/com_fields.ini @@ -57,8 +57,15 @@ COM_FIELDS_FIELD_RENDER_CLASS_LABEL="Render Class" COM_FIELDS_FIELD_RENDEROPTIONS_HEADING="Render Options" COM_FIELDS_FIELD_REQUIRED_LABEL="Required" COM_FIELDS_FIELD_SAVE_SUCCESS="Field saved" +COM_FIELDS_FIELD_SEARCHINDEX_BOTH="Make searchable and add as taxonomy" +COM_FIELDS_FIELD_SEARCHINDEX_DONT="Don't make searchable" +COM_FIELDS_FIELD_SEARCHINDEX_LABEL="Searchindex" +COM_FIELDS_FIELD_SEARCHINDEX_SEARCHABLE="Make searchable" +COM_FIELDS_FIELD_SEARCHINDEX_TAXONOMY="Add as taxonomy" + COM_FIELDS_FIELD_SHOWLABEL_DESC="Show or Hide the label when the field renders." COM_FIELDS_FIELD_SHOWLABEL_LABEL="Label" +COM_FIELDS_FIELD_SMARTSEARCHOPTIONS_HEADING="Smart Search" COM_FIELDS_FIELD_SUFFIX_LABEL="Suffix" COM_FIELDS_FIELD_TYPE_LABEL="Type" COM_FIELDS_FIELD_USE_GLOBAL="Use settings from Plugin" diff --git a/plugins/finder/contacts/contacts.php b/plugins/finder/contacts/contacts.php index 45b587ccdbe1f..b916ebc134584 100644 --- a/plugins/finder/contacts/contacts.php +++ b/plugins/finder/contacts/contacts.php @@ -387,6 +387,7 @@ protected function index(Result $item) // Get content extras. Helper::getContentExtras($item); + Helper::addCustomFields($item, 'com_contact.contact'); // Index the item. $this->indexer->index($item); diff --git a/plugins/finder/content/content.php b/plugins/finder/content/content.php index dd18a3b4818ba..a3d1f707b2a22 100644 --- a/plugins/finder/content/content.php +++ b/plugins/finder/content/content.php @@ -343,6 +343,7 @@ protected function index(Result $item) // Get content extras. Helper::getContentExtras($item); + Helper::addCustomFields($item, 'com_content.article'); // Index the item. $this->indexer->index($item); From fc3ad5a591ad8dd0bc6093d113936c1c5b8c0c58 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 20 Jan 2022 08:53:31 +0100 Subject: [PATCH 2/6] Adding field output to field edit screen --- administrator/components/com_fields/tmpl/field/edit.php | 1 + 1 file changed, 1 insertion(+) diff --git a/administrator/components/com_fields/tmpl/field/edit.php b/administrator/components/com_fields/tmpl/field/edit.php index a357d89c7928b..1a6f95a144c16 100644 --- a/administrator/components/com_fields/tmpl/field/edit.php +++ b/administrator/components/com_fields/tmpl/field/edit.php @@ -77,6 +77,7 @@
+ form->renderField('searchindexing'); ?>
From d35d89142528c878acf7945372b4a45528071eda Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 20 Jan 2022 09:12:29 +0100 Subject: [PATCH 3/6] Update administrator/components/com_fields/forms/field.xml Co-authored-by: Brian Teeman --- administrator/components/com_fields/forms/field.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/administrator/components/com_fields/forms/field.xml b/administrator/components/com_fields/forms/field.xml index be8b3ee94ee5b..92d8952cf7f98 100644 --- a/administrator/components/com_fields/forms/field.xml +++ b/administrator/components/com_fields/forms/field.xml @@ -329,12 +329,12 @@
+ name="searchindex" + type="list" + label="COM_FIELDS_FIELD_SEARCHINDEX_LABEL" + default="0" + validate="options" + > From 9122eba500249faa508293b8b35dc8bb2fb9d558 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 20 Jan 2022 09:13:01 +0100 Subject: [PATCH 4/6] Reverting todo --- administrator/components/com_finder/src/Indexer/Helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_finder/src/Indexer/Helper.php b/administrator/components/com_finder/src/Indexer/Helper.php index 2858c78d85a3e..27340301f8dbb 100644 --- a/administrator/components/com_finder/src/Indexer/Helper.php +++ b/administrator/components/com_finder/src/Indexer/Helper.php @@ -113,7 +113,7 @@ public static function tokenize($input, $lang, $phrase = false) $tokens = array(); $terms = $language->tokenise($input); - // TODO: array_filter removes any number 0's from the terms. Not sure this is entirely intended + // @todo: array_filter removes any number 0's from the terms. Not sure this is entirely intended $terms = array_filter($terms); $terms = array_values($terms); From 550ca97ef118880e2e930920a915e9abb3e558be Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 11 Feb 2022 09:35:43 +0100 Subject: [PATCH 5/6] Update administrator/language/en-GB/com_fields.ini Co-authored-by: Quy --- administrator/language/en-GB/com_fields.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/administrator/language/en-GB/com_fields.ini b/administrator/language/en-GB/com_fields.ini index a07107ef59f8d..5125e305b0c08 100644 --- a/administrator/language/en-GB/com_fields.ini +++ b/administrator/language/en-GB/com_fields.ini @@ -62,7 +62,6 @@ COM_FIELDS_FIELD_SEARCHINDEX_DONT="Don't make searchable" COM_FIELDS_FIELD_SEARCHINDEX_LABEL="Searchindex" COM_FIELDS_FIELD_SEARCHINDEX_SEARCHABLE="Make searchable" COM_FIELDS_FIELD_SEARCHINDEX_TAXONOMY="Add as taxonomy" - COM_FIELDS_FIELD_SHOWLABEL_DESC="Show or Hide the label when the field renders." COM_FIELDS_FIELD_SHOWLABEL_LABEL="Label" COM_FIELDS_FIELD_SMARTSEARCHOPTIONS_HEADING="Smart Search" From 92b39ea937340d40f0d84030af70ce18158eb61e Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Tue, 17 May 2022 12:12:17 +0200 Subject: [PATCH 6/6] Fix option selection --- administrator/components/com_finder/src/Indexer/Helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_finder/src/Indexer/Helper.php b/administrator/components/com_finder/src/Indexer/Helper.php index 27340301f8dbb..8539c76707ce7 100644 --- a/administrator/components/com_finder/src/Indexer/Helper.php +++ b/administrator/components/com_finder/src/Indexer/Helper.php @@ -463,7 +463,7 @@ public static function addCustomFields(Result $item, $context) } // We want to add this field as a taxonomy - if (($searchindex == 1 || $searchindex == 3) && $field->value) + if (($searchindex == 2 || $searchindex == 3) && $field->value) { $item->addTaxonomy($field->title, $field->value, $field->state, $field->access, $field->language); }