Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Synolia\Bundle\FavoriteBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecord;
use Oro\Bundle\DataGridBundle\Event\BuildBefore;
use Oro\Bundle\DataGridBundle\Extension\Formatter\Property\PropertyInterface;
use Oro\Bundle\OrganizationBundle\Entity\Organization;
use Oro\Bundle\SearchBundle\Datagrid\Event\SearchResultAfter;
use Oro\Bundle\SecurityBundle\Authentication\TokenAccessor;
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
use Symfony\Bundle\SecurityBundle\Security;
use Synolia\Bundle\FavoriteBundle\Entity\Favorite;
use Synolia\Bundle\FavoriteBundle\Entity\Repository\FavoriteRepository;

Expand All @@ -19,7 +21,7 @@ class FrontendProductFavoriteDatagridListener
public function __construct(
private readonly EntityManager $entityManager,
private readonly AclHelper $aclHelper,
private readonly Security $security
private readonly TokenAccessor $tokenAccessor
) {
}

Expand All @@ -40,8 +42,11 @@ public function onBuildBefore(BuildBefore $event): void

public function onResultAfter(SearchResultAfter $event): void
{
$user = $this->tokenAccessor->getUser();
$organization = $this->tokenAccessor->getOrganization();
$records = $event->getRecords();
if (0 === \count($records)) {

if (!$user instanceof CustomerUser || !$organization instanceof Organization || 0 === \count($records)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace Synolia\Bundle\FavoriteBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\OrganizationBundle\Entity\Organization;
use Oro\Bundle\ProductBundle\Event\BuildResultProductListEvent;
use Oro\Bundle\SecurityBundle\Authentication\TokenAccessor;
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
use Synolia\Bundle\FavoriteBundle\Entity\Favorite;
use Synolia\Bundle\FavoriteBundle\Entity\Repository\FavoriteRepository;
Expand All @@ -14,15 +17,18 @@ class ProductListAddFavoriteEventListener
{
public function __construct(
private readonly EntityManager $entityManager,
private readonly AclHelper $aclHelper
private readonly AclHelper $aclHelper,
private readonly TokenAccessor $tokenAccessor
) {
}

public function onBuildResult(BuildResultProductListEvent $event): void
{
$user = $this->tokenAccessor->getUser();
$organization = $this->tokenAccessor->getOrganization();
$records = $event->getProductData();

if (0 === \count($records)) {
if (!$user instanceof CustomerUser || !$organization instanceof Organization || 0 === \count($records)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
arguments:
- '@doctrine.orm.entity_manager'
- '@oro_security.acl_helper'
- '@security.helper'
- '@oro_security.token_accessor'
tags:
- { name: kernel.event_listener, event: oro_datagrid.datagrid.build.before.frontend-product-search-grid, method: onBuildBefore }
- { name: kernel.event_listener, event: oro_datagrid.search_datasource.result.after.frontend-product-search-grid, method: onResultAfter }
Expand All @@ -19,5 +19,6 @@ services:
arguments:
- '@doctrine.orm.entity_manager'
- '@oro_security.acl_helper'
- '@oro_security.token_accessor'
tags:
- { name: kernel.event_listener, event: oro_product.product_list.build_result, method: onBuildResult, priority: 10 }
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getFunctions(): array
public function isFavoriteProduct(Product|array|ProductView $product): bool
{
if ($product instanceof ProductView) {
return $product->get('favorite');
return $product->has('favorite') ? $product->get('favorite') : false;
}

if (\is_array($product)) {
Expand Down
Loading