Skip to content

Commit 42f4e48

Browse files
author
chrisdavenport
committed
Add appropriate fields to search taxonomies
1 parent 4fd1689 commit 42f4e48

File tree

1 file changed

+111
-4
lines changed

1 file changed

+111
-4
lines changed

plugins/system/fields/fields.php

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ public function onContentPrepare($context, $item)
488488
*/
489489
public function onPrepareFinderContent(FinderIndexerResult &$item, $extension = '')
490490
{
491+
$db = JFactory::getDbo();
491492
$context = $extension . '.' . strtolower($item->layout);
492493

493494
// Create a dummy object with the required fields
@@ -509,10 +510,116 @@ public function onPrepareFinderContent(FinderIndexerResult &$item, $extension =
509510
// Add the extra custom fields to the item to be indexed.
510511
foreach ($fields as $field)
511512
{
512-
// Add an instruction to index the field value.
513-
$indexFieldName = 'jfield_' . $field->alias;
514-
$item->addInstruction(FinderIndexer::TEXT_CONTEXT, $indexFieldName);
515-
$item->{$indexFieldName} = $model->getFieldValue($field->id, $context, $item->id);
513+
// Get the raw value(s) of the field.
514+
$values = (array) $model->getFieldValue($field->id, $context, $item->id);
515+
516+
if (empty($values))
517+
{
518+
continue;
519+
}
520+
521+
switch ($field->type)
522+
{
523+
case 'calendar':
524+
case 'editor':
525+
case 'text':
526+
case 'textarea':
527+
case 'url':
528+
// Add an instruction to index the field value as HTML.
529+
$indexFieldName = 'jfield_' . $field->alias;
530+
$item->addInstruction(FinderIndexer::TEXT_CONTEXT, $indexFieldName);
531+
$item->{$indexFieldName} = implode(' ', $values);
532+
break;
533+
534+
case 'checkboxes':
535+
case 'list':
536+
case 'radio':
537+
// Add enumerated fields to search taxonomies.
538+
$options = array();
539+
540+
// Construct a map of possible field name-value pairs.
541+
foreach ($field->fieldparams->get('options', array()) as $option)
542+
{
543+
$options[$option->value] = $option->name;
544+
}
545+
546+
// Add the actual field values to the search taxonomy.
547+
foreach ($values as $value)
548+
{
549+
$item->addTaxonomy($field->title, $options[$value]);
550+
}
551+
552+
break;
553+
554+
case 'colour':
555+
case 'color':
556+
case 'integer':
557+
case 'media':
558+
case 'tel':
559+
case 'timezone':
560+
// Array of simple values to add to the search taxonomy.
561+
foreach ($values as $value)
562+
{
563+
$item->addTaxonomy($field->title, $value);
564+
}
565+
566+
break;
567+
568+
case 'sql':
569+
// Get all the available options.
570+
$options = $db->setQuery($field->fieldparams->get('query'))->loadObjectList('value');
571+
572+
// Nothing to index.
573+
if (empty($options))
574+
{
575+
continue;
576+
}
577+
578+
// Add the actual field values to the search taxonomy.
579+
foreach ($values as $value)
580+
{
581+
if (!empty($options[$value]))
582+
{
583+
$item->addTaxonomy($field->title, $options[$value]->text);
584+
}
585+
}
586+
587+
break;
588+
589+
case 'user':
590+
// Get the names of one or more users from the users table by user id.
591+
$query = $db->getQuery(true)
592+
->select('name')
593+
->from('#__users')
594+
->where('id IN (' . implode(',', $values) . ')');
595+
596+
// Add the actual field values to the search taxonomy.
597+
foreach ($db->setQuery($query)->loadColumn() as $value)
598+
{
599+
$item->addTaxonomy($field->title, $value);
600+
}
601+
602+
break;
603+
604+
case 'usergrouplist':
605+
// Get the names of one or more usergroups by usergroup id.
606+
$query = $db->getQuery(true)
607+
->select('title')
608+
->from('#__usergroups')
609+
->where('id IN (' . implode(',', $values) . ')');
610+
611+
// Add the actual field values to the search taxonomy.
612+
foreach ($db->setQuery($query)->loadColumn() as $value)
613+
{
614+
$item->addTaxonomy($field->title, $value);
615+
}
616+
617+
break;
618+
619+
default:
620+
// Non-searchable fields.
621+
break;
622+
}
516623
}
517624

518625
return true;

0 commit comments

Comments
 (0)