2017-01-24 3 views
0

Sylius 1.0.0-beta1を使用し、dev-masterからEntityFilterを移植しました。最後の安定バージョンではこの機能が不足しています。すべてがうまくいきますが、すべてのリソースからではなく、その一部からのみを選択する方法はありますか? タクソノミーに基づいてフィルタを作成する必要があります。私は都市名であるいくつかの分類群を持っていて、それらのすべてに都市(コード:都市)と呼ばれる親分類群があります。だから、私は都市分類群のすべての子供たちをそのフィルターに表示したいと思っています。リソースの一部からのSylius EntityFilterの選択

私のグリッドの構成を以下に示します。

sylius_grid: 
    grids: 
     smartbyte_admin_products_by_event_archetype: 
      ... 
      filters: 
       ... 
       taxon: 
        type: app_entity 
        options: 
         fields: [taxon.id] 
         class: "%sylius.model.taxon.class%" 
       city: 
        type: app_taxon 

細かい設定の作品やフィルタからの第一のフィルタ、それはすべての分類群がかかりますが、私はいくつかだけを表示する必要がある点が異なります。

私は自分のフィルタ(2番目のもの)も作ってみましたが、フィルタのエンティティフィールドの代わりにテキストフィールドを取得しました。 docsに続いて私はカスタム版を作成しました。ここに私の試みは、次のとおりです。

<?php 

namespace SyliusExtensionBundle\Form\Type\Filter; 


use Doctrine\ORM\EntityRepository; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

final class TaxonFilterType extends AbstractType { 


    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('city', EntityType::class, array(
      'class' => 'Sylius\Component\Core\Model\Taxon', 
      'query_builder' => function (EntityRepository $er) { 
       return $er->createQueryBuilder('t') 
          ->leftJoin('t.parent', 'taxon') 
          ->where("taxon.code = 'city'"); 
      }, 
      'label' => 'Miasto', 
      'required' => false 
     )); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver 
      ->setDefaults([ 
       'label' => false, 
       'placeholder' => 'sylius.ui.all', 
      ]) 
      ->setDefined('fields') 
      ->setAllowedTypes('fields', 'array') 
     ; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getName() 
    { 
     return 'sylius_grid_filter_taxon'; 
    } 
} 

サービスの設定:

services: 
    sylius.grid_filter.entity: 
     class: SyliusExtensionBundle\Grid\Filter\EntityFilter 
     tags: 
      - { name: sylius.grid_filter, type: app_entity, form-type: SyliusExtensionBundle\Form\Type\Filter\EntityFilterType } 
      - { name: sylius.grid_filter, type: app_taxon, form-type: SyliusExtensionBundle\Form\Type\Filter\EntityFilterType } 
    sylius.form.type.grid_filter.entity: 
     class: SyliusExtensionBundle\Form\Type\Filter\EntityFilterType 
     tags: 
      - { name: form.type, alias: sylius_grid_filter_entity } 
    app.form.type.grid_filter.taxon: 
     class: SyliusExtensionBundle\Form\Type\Filter\TaxonFilterType 
     tags: 
      - { name: form.type, alias: sylius_grid_filter_taxon } 

そして最後にフィルターテンプレート:

sylius_grid: 
    templates: 
     filter: 
      app_entity: "SyliusExtensionBundle:Grid/Filter:entity.html.twig" 
      app_taxon: "SyliusExtensionBundle:Grid/Filter:entity.html.twig" 

私はEntityFilterを制限することができ、私の方法またはカスタムフィルタを動作させる方法を案内してください。 。私はこの件に関して何時間も過ごしましたが、どこにエラーがあるのか​​分かりません。以下

現在の効果:

enter image description here

EDIT:パヴェルJędrzejewskiヒントに従って

現在TaxonFilterType。まだ動作しませんし、構成のフィールドオプションを検出しません。

<?php 
/** 
* Created by PhpStorm. 
* User: Krzysztof Wędrowicz [email protected] 
* Date: 23.01.17 
* Time: 14:56 
*/ 

namespace SyliusExtensionBundle\Form\Type\Filter; 


use Doctrine\ORM\EntityRepository; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

final class TaxonFilterType extends AbstractType { 

    public function getParent() 
    { 
     return EntityType::class; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver 
      ->setDefaults([ 
       'label' => false, 
       'placeholder' => 'sylius.ui.all', 
       'class' => 'Sylius\Component\Core\Model\Taxon', 
       'query_builder' => function (EntityRepository $er) { 
        return $er->createQueryBuilder('t') 
           ->leftJoin('t.parent', 'taxon') 
           ->where("taxon.code = 'city'") 
         ; 
       }, 
       'required' => false 
      ]) 
      ->setDefined('fields') 
      ->setAllowedTypes('fields', 'array') 
     ; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getBlockPrefix() 
    { 
     return 'sylius_grid_filter_city'; 
    } 
} 

答えて

1

現在、これは設定ではできません。バックログに追加しますが、いつ実装できるかはわかりません。つまり、カスタムフィルタは良いアイデアです。小さな変更を行う必要がありますし、動作します。buildFormメソッドを使用する代わりに、フォームの種類がでgetParent()にある必要があります。また、カスタムクエリービルダーはconfigureOptionsメソッドで設定し、適切なフィールドを表示します。

<?php 

namespace AcmeExtension\Form\Type\Filter; 

use Doctrine\ORM\EntityRepository; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

final class CityFilterType extends AbstractType 
{ 
    public function getParent() 
    { 
     return EntityType::class; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver 
      ->setDefaults([ 
       'label' => false, 
       'placeholder' => 'sylius.ui.all', 
       'class' => 'Sylius\Component\Core\Model\Taxon', 
       'query_builder' => function (EntityRepository $er) { 
        return $er->createQueryBuilder('t') 
         ->leftJoin('t.parent', 'taxon') 
         ->where("taxon.code = 'city'") 
        ; 
       }, 
       'required' => false 
      ]) 
      ->setDefined('fields') 
      ->setAllowedTypes('fields', 'array') 
     ; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getBlockPrefix() 
    { 
     return 'sylius_grid_filter_city'; 
    } 
} 
+0

あなたの応答に感謝しますが、まだ何かが壊れています。エンティティフィルタのようにoptions.fieldsを追加すると、「オプション」フィールドが「存在しません」というTwigエラーが表示されます。私はそれを削除したとき、まだ生のテキスト入力です。私はあなたのソリューションからクラス全体をコピーしました。 – Starspire

+0

あなたのヒントに従って質問が更新されました。 – Starspire

関連する問題