1

I want to show the intro image of articles in Joomla 3.10 search results (com_search).

In previous Joomla 3 versions, I know that you could do this:

  1. Create template override:

    /templa tes/template_name/html/com_search/search/default_results.php
    
  2. Then get access to the images using this code:

    $images = json_decode($result->images);
    

Now I see in Joomla 3.10 that $result only has these values:

[relevance] => 
[title] => 
[metadesc] => 
[metakey] => 
[created] => 
[language] => 
[catid] => 
[text] => 
[section] => 
[slug] => 
[catslug] => 
[browsernav] => 
[href] => 
[jcfields] => 
[count] => 

Is there any way to get access to article images?

1 Answer 1

1

The search component (com_search) gets its results from search plugin and it returns precomputed data of article's most needed parts.

I can give you just a quick example how you could fetch article but it may affect the overall performance, because you're going to load them one by one.

The first method via the JTable:

$article = JTable::getInstance('Content', 'JTable');
$article->load(array('id'=>$result->slug)); 
$images = json_decode($article->images);

The second method via the JModelLegacy

JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request' => true)); 
$model->setState('params', JApplication::getInstance('site')->getParams()); 
$article = $model->getItem($result->slug);
$images = json_decode($article->images);

In case of JTable you can provide array of slugs and fetch many articles, but you need to parse $results variable in your template override. Mind that the code I've presented has no checks at all, but it builds basic playground to start with.

3
  • Hey, thanks, JTable method works. And any idea, how to get an image from a Contact, instead of Article?
    – bolando
    Oct 17, 2022 at 7:31
  • Same way with minor upgrade. JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_contact/tables'); $table = JTable::getInstance('Contact', 'ContactTable'); $table->load(array('id'=>$result->slug)); var_dump($table->image);
    – Elboyler
    Oct 17, 2022 at 20:29
  • Thanks, it also works!
    – bolando
    Oct 19, 2022 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.