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

Migrating the shop grid configuration to php #15419

Open
wants to merge 7 commits into
base: 2.0
Choose a base branch
from
Open
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
109 changes: 109 additions & 0 deletions src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShopBundle\Grid\Account;

use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\Action\Action;
use Sylius\Bundle\GridBundle\Builder\Action\ShowAction;
use Sylius\Bundle\GridBundle\Builder\Field\DateTimeField;
use Sylius\Bundle\GridBundle\Builder\Field\TwigField;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Customer\Context\CustomerContextInterface;

final class OrderGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public function __construct(
private string $resourceClass,
private CustomerContextInterface $customerContext,
private ChannelContextInterface $channelContext,
) {
}

public static function getName(): string
{
return 'sylius_shop_account_order';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->setRepositoryMethod('createByCustomerAndChannelIdQueryBuilder', [
$this->customerContext->getCustomer()?->getId(),
mamazu marked this conversation as resolved.
Show resolved Hide resolved
$this->channelContext->getChannel()->getId(),
])
->orderBy('checkoutCompletedAt', 'desc')
->addField(
TwigField::create('number', '@SyliusShop/Account/Order/Grid/Field/number.html.twig')
->setLabel('sylius.ui.number')
->setSortable(true)
)
->addField(
DateTimeField::create('checkoutCompletedAt', 'm/d/Y')
mamazu marked this conversation as resolved.
Show resolved Hide resolved
->setLabel('sylius.ui.date')
->setSortable(true)
)
->addField(
TwigField::create('shippingAddress', '@SyliusShop/Account/Order/Grid/Field/address.html.twig')
->setLabel('sylius.ui.ship_to')
)
->addField(
TwigField::create('total', '@SyliusShop/Account/Order/Grid/Field/total.html.twig')
->setLabel('sylius.ui.total')
->setPath('.')
->setSortable(true, 'total')
)
->addField(
TwigField::create('state', '@SyliusUi/Grid/Field/label.html.twig')
->setLabel('sylius.ui.state')
->setSortable(true)
->addOptions([
'vars' => [
'labels' => '@SyliusShop/Account/Order/Label/State',
],
])
)
->addActionGroup(
ItemActionGroup::create(
Action::create('show', 'shop_show')
->setLabel('sylius.ui.show')
->setOptions([
'link' => [
'route' => 'sylius_shop_account_order_show',
'parameters' => [
'number' => 'resource.number',
],
],
]),
Action::create('pay', 'shop_pay')
->setLabel('sylius.ui.pay')
->setOptions([
'link' => [
'route' => 'sylius_shop_order_show',
'parameters' => [
'tokenValue' => 'resource.tokenvalue',
],
],
]),
),
);
}

public function getResourceClass(): string
{
return $this->resourceClass;
}
}
106 changes: 106 additions & 0 deletions src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ShopBundle\Grid;

use Sylius\Bundle\GridBundle\Builder\Field\DateTimeField;
use Sylius\Bundle\GridBundle\Builder\Field\Field;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Filter\Filter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Webmozart\Assert\Assert;

final class ProductGrid extends AbstractGrid implements ResourceAwareGridInterface
{
/**
* @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository
*/
public function __construct(
private string $resourceClass,
private ChannelContextInterface $channelContext,
private LocaleContextInterface $localeContext,
private TaxonRepositoryInterface $taxonRepository,
private RequestStack $requestStack,
private bool $includeAllDescendants,
) {
}

public static function getName(): string
{
return 'sylius_shop_product';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$request = $this->requestStack->getMainRequest();
Assert::notNull($request, 'No main request available.');

$localeCode = $this->localeContext->getLocaleCode();

// @see Sylius\Bundle\ResourceBundle\ExpressionLanguage\NotNullExpressionFunctionProvider
mamazu marked this conversation as resolved.
Show resolved Hide resolved
$taxon = $this->taxonRepository->findOneBySlug($request->attributes->get('slug'), $localeCode);
mamazu marked this conversation as resolved.
Show resolved Hide resolved
if ($taxon === null) {
throw new NotFoundHttpException('Requested page is invalid');
}

$gridBuilder
->setRepositoryMethod('createShopListQueryBuilder', [
$this->channelContext->getChannel(),
$taxon,
$localeCode,
$request->query->all('sorting'),
diimpp marked this conversation as resolved.
Show resolved Hide resolved
$this->includeAllDescendants,
])
->orderBy('position', 'asc')
->setLimits([
9,
18,
27,
])
->addField(
DateTimeField::create('createdAt')
->setSortable(true),
)
->addField(
StringField::create('position')
->setSortable(true, 'productTaxon.position'),
)
->addField(
StringField::create('name')
->setSortable(true, 'translation.name'),
)
->addField(
Field::create('price', 'int')
->setSortable(true, 'channelPricing.price'),
)
->addFilter(
Filter::create('search', 'shop_string')
->setLabel(false)
->addOption('fields', ['translation.name'])
->addFormOption('type', 'contains')
);
}

public function getResourceClass(): string
{
return $this->resourceClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
imports:
- { resource: 'twig_hooks/**/*.yaml' }
- { resource: "@SyliusShopBundle/Resources/config/app/sylius/sylius_mailer.yml" }
- { resource: "@SyliusShopBundle/Resources/config/grids/account/order.yml" }
- { resource: "@SyliusShopBundle/Resources/config/grids/product.yml" }

parameters:
sylius.security.shop_regex: "^/(?!%sylius_admin.path_name%|api/.*|api$|media/.*)[^/]++"
Expand Down
33 changes: 33 additions & 0 deletions src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

This file is part of the Sylius package.

(c) Sylius Sp. z o.o.

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.shop.grid.account.order" class="Sylius\Bundle\ShopBundle\Grid\Account\OrderGrid">
<argument>%sylius.model.order.class%</argument>
<argument type="service" id="sylius.context.customer" />
<argument type="service" id="sylius.context.channel" />
<tag name="sylius.grid" />
</service>

<service id="sylius.shop.grid.product" class="Sylius\Bundle\ShopBundle\Grid\ProductGrid">
<argument>%sylius.model.product.class%</argument>
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.context.locale" />
mamazu marked this conversation as resolved.
Show resolved Hide resolved
<argument type="service" id="sylius.repository.taxon" />
<argument type="service" id="request_stack" />
<argument>%sylius_shop.product_grid.include_all_descendants%</argument>
<tag name="sylius.grid" />
</service>
</services>
</container>
Loading