Jump to content

Show cover image on listing module


GJNR

Recommended Posts

Hey forum!

I needed to show the cover image instead of the main image on the listing module, and <?php dd($listing); ?> didn't had anything related to cover image.

Thanks to @Alejandro help, I managed to build my custom listing module with the cover image.

First, you need to add this filter:

Clickfwd\Hook\Filter::add('post_get_listings_listings_module_query', function($listings, $params) {
	$config = S2Object::make('config');
	$Media = new MediaModel;

	$listings = $Media->addMedia(
		$listings,
		'Listing',
		'listing_id',
		[
			'sort'=> $config->media_general_default_order_listing,
			'extension'=>'com_content',
			'controller'=> 'com_content',
			'action'=> 'com_content_view',
		]
	);

	return $listings;
});

and now you can get the cover image info with $listing['Cover'].

In my case, I created a override template for listing_card_layout.thtml, and I changed this line:

$mainMediaThumb = $settings['showThumbnail'] ? $Media->thumb(Sanitize::getVar($listing,'MainMedia'),['listing'=> &$listing,'size'=>'640x640','mode'=>'scale','return_url'=>true,'thumbnailer'=>'url']) : null;

to this:

$mainMediaThumb = $settings['showThumbnail'] ? $Media->thumb(Sanitize::getVar($listing,'Cover'),['listing'=> &$listing,'size'=>'640x640','mode'=>'scale','return_url'=>true,'thumbnailer'=>'url']) : null;

just changing "MainMedia' to 'Cover'.

But with the filter making the cover data available for modules, you can build your own code on the listing module instead of using the $mainMediaThumb, maybe with something like:

$coverPhoto = Sanitize::getVar($listing,'Cover');
$coverImg = $coverPhoto ? $Media->thumb($coverPhoto, ['size'=>'1440x800','mode'=>'scale', 'return_src'=>false,'thumbnailer'=>'api','suppress_size'=>true], ['class' => false]) : false;

*not sure if the code above works, I've got the easy road with MainMedia 🙂

That's all!

Link to comment
×
×
  • Create New...