Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.1] Images in the Smart Search results #32450

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c45283a
Add the image object in the Result item
sakiss Feb 17, 2021
28349ba
Add image to content Result
sakiss Feb 17, 2021
7bb1cde
Add a 'show_image' setting, both to the finder's configuration and th…
sakiss Feb 17, 2021
3a31d12
Show image defaults to 0 (hide)
sakiss Feb 17, 2021
e998b40
Add image class setting (as set for the com_content)
sakiss Feb 17, 2021
61a416e
Rename addImage to setImage (we only have 1)
sakiss Feb 17, 2021
9220a51
getImage function
sakiss Feb 17, 2021
9160bc8
print the image in the results layout
sakiss Feb 17, 2021
c5a569f
code style
sakiss Feb 17, 2021
caffe18
Update administrator/components/com_finder/config.xml
sakiss Feb 17, 2021
bd0bb74
Update administrator/components/com_finder/config.xml
sakiss Feb 17, 2021
005683f
Update administrator/components/com_finder/src/Indexer/Result.php
sakiss Feb 17, 2021
46527b0
Update components/com_finder/tmpl/search/default.xml
sakiss Feb 17, 2021
de578a4
Update components/com_finder/tmpl/search/default_result.php
sakiss Feb 17, 2021
17e7970
Fix language constants order
sakiss Feb 17, 2021
60df430
Revert "Fix language constants order" as it ovewrites github commits
sakiss Feb 17, 2021
ade0566
__DEPLOY_VERSION__
sakiss Feb 17, 2021
a499732
alphabetic order to lang constants
sakiss Feb 17, 2021
f50a8a8
Update components/com_finder/tmpl/search/default_result.php
sakiss Feb 17, 2021
4faff60
Update components/com_finder/tmpl/search/default_result.php
sakiss Feb 17, 2021
f3f8b70
CS
sakiss Feb 17, 2021
f5cf2a9
Revert "CS"
sakiss Feb 17, 2021
1fa0e3d
cs hell
sakiss Feb 17, 2021
e6d833e
Tabs
sakiss Feb 17, 2021
fcb3817
alt attribute
sakiss Feb 17, 2021
567096d
Remove description from class field
sakiss Feb 18, 2021
17042f6
initialize $extraAttr
sakiss Feb 18, 2021
1949eaa
Update administrator/components/com_finder/config.xml
sakiss Feb 18, 2021
20dc24e
docBlocks
sakiss Feb 18, 2021
f0a72de
Image Url and Image Alt as public scalar properties
sakiss Sep 1, 2021
896e612
Set image url and image alt directly, in finder plugin
sakiss Sep 1, 2021
115b924
Access image url and image alt directly in the layout
sakiss Sep 1, 2021
66be9c1
Alt & url strings docblock
sakiss Sep 1, 2021
78f8a69
setElement/getElement
sakiss Sep 1, 2021
c2e4b7e
htmlspecicalcacters for alt
sakiss Sep 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions administrator/components/com_finder/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@
showon="show_description:1"
/>

<field
name="show_image"
type="radio"
label="COM_FINDER_CONFIG_SHOW_IMAGE_LABEL"
layout="joomla.form.field.radio.switcher"
default="0"
>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>

<field
name="image_class"
type="text"
label="COM_FINDER_CONFIG_IMAGE_CLASS_LABEL"
validate="CssIdentifier"
showon="show_image:1"
/>

<field
name="show_date"
type="radio"
Expand Down
2 changes: 2 additions & 0 deletions administrator/language/en-GB/com_finder.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ COM_FINDER_CONFIG_FILTER_COMMONWORDS_LABEL="Filter Common Words"
COM_FINDER_CONFIG_FILTER_NUMERICS_LABEL="Filter Numeric Terms"
COM_FINDER_CONFIG_GATHER_SEARCH_STATISTICS_LABEL="Gather Search Statistics"
COM_FINDER_CONFIG_HILIGHT_CONTENT_SEARCH_TERMS_LABEL="Highlight Search Terms"
COM_FINDER_CONFIG_IMAGE_CLASS_LABEL="Image Class"
COM_FINDER_CONFIG_LANGUAGE_DEFAULT_DEFAULT_LANGUAGE="Default Site Language"
COM_FINDER_CONFIG_LANGUAGE_DEFAULT_DESC="Set the language to be used for non-multilingual sites or content marked as \"All\"."
COM_FINDER_CONFIG_LANGUAGE_DEFAULT_LABEL="Default Language"
Expand All @@ -34,6 +35,7 @@ COM_FINDER_CONFIG_SHOW_DATE_FILTERS_LABEL="Date Filters"
COM_FINDER_CONFIG_SHOW_DATE_LABEL="Result Date"
COM_FINDER_CONFIG_SHOW_DESCRIPTION_LABEL="Result Description"
COM_FINDER_CONFIG_SHOW_EXPLAINED_QUERY_LABEL="Query Explanation"
COM_FINDER_CONFIG_SHOW_IMAGE_LABEL="Result Image"
COM_FINDER_CONFIG_SHOW_SUGGESTED_QUERY_LABEL="Did You Mean"
COM_FINDER_CONFIG_SHOW_TAXONOMY_LABEL="Result Taxonomy"
COM_FINDER_CONFIG_SHOW_URL_LABEL="Result URL"
Expand Down
12 changes: 12 additions & 0 deletions components/com_finder/tmpl/search/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@
size="5"
useglobal="true"
/>
<field
name="show_image"
type="list"
label="COM_FINDER_CONFIG_SHOW_IMAGE_LABEL"
default=""
useglobal="true"
class="form-select-color-state"
validate="options"
>
<option value="1">JSHOW</option>
<option value="0">JHIDE</option>
</field>
<field
name="show_date"
type="list"
Expand Down
25 changes: 25 additions & 0 deletions components/com_finder/tmpl/search/default_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Joomla\Component\Finder\Administrator\Indexer\Helper;
use Joomla\Component\Finder\Administrator\Indexer\Taxonomy;
use Joomla\String\StringHelper;
use Joomla\Utilities\ArrayHelper;

$user = Factory::getUser();

Expand Down Expand Up @@ -48,6 +49,22 @@
$description = HTMLHelper::_('string.truncate', StringHelper::substr($full_description, $start), $desc_length, true);
}

$showImage = $this->params->get('show_image', 0);

if ($showImage && !empty($this->result->getElement('imageUrl')))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do not need use empty check, just do:

if ($showImage && $this->result->imageUrl)

Remember we have a __get

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->result->imageUrl and $this->result->imageAlt no longer exist, based on the last commit.

The imageUrl and imageAlt are now part of the Joomla\Component\Finder\Administrator\Indexer\Result::$elements
which can only be accessed through the __get() or the getElement() functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, do you know what __get()/__set() doing? :)
Please check how the code access to $item->title and other values.

$this->result->imageUrl and $this->result->imageAlt no longer exist

Correct, they not need to exist. That is magic of magic methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test your suggestions before writing them down.
$this->result->imageUrl and $this->result->imageAlt are null since they do not exist.

If you want to provide a working example based on that suggestion, please commit.

Copy link
Member

@Fedik Fedik Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is null only when it empty,
if you set a value while indexing then it will be not null here

{
$imageclass = $this->params->get('image_class', '');
$image = HTMLHelper::cleanImageURL($this->result->getElement('imageUrl'));
sakiss marked this conversation as resolved.
Show resolved Hide resolved
$extraAttr = '';

// Set lazyloading only for images which have width and height attributes
if ((isset($image->attributes['width']) && (int) $image->attributes['width'] > 0)
&& (isset($image->attributes['height']) && (int) $image->attributes['height'] > 0))
{
$extraAttr = ArrayHelper::toString($image->attributes) . ' loading="lazy"';
}
}

$icon = '';
if (!empty($this->result->mime)) :
$icon = '<span class="icon-file-' . $this->result->mime . '" aria-hidden="true"></span> ';
Expand All @@ -72,6 +89,14 @@
<?php echo $this->result->title; ?>
<?php endif; ?>
</p>
<?php if ($showImage && isset($image)) : ?>
<figure class="<?php echo htmlspecialchars($imageclass, ENT_COMPAT, 'UTF-8'); ?> result__image">
<img src="<?php echo htmlspecialchars($image->url, ENT_COMPAT, 'UTF-8'); ?>"
alt="<?php echo $this->result->getElement('imageAlt'); ?>"
sakiss marked this conversation as resolved.
Show resolved Hide resolved
<?php echo $extraAttr; ?>
/>
</figure>
<?php endif; ?>
<?php if ($show_description && $description !== '') : ?>
<p class="result__description">
<?php if ($this->result->start_date && $this->params->get('show_date', 1)) : ?>
Expand Down
9 changes: 9 additions & 0 deletions plugins/finder/content/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ protected function index(Result $item)
$item->title = $title;
}

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

// Add the image.
if (!empty($images) && $images->image_intro)
{
$item->setElement('imageUrl', $images->image_intro);
$item->setElement('imageAlt', $images->image_intro_alt ?? '');
Copy link
Member

@Fedik Fedik Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look how $item->title is set, and do the same:

$item->imageUrl = $images->image_intro;
$item->imageAlt = $images->image_intro_alt ?? '';

Copy link
Contributor Author

@sakiss sakiss Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly how it was before pointing me out to use the getters/setters. Probably you are commenting without checking my commits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, here it was correct before,

before pointing me out to use the getters/setters

It was about Result class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i cannot go back and forth. I am closing that as resolved since it works and it's indeed much better than setting public properties. For any further change please commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, title was a bad example sorry, however please do:

$item->imageUrl = $images->image_intro;
$item->imageAlt = $images->image_intro_alt ?? '';

No need to create any properties, just do this.

Copy link
Contributor Author

@sakiss sakiss Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously you did not bother to examine the finder's code, neither to test what you are writing.
Since you are wasting my time and i have no appetite to talk with toddlers, i am closing that.

}

// Add the meta author.
$item->metaauthor = $item->metadata->get('author');

Expand Down