Last active
June 3, 2021 08:34
-
-
Save webdevilopers/11520ffd82d762c4ca72 to your computer and use it in GitHub Desktop.
Sonata Admin - Include custom block / list view in edit template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sonata_block: | |
default_contexts: [cms] | |
blocks: | |
sonata.block.service.recent_contracts: ~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'SonataAdminBundle:CRUD:edit.html.twig' %} | |
{% block sonata_tab_content %} | |
</div> | |
{{ sonata_block_render({ | |
'type': 'sonata.block.service.recent_contracts' | |
}) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends sonata_block.templates.block_base %} | |
{% block block %} | |
{% sonata_template_box 'This is the recent contracts list block.' %} | |
<div class="sonata-contract-block-recent-contract panel panel-default"> | |
{% if settings.title %} | |
<div class="panel-heading"> | |
<div class="panel-title"> | |
<h4 class="sonata-contract-block-recent-contract">{{ settings.title }}</h4> | |
</div> | |
</div> | |
{% endif %} | |
{% if contracts|length > 0 %} | |
<table class="sonata-contract-block-contract-container table table-condensed"> | |
{% for contract in contracts %} | |
<tr> | |
{% if context.getSetting('mode') == 'admin' %} | |
<td><a href="{{ url('admin_plusquam_contract_contract_edit', { 'id': contract.id }) }}">{{ contract.number }}</a></td> | |
{% else %} | |
<td><a href="{{ url('admin_plusquam_contract_contract_show', { 'reference': contract.updatedAt }) }}">{{ contract.updatedAt }}</a></td> | |
{% endif %} | |
<td>{{ contract.createdAt|format_datetime(null) }}</td> | |
</tr> | |
{% endfor %} | |
</table> | |
<div class="panel-body"> | |
{% if context.getSetting('mode') == 'admin' %} | |
<a href="{{ url('admin_plusquam_contract_contract_list') }}" class="btn btn-primary btn-small pull-right"><i class="icon-list icon-white"></i> {{ 'view_all_contracts'|trans({}, 'SonataOrderBundle') }}</a> | |
{% else %} | |
<a href="{{ url('admin_plusquam_contract_contract_list') }}" class="btn btn-primary btn-small pull-right"><i class="glyphicon glyphicon-list icon-white"></i> {{ 'view_all_contracts'|trans({}, 'SonataOrderBundle') }}</a> | |
{% endif %} | |
</div> | |
{% else %} | |
<div class="panel-body"> | |
{% if context.getSetting('mode') == 'admin' %} | |
<p>{{ 'no_contract_found'|trans({}, 'SonataOrderBundle') }}</p> | |
{% else %} | |
<p>{{ 'sonata.contract.list.no_contracts'|trans({}, 'SonataOrderBundle') }}</p> | |
{% endif %} | |
</div> | |
{% endif %} | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Plusquam\Bundle\ContractBundle\Block; | |
use Sonata\BlockBundle\Block\BlockContextInterface; | |
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | |
use Symfony\Component\HttpFoundation\Response; | |
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; | |
use Symfony\Component\Security\Core\SecurityContextInterface; | |
use Doctrine\ORM\EntityManager; | |
/** | |
* Class RecentContractBlockService | |
* | |
* @package Plusquam\Bundle\ContractBundle\Block; | |
* | |
* @author Michael Borchers | |
*/ | |
class RecentContractsBlockService extends BaseBlockService | |
{ | |
/** | |
* @var EntityManager | |
*/ | |
protected $entityManager; | |
/** | |
* @var SecurityContextInterface | |
*/ | |
protected $securityContext; | |
/** | |
* @param string $name | |
* @param EngineInterface $templating | |
* @param EntityManager $entityManager | |
* @param SecurityContextInterface $securityContext | |
*/ | |
public function __construct($name, EngineInterface $templating, EntityManager $entityManager, SecurityContextInterface $securityContext) | |
{ | |
$this->entityManager = $entityManager; | |
$this->securityContext = $securityContext; | |
parent::__construct($name, $templating); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function execute(BlockContextInterface $blockContext, Response $response = null) | |
{ | |
$criteria = array(); | |
$user = $this->securityContext->getToken()->getUser(); | |
$criteria['projectManager'] = $user->getId(); | |
return $this->renderPrivateResponse($blockContext->getTemplate(), array( | |
'context' => $blockContext, | |
'settings' => $blockContext->getSettings(), | |
'block' => $blockContext->getBlock(), | |
'contracts' => $this->entityManager->getRepository('PlusquamContractBundle:Contract') | |
->findBy($criteria, array('updatedAt' => 'DESC'), $blockContext->getSetting('number')) | |
), $response); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'Recent Contracts'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setDefaultSettings(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'number' => 5, | |
'mode' => 'admin', | |
'title' => 'Recent Contracts', | |
'template' => 'PlusquamContractBundle:Block:recent_contracts.html.twig' | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<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"> | |
<parameters> | |
<parameter key="plusquam.contract_bundle.block.recent_contracts.class">Plusquam\Bundle\ContractBundle\Block\RecentContractsBlockService</parameter> | |
</parameters> | |
<services> | |
<service id="sonata.block.service.recent_contracts" class="%plusquam.contract_bundle.block.recent_contracts.class%"> | |
<tag name="sonata.block"/> | |
<argument>sonata.order.block.recent_orders</argument> | |
<argument type="service" id="templating"></argument> | |
<argument type="service" id="doctrine.orm.entity_manager"></argument> | |
<argument type="service" id="security.context"></argument> | |
</service> | |
</services> | |
</container> |
Example using
productId
: @mattboutet
https://gist.github.com/ronanguilloux/0676257355c3e6bf3005 by @ronanguillouxpublic 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); }
Maybe it's better $product = $blockContext->getSetting('productId');
instead of $product = $blockContext->getBlock()->getSetting('productId');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example using
productId
: @mattboutethttps://gist.github.com/ronanguilloux/0676257355c3e6bf3005 by @ronanguilloux