Skip to content

Instantly share code, notes, and snippets.

@ronanguilloux
Created September 30, 2014 13:29
Show Gist options
  • Save ronanguilloux/0676257355c3e6bf3005 to your computer and use it in GitHub Desktop.
Save ronanguilloux/0676257355c3e6bf3005 to your computer and use it in GitHub Desktop.
Associate a Custom Sonata Admin Class to a Sonata Block Service
<?php
namespace Application\Sonata\PageBundle\Block\Service;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Class ProductBlockService
*
* @package Application\Sonata\PageBundle\Block\Service
*/
class ProductBlockService extends BaseBlockService
{
protected $productAdmin;
/**
* @param string $name
* @param EngineInterface $templating
* @param ContainerInterface $container
*/
public function __construct($name, EngineInterface $templating, ContainerInterface $container)
{
parent::__construct($name, $templating);
$this->container = $container;
$this->manager = $this->container->get('doctrine')->getManager();
}
/**
* {@inheritdoc}
*/
public function getName()
{
return "Product" ;
}
/**
* {@inheritdoc}
*/
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
$settings = $blockContext->getSettings();
$product = $blockContext->getBlock()->getSetting('productId');
$parameters = array(
'context' => $blockContext,
'settings' => $settings,
'block' => $blockContext->getBlock(),
'product' => $product,
);
return $this->renderResponse($blockContext->getTemplate(), $parameters, $response);
}
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('title', 'text', array('required' => true)),
array($this->getProductBuilder($formMapper), null, array()),
)));
}
/**
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper
*
* @return \Symfony\Component\Form\FormBuilder
*/
protected function getProductBuilder(FormMapper $formMapper)
{
// simulate an association ...
$fieldDescription = $this->getProductAdmin()->getModelManager()->getNewFieldDescriptionInstance($this->productAdmin->getClass(), 'product');
$fieldDescription->setAssociationAdmin($this->getProductAdmin());
$fieldDescription->setAdmin($formMapper->getAdmin());
//$fieldDescription->setOption('edit', 'list');
$fieldDescription->setAssociationMapping(array(
'fieldName' => 'product',
'type' => \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_MANY
));
return $formMapper->create('productId', 'sonata_type_model', array(
'sonata_field_description' => $fieldDescription,
'class' => $this->getProductAdmin()->getClass(),
'model_manager' => $this->getProductAdmin()->getModelManager()
));
}
/**
* @return mixed
*/
public function getProductAdmin()
{
if (!$this->productAdmin) {
$this->productAdmin = $this->container->get('my.admin.product'); // cf. sonata services in .yml files
}
return $this->productAdmin;
}
/**
* {@inheritdoc}
*/
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
$errorElement
->with('settings.title')
->assertNotNull(array())
->assertNotBlank()
->assertMaxLength(array('limit' => 255))
->end();
}
/**
* {@inheritdoc}
*/
public function setDefaultSettings(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'title' => '',
'product' => false,
'productId' => false,
'template' => 'ApplicationSonataPageBundle:Block:product.html.twig',
));
}
/**
* {@inheritdoc}
*/
public function prePersist(BlockInterface $block)
{
$block->setSetting('productId', is_object($block->getSetting('productId')) ? $block->getSetting('productId')->getId() : null);
}
/**
* {@inheritdoc}
*/
public function preUpdate(BlockInterface $block)
{
$block->setSetting('productId', is_object($block->getSetting('productId')) ? $block->getSetting('productId')->getId() : null);
}
/**
* {@inheritdoc}
*/
public function load(BlockInterface $block)
{
$product = $block->getSetting('productId');
if ($product) {
$product = $this->manager->getRepository('My\CustomBundle\Entity\Product')->findOneBy(array('id' => $product));
}
$block->setSetting('productId', $product);
}
}
@webdevilopers
Copy link

👍

@flgiraud
Copy link

Big thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment