Skip to content

Commit

Permalink
Breadcrumps subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Oct 11, 2023
1 parent 2c7a5e3 commit 885f626
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/lib/EventDispatcher/EventListener/BreadCrumbSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Search\EventDispatcher\EventListener;

use Ibexa\Search\EventDispatcher\Event\PostAutoCompleteSearch;
use Ibexa\Search\Model\Suggestion;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class BreadCrumbSubscriber implements EventSubscriberInterface
{
private \Ibexa\Contracts\Core\Repository\LocationService $locationService;

public function __construct(\Ibexa\Contracts\Core\Repository\LocationService $locationService)
{
$this->locationService = $locationService;
}

public static function getSubscribedEvents(): array
{
return [
PostAutoCompleteSearch::class => 'onPostAutoCompleteSearch',
];
}

public function onPostAutoCompleteSearch(PostAutoCompleteSearch $event): PostAutoCompleteSearch
{
/** @var Suggestion $suggestion */
foreach ($event->getSuggestionCollection() as $suggestion){
foreach ($suggestion->getParentsLocation() as $locationId => $name){
$location = $this->locationService->loadLocation($locationId);
$suggestion->addPath($locationId, $location->contentInfo->name);
}

}

return $event;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function transform(SearchResult $searchResult, ?string $language = null):
$content->getFieldValue('title', $language)->text,
$result->valueObject->contentInfo->getContentType()->identifier,
$result->valueObject->contentInfo->mainLocation->pathString,
$result->valueObject->contentInfo->mainLocation->path
array_flip($result->valueObject->contentInfo->mainLocation->path)
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib/Model/Suggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function getParentsLocation(): array
{
return $this->parentsLocation;
}

public function addPath(int $locationId, string $name)
{
$this->parentsLocation[$locationId] = $name;
}
}

0 comments on commit 885f626

Please sign in to comment.