Skip to content

Commit f001c84

Browse files
authored
[4.2] Smart Search: Index custom fields (#36747)
Finder: Add custom fields to search index
1 parent 3eba8c7 commit f001c84

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

administrator/components/com_fields/forms/field.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,20 @@
328328
<option value="0">JNO</option>
329329
</field>
330330
</fieldset>
331+
<fieldset name="smartsearchoptions" label="COM_FIELDS_FIELD_SMARTSEARCHOPTIONS_HEADING">
332+
<field
333+
name="searchindex"
334+
type="list"
335+
label="COM_FIELDS_FIELD_SEARCHINDEX_LABEL"
336+
default="0"
337+
validate="options"
338+
>
339+
<option value="0">COM_FIELDS_FIELD_SEARCHINDEX_DONT</option>
340+
<option value="1">COM_FIELDS_FIELD_SEARCHINDEX_SEARCHABLE</option>
341+
<option value="2">COM_FIELDS_FIELD_SEARCHINDEX_TAXONOMY</option>
342+
<option value="3">COM_FIELDS_FIELD_SEARCHINDEX_BOTH</option>
343+
</field>
344+
</fieldset>
331345
</fieldset>
332346
</fields>
333347
</form>

administrator/components/com_fields/tmpl/field/edit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<legend><?php echo Text::_('JGLOBAL_FIELDSET_PUBLISHING'); ?></legend>
7878
<div>
7979
<?php echo LayoutHelper::render('joomla.edit.publishingdata', $this); ?>
80+
<?php echo $this->form->renderField('searchindexing'); ?>
8081
</div>
8182
</fieldset>
8283
<?php echo HTMLHelper::_('uitab.endTab'); ?>

administrator/components/com_finder/src/Indexer/Helper.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Joomla\CMS\Language\Multilanguage;
1818
use Joomla\CMS\Plugin\PluginHelper;
1919
use Joomla\CMS\Table\Table;
20+
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
2021
use Joomla\Registry\Registry;
2122
use Joomla\String\StringHelper;
2223

@@ -432,6 +433,43 @@ public static function getContentExtras(Result $item)
432433
return true;
433434
}
434435

436+
/**
437+
* Add custom fields for the item to the Result object
438+
*
439+
* @param Result $item Result object to add the custom fields to
440+
* @param string $context Context of the item in the custom fields
441+
*
442+
* @return void
443+
*
444+
* @since __DEPLOY_VERSION__
445+
*/
446+
public static function addCustomFields(Result $item, $context)
447+
{
448+
$obj = new \stdClass;
449+
$obj->id = $item->id;
450+
451+
$fields = FieldsHelper::getFields($context, $obj, true);
452+
453+
foreach ($fields as $field)
454+
{
455+
$searchindex = $field->params->get('searchindex', 0);
456+
457+
// We want to add this field to the search index
458+
if ($searchindex == 1 || $searchindex == 3)
459+
{
460+
$name = 'jsfield_' . $field->name;
461+
$item->$name = $field->value;
462+
$item->addInstruction(Indexer::META_CONTEXT, $name);
463+
}
464+
465+
// We want to add this field as a taxonomy
466+
if (($searchindex == 2 || $searchindex == 3) && $field->value)
467+
{
468+
$item->addTaxonomy($field->title, $field->value, $field->state, $field->access, $field->language);
469+
}
470+
}
471+
}
472+
435473
/**
436474
* Method to process content text using the onContentPrepare event trigger.
437475
*

administrator/language/en-GB/com_fields.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ COM_FIELDS_FIELD_RENDER_CLASS_LABEL="Display Class"
5757
COM_FIELDS_FIELD_RENDEROPTIONS_HEADING="Display Options"
5858
COM_FIELDS_FIELD_REQUIRED_LABEL="Required"
5959
COM_FIELDS_FIELD_SAVE_SUCCESS="Field saved"
60+
COM_FIELDS_FIELD_SEARCHINDEX_BOTH="Make searchable and add as taxonomy"
61+
COM_FIELDS_FIELD_SEARCHINDEX_DONT="Don't make searchable"
62+
COM_FIELDS_FIELD_SEARCHINDEX_LABEL="Searchindex"
63+
COM_FIELDS_FIELD_SEARCHINDEX_SEARCHABLE="Make searchable"
64+
COM_FIELDS_FIELD_SEARCHINDEX_TAXONOMY="Add as taxonomy"
6065
COM_FIELDS_FIELD_SHOWLABEL_DESC="Show or Hide the label when the field displays."
6166
COM_FIELDS_FIELD_SHOWLABEL_LABEL="Label"
67+
COM_FIELDS_FIELD_SMARTSEARCHOPTIONS_HEADING="Smart Search"
6268
COM_FIELDS_FIELD_SUFFIX_LABEL="Suffix"
6369
COM_FIELDS_FIELD_TYPE_LABEL="Type"
6470
COM_FIELDS_FIELD_USE_GLOBAL="Use settings from Plugin"

plugins/finder/contacts/contacts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ protected function index(Result $item)
387387

388388
// Get content extras.
389389
Helper::getContentExtras($item);
390+
Helper::addCustomFields($item, 'com_contact.contact');
390391

391392
// Index the item.
392393
$this->indexer->index($item);

plugins/finder/content/content.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ protected function index(Result $item)
343343

344344
// Get content extras.
345345
Helper::getContentExtras($item);
346+
Helper::addCustomFields($item, 'com_content.article');
346347

347348
// Index the item.
348349
$this->indexer->index($item);

0 commit comments

Comments
 (0)