Skip to content

Commit

Permalink
Be able to only add editable blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Oct 6, 2022
1 parent dc3481a commit e662e16
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
23 changes: 17 additions & 6 deletions src/Controller/BlockAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\AdminBundle\Exception\BadRequestParamHttpException;
use Sonata\BlockBundle\Block\BlockServiceManagerInterface;
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\PageBundle\Model\BlockInteractorInterface;
use Sonata\PageBundle\Model\PageBlockInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -87,12 +88,22 @@ public function createAction(Request $request): Response

$parameters = $this->admin->getPersistentParameters();

$blockManager = $this->container->get('sonata.block.manager');
\assert($blockManager instanceof BlockServiceManagerInterface);

if (null === $parameters['type']) {
$blockManager = $this->container->get('sonata.block.manager');
\assert($blockManager instanceof BlockServiceManagerInterface);

$blockServices = $blockManager->getServicesByContext('sonata_page_bundle');

foreach ($blockServices as $code => $service) {
if ($service instanceof EditableBlockService) {
continue;
}

unset($blockServices[$code]);
}

return $this->renderWithExtraParams('@SonataPage/BlockAdmin/select_type.html.twig', [
'services' => $blockManager->getServicesByContext('sonata_page_bundle'),
'blockServices' => $blockServices,
'base_template' => $this->getBaseTemplate(),
'admin' => $this->admin,
'action' => 'create',
Expand Down Expand Up @@ -159,12 +170,12 @@ public function composePreviewAction(Request $request): Response

$blockManager = $this->container->get('sonata.block.manager');
\assert($blockManager instanceof BlockServiceManagerInterface);
$blockServices = $blockManager->getServicesByContext('sonata_page_bundle', false);
$blockService = $blockManager->get($existingObject);

return $this->renderWithExtraParams('@SonataPage/BlockAdmin/compose_preview.html.twig', [
'container' => $container,
'child' => $existingObject,
'blockServices' => $blockServices,
'blockService' => $blockService,
'blockAdmin' => $this->admin,
]);
}
Expand Down
11 changes: 10 additions & 1 deletion src/Controller/PageAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\BlockBundle\Block\BlockServiceManagerInterface;
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\PageBundle\Admin\BlockAdmin;
use Sonata\PageBundle\Admin\SnapshotAdmin;
use Sonata\PageBundle\Model\BlockInteractorInterface;
Expand Down Expand Up @@ -273,6 +274,14 @@ public function composeContainerShowAction(Request $request): Response
\assert($blockManager instanceof BlockServiceManagerInterface);
$blockServices = $blockManager->getServicesByContext('sonata_page_bundle', false);

foreach ($blockServices as $code => $blockService) {
if ($blockService instanceof EditableBlockService) {
continue;
}

unset($blockServices[$code]);
}

$page = $block->getPage();
$blockCode = $block->getSetting('code');

Expand All @@ -295,7 +304,7 @@ public function composeContainerShowAction(Request $request): Response
$container = $template->getContainer($blockCode);

if (null !== $container && \count($container['blocks']) > 0) {
foreach ($blockServices as $code => $service) {
foreach ($blockServices as $code => $blockService) {
if (\in_array($code, $container['blocks'], true)) {
continue;
}
Expand Down
16 changes: 7 additions & 9 deletions src/Resources/views/BlockAdmin/compose_preview.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
data-block-enabled="{{ child.enabled|default('0') }}"
data-block-type="{{ child.type }}"
>
{% set service = attribute(blockServices, child.type) ?? null %}

{% if service %}
{% if blockService %}
<a
class="page-composer__container__child__edit"
href="{{ blockAdmin.generateUrl('edit', { 'id': child.id, 'composer': true }) }}"
>
{% else %}
<div class="page-composer__container__child__error">
{% endif %}
{% if service %}
{% set metadataTitle = service.metadata.title|trans({}, service.metadata.domain|default('SonataPageBundle')) %}
{% if blockService %}
{% set metadataTitle = blockService.metadata.title|trans({}, blockService.metadata.domain|default('SonataPageBundle')) %}
{% set blockTitle = child.name|default(metadataTitle) %}
{% set blockImage = service.metadata.image ? asset(service.metadata.image) : null %}
{% set blockClass = service.metadata.option('class') %}
{% set blockImage = blockService.metadata.image ? asset(blockService.metadata.image) : null %}
{% set blockClass = blockService.metadata.option('class') %}
{% else %}
{% set metadataTitle = child.type %}
{% set blockTitle = child.name %}
Expand All @@ -36,13 +34,13 @@

<small>{{ metadataTitle }}</small>

{% if service %}
{% if blockService %}
<span class="page-composer__container__child__toggle">
<i class="fa fa-chevron-down"></i>
<i class="fa fa-chevron-up"></i>
</span>
{% endif %}
{% if service %}
{% if blockService %}
</a>
{% else %}
</div>
Expand Down
19 changes: 7 additions & 12 deletions src/Resources/views/BlockAdmin/select_type.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,23 @@ file that was distributed with this source code.
</h3>
</div>
<div class="box-body">
{% for code, service in services|filter(service => service.metadata is defined or service.blockMetadata is defined) %}
{% if service.metadata is defined %}
{% set metadata = service.metadata %}
{% else %}
{% set metadata = service.blockMetadata %}
{% endif %}
{% for code, blockService in blockServices %}
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
<a href="{{ admin.generateUrl('create', {'type': code}) }}"
class="btn btn-app btn-block sonata-block-type"
data-toggle="tooltip"
data-placement="top"
{% if metadata.description|length > 0 %}
title="{{ metadata.description|trans({}, metadata.domain|default('SonataBlockBundle')) }}"
{% if blockService.metadata.description %}
title="{{ blockService.metadata.description|trans({}, blockService.metadata.domain|default('SonataBlockBundle')) }}"
{% endif %}
>
{% if not metadata.image %}
<i class="{{ metadata.option('class') }}" ></i>
{% if not blockService.metadata.image %}
<i class="{{ blockService.metadata.option('class') }}" ></i>
{% else %}
<img src="{{ asset(metadata.image) }}" style="max-height: 20px; max-width: 100px;"/>
<img src="{{ asset(blockService.metadata.image) }}" style="max-height: 20px; max-width: 100px;"/>
<br />
{% endif %}
<span>{{ metadata.title|trans({}, metadata.domain|default('SonataBlockBundle')) }}</span>
<span>{{ blockService.metadata.title|trans({}, blockService.metadata.domain|default('SonataBlockBundle')) }}</span>
</a>
</div>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
<div class="page-composer__block-type-selector">
<label>{{ 'composer.block.add.type'|trans({}, 'SonataPageBundle') }}</label>
<select class="page-composer__block-type-selector__select" style="width: auto">
{% for blockServiceId, blockService in blockServices|filter(service => service.metadata is defined) %}
{% for blockServiceId, blockService in blockServices %}
<option value="{{ blockServiceId }}">{{ blockService.metadata.title|trans({}, blockService.metadata.domain|default('SonataBlockBundle')) }}</option>
{% endfor %}
{% for blockServiceId, blockService in blockServices|filter(service => service.blockMetadata is defined and service.metadata is not defined) %}
<option value="{{ blockServiceId }}">{{ blockService.blockMetadata.title|trans({}, blockService.blockMetadata.domain|default('SonataBlockBundle')) }}</option>
{% endfor %}
</select>
<a class="btn btn-action btn-small page-composer__block-type-selector__confirm"
href="{{ admin.generateObjectUrl('sonata.page.admin.block.create', page, {'composer': true}) }}"
Expand All @@ -27,7 +24,9 @@

<ul class="page-composer__container__children">
{% for child in container.children %}
{% include '@SonataPage/BlockAdmin/compose_preview.html.twig' %}
{% include '@SonataPage/BlockAdmin/compose_preview.html.twig' with {
'blockService': attribute(blockServices, child.type) ?? null
} %}
{% endfor %}
</ul>
</div>

0 comments on commit e662e16

Please sign in to comment.