Skip to content

Instantly share code, notes, and snippets.

@chihiro-adachi
Created March 29, 2017 08:12
Show Gist options
  • Save chihiro-adachi/730925fe403140d9db5fbbe0cf3f89dd to your computer and use it in GitHub Desktop.
Save chihiro-adachi/730925fe403140d9db5fbbe0cf3f89dd to your computer and use it in GitHub Desktop.
規格とかのcollection系に項目追加するサンプル
<?php
namespace Plugin\Hoge\ServiceProvider;
use Eccube\Entity\ProductClass;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Silex\Application as BaseApplication;
use Silex\ServiceProviderInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class HogeServiceProvider implements ServiceProviderInterface
{
public function register(BaseApplication $app)
{
$app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function ($extensions) use ($app) {
$extensions[] = new ProductClassHogeExtension();
return $extensions;
}));
$app['eccube.event.dispatcher']->addListener(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_INDEX_CLASSES, function (EventArgs $event) {
// ここではとくになにもしない
$builder = $event->getArgument('builder');
});
//
$app['eccube.event.dispatcher']->addListener('Admin/Product/product_class.twig', function (TemplateEvent $event) {
$source = $event->getSource();
$search = '<th id="result_box__header_class_category1';
$replace = '<th>ほげ</th>'.$search;
$source = str_replace($search, $replace, $source);
$search = '<td id="result_box__class_category1';
$replace = '<td>
{{ form_widget(product_class_form.koumoku) }}
{{ form_errors(product_class_form.koumoku) }}
</td>'.$search;
$source = str_replace($search, $replace, $source);
$event->setSource($source);
});
}
public function boot(BaseApplication $app)
{
}
}
class ProductClassHogeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('koumoku', 'text', array(
'required' => false,
'mapped' => false,
));
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
/** @var ProductClass $data */
$data = $event->getData();
if (is_null($data)) {
return;
}
if ($data->getId()) {
// product classに紐づくデータ取得
// $app['repository']->find($date->getId())
$form = $event->getForm();
// 紐付いているデータをセットする
$form['koumoku']->setData('ほげほげ');
}
});
}
public function getExtendedType()
{
return 'admin_product_class';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment