Skip to content

Instantly share code, notes, and snippets.

@cornobils
Created July 22, 2017 12:54
Show Gist options
  • Save cornobils/2802e710d3a63f7afd0b388376f4d6de to your computer and use it in GitHub Desktop.
Save cornobils/2802e710d3a63f7afd0b388376f4d6de to your computer and use it in GitHub Desktop.
Sylius product filter by taxons
# SyliusShopBundle/config/routing/product.yml
sylius_shop_product_index:
path: /taxons/{slug}
methods: [GET]
defaults:
_controller: sylius.controller.product:indexAction
_sylius:
template: "@SyliusShop/Product/index.html.twig"
grid: sylius_shop_custom_filter
requirements:
slug: .+
# SyliusShopBundle/config/grids/product.yml
sylius_shop_custom_filter:
driver:
name: doctrine/orm
options:
class: "%sylius.model.product.class%"
repository:
method: customCreateShopListQueryBuilder
arguments:
channel: "expr:service('sylius.context.channel').getChannel()"
#taxon: "expr:notFoundOnNull(service('sylius.repository.taxon').findOneBySlug($slug, service('sylius.context.locale').getLocaleCode()))"
locale: "expr:service('sylius.context.locale').getLocaleCode()"
sorting: "expr:service('request_stack').getCurrentRequest().get('sorting', [])"
#sorting:
# position: asc
limits: [9, 18, 27]
fields:
createdAt:
type: datetime
sortable: ~
position:
type: string
#sortable: productTaxon.position
name:
type: string
sortable: translation.name
price:
type: int
sortable: channelPricing.price
filters:
#search: # if you not need in keyword searching
# type: string
# label: false
# options:
# fields: [translation.name]
# form_options:
# type: contains
taxon:
type: taxon_checkboxes
templates:
filter:
taxon_checkboxes: 'AppBundle:Grid/Filter:taxon_filter.html.twig'
services:
app.grid.filter.taxon_checkboxes:
class: AppBundle\Grid\Filter\TaxonsFilter
tags:
- { name: sylius.grid_filter, type: taxon_checkboxes, form-type: AppBundle\Form\Type\Filter\TaxonsCheckboxType }
{% form_theme form '@App/custom_theme.html.twig' %}
{{ form_row(form.id) }}
<?php
namespace AppBundle\Form\Type\Filter;
use Sylius\Component\Core\Model\Taxon;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class TaxonsCheckboxType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('id', EntityType::class, [
'class' => Taxon::class,
'expanded' => TRUE,
'multiple' => TRUE,
'label' => false
])
;
}
}
<?php
namespace AppBundle\Grid\Filter;
use Doctrine\ORM\EntityManager;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Grid\Data\DataSourceInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
class TaxonsFilter implements FilterInterface {
public function apply(DataSourceInterface $dataSource, $name, $data, array $options) {
$dataSource->restrict($dataSource->getExpressionBuilder()->in('t.id', array_values($data['id'])));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment