Skip to content

Commit

Permalink
Added Twig extension for to remove highlighter tags, some corrections…
Browse files Browse the repository at this point in the history
… in SalesChannelRepositoryDecorator,
  • Loading branch information
Torsten Freyda committed Feb 24, 2024
1 parent fdb22e2 commit dd4392d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace MuckiSearchPlugin\Core\System\SalesChannel\Entity;

use MuckiSearchPlugin\Search\Content\Category;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\SalesChannel\Listing\Processor\CompositeListingProcessor;
use Shopware\Core\Content\Product\SearchKeyword\ProductSearchBuilderInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
Expand Down Expand Up @@ -66,6 +63,7 @@ public function __construct(
protected ContentCategory $contentCategory
) {
$this->originalSalesChannelRepository = $salesChannelRepository;
$this->entitySearchResult = null;

parent::__construct(
$definition,
Expand All @@ -81,11 +79,13 @@ public function search(Criteria $criteria, SalesChannelContext $salesChannelCont
{
if($this->pluginSettings->isEnabled()) {

if($this->entitySearchResult->getEntities()->count() <= 0) {
$this->pluginSearch($criteria, $salesChannelContext);
if(!$this->entitySearchResult || $this->entitySearchResult->getEntities()->count() <= 0) {
$this->entitySearchResult = $this->pluginSearch($criteria, $salesChannelContext);
} else {
return $this->entitySearchResult;
}

return $this->entitySearchResult;

}

return $this->regularSearch($criteria, $salesChannelContext);
Expand Down Expand Up @@ -163,11 +163,11 @@ private function read(Criteria $criteria, SalesChannelContext $salesChannelConte

private function doSearch(Criteria $criteria, SalesChannelContext $salesChannelContext): IdSearchResult
{
if($this->pluginSettings->isEnabled()) {
if($this->pluginSettings->isEnabled() && !empty($criteria->getQueries())) {
$this->entitySearchResult = $this->pluginSearch($criteria, $salesChannelContext);
}

if($this->entitySearchResult->getEntities()->count() >= 1) {
if($this->entitySearchResult && $this->entitySearchResult->getEntities()->count() >= 1) {

$data = array();
foreach ($this->entitySearchResult->getElements() as $elementKey => $elementItems) {
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,10 @@
<argument type="service" id="MuckiSearchPlugin\Services\IndicesSettings"/>
<argument type="service" id="MuckiSearchPlugin\Services\Settings"/>
</service>

<service id="MuckiSearchPlugin\Twig\Extension\HighlightRemoverExtension" class="MuckiSearchPlugin\Twig\Extension\HighlightRemoverExtension">
<argument type="service" id="MuckiSearchPlugin\Services\Settings"/>
<tag name="twig.extension"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% sw_extends '@Storefront/storefront/component/product/card/box-standard.html.twig' %}

{% block component_product_box_image_link %}
<a href="{{ seoUrl('frontend.detail.page', { 'productId': id }) }}"
title="{{ muwa_removeHighlightMarkers(name) }}"
class="product-image-link is-{{ displayMode }}">
{% block component_product_box_image_link_inner %}
{% if cover.url and cover.isSpatialObject() == false %}
{% set attributes = {
'class': 'product-image is-'~displayMode,
'alt': (cover.translated.alt ?: muwa_removeHighlightMarkers(name)),
'title': (cover.translated.title ?: muwa_removeHighlightMarkers(name)),
'loading': 'lazy'
} %}

{% if displayMode == 'cover' or displayMode == 'contain' %}
{% set attributes = attributes|merge({ 'data-object-fit': displayMode }) %}
{% endif %}

{% block component_product_box_image_thumbnail %}
{{ parent() }}
{% endblock %}
{% else %}
{% block component_product_box_image_placeholder %}
{{ parent() }}
{% endblock %}
{% endif %}
{% endblock %}
</a>
{% endblock %}

{% block component_product_box_name %}
<a href="{{ seoUrl('frontend.detail.page', {'productId': id}) }}"
class="product-name"
title="{{ muwa_removeHighlightMarkers(name) }}">
{{ name|raw }}
</a>
{% endblock %}
34 changes: 34 additions & 0 deletions src/Twig/Extension/HighlightRemoverExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace MuckiSearchPlugin\Twig\Extension;

use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

use MuckiSearchPlugin\Services\Settings as PluginSettings;

#[Package('core')]
class HighlightRemoverExtension extends AbstractExtension
{
public function __construct(
protected PluginSettings $pluginSettings
)
{}

public function getFunctions(): array
{
return [
new TwigFunction('muwa_removeHighlightMarkers', $this->removeHighlightMarkers(...)),
];
}

public function removeHighlightMarkers(string $input): ?string
{
$replaceItems[] = $this->pluginSettings->getSearchRequestSettingsPreTags();
$replaceItems[] = $this->pluginSettings->getSearchRequestSettingsPostTags();

return str_replace($replaceItems, '', $input);
}
}

0 comments on commit dd4392d

Please sign in to comment.