Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-463: Block templates #515

Merged
merged 8 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions features/admin/adding_block.feature
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,16 @@ Feature: Adding blocks
And I try to add it
Then I should be notified that "Code, Name" fields are too long

@ui @javascript
Scenario: Adding block with template
Given there is an existing template named "Homepage" with "Block" type that contains "Textarea, Single media" content elements
When I go to the create block page
And I fill the code with "intro"
And I fill the name with "Intro"
And I select "Homepage" template
And I click button to use this template
And I confirm that I want to use this template
And I add it
Then I should be notified that the block has been created
And I should see newly created "Textarea" content element in Content elements section
And I should see newly created "Single media" content element in Content elements section
10 changes: 9 additions & 1 deletion features/admin/adding_template.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ Feature: Adding cms templates
And I am logged in as an administrator

@ui
Scenario: Creating template
Scenario: Creating template with type page
When I go to the create template page
And I fill the name with "Test template"
And I choose "Page" in Type field
And I add it
Then I should be notified that the template has been created

@ui
Scenario: Creating template with type block
When I go to the create template page
And I fill the name with "Test template"
And I choose "Block" in Type field
And I add it
Then I should be notified that the template has been created

@ui @javascript
Scenario: Creating template with content elements
When I go to the create template page
Expand Down
11 changes: 11 additions & 0 deletions spec/Menu/ContentManagementMenuBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
namespace spec\BitBag\SyliusCmsPlugin\Menu;

use BitBag\SyliusCmsPlugin\Menu\ContentManagementMenuBuilder;
use BitBag\SyliusCmsPlugin\Menu\MenuReorderInterface;
use Knp\Menu\ItemInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;

final class ContentManagementMenuBuilderSpec extends ObjectBehavior
{
public function let(MenuReorderInterface $menuReorder): void
{
$this->beConstructedWith($menuReorder);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ContentManagementMenuBuilder::class);
Expand Down Expand Up @@ -65,6 +71,11 @@ public function it_build_menu(
$cmsRootMenuItem->setLabel('bitbag_sylius_cms_plugin.ui.media')->willReturn($cmsRootMenuItem);
$cmsRootMenuItem->setLabelAttribute('icon', 'file')->shouldBeCalled();

$menu->getChildren()->willReturn(['marketing' => $cmsRootMenuItem]);
$menu->getChild('bitbag_cms')->willReturn($cmsRootMenuItem);

$menu->setChildren(['marketing' => $cmsRootMenuItem, 'bitbag_cms' => $cmsRootMenuItem])->willReturn($menu);

$this->buildMenu($menuBuilderEvent);
}
}
106 changes: 106 additions & 0 deletions spec/Menu/MenuReorderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusCmsPlugin\Menu;

use BitBag\SyliusCmsPlugin\Menu\MenuReorder;
use BitBag\SyliusCmsPlugin\Menu\MenuReorderInterface;
use Knp\Menu\ItemInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

final class MenuReorderSpec extends ObjectBehavior
{
public function it_is_initializable(): void
{
$this->shouldHaveType(MenuReorder::class);
}

public function it_implements_menu_reorder_interface(): void
{
$this->shouldImplement(MenuReorderInterface::class);
}

public function it_reorders_menu_items(
ItemInterface $menu,
ItemInterface $item1,
ItemInterface $item2,
ItemInterface $item3
): void
{
$menu->getChildren()->willReturn([
'item1' => $item1,
'item2' => $item2,
'item3' => $item3
]);

$menu->getChild('item2')->willReturn($item2);

$menu->setChildren([
'item1' => $item1,
'item3' => $item3,
'item2' => $item2
])->shouldBeCalled();

$this->reorder($menu, 'item2', 'item3');
}

public function it_does_not_reorder_if_new_item_is_not_found(
ItemInterface $menu,
ItemInterface $item1,
ItemInterface $item3
): void
{
$menu->getChildren()->willReturn([
'item1' => $item1,
'item3' => $item3
]);

$menu->getChild('item2')->willReturn(null);
$menu->setChildren(Argument::any())->shouldNotBeCalled();

$this->reorder($menu, 'item2', 'item3');
}

public function it_does_not_reorder_if_target_item_is_not_found(
ItemInterface $menu,
ItemInterface $item1,
ItemInterface $item2
): void
{
$menu->getChildren()->willReturn([
'item1' => $item1,
'item2' => $item2
]);

$menu->getChild('item1')->willReturn($item1);
$menu->setChildren(Argument::any())->shouldNotBeCalled();

$this->reorder($menu, 'item1', 'item3');
}

public function it_does_not_modify_menu_when_no_reorder_is_needed(
ItemInterface $menu,
ItemInterface $item1,
ItemInterface $item2
): void
{
$menu->getChildren()->willReturn([
'item1' => $item1,
'item2' => $item2
]);

$menu->getChild('item1')->willReturn($item1);
$menu->getChild('item2')->willReturn($item2);
$menu->setChildren(Argument::any())->shouldNotBeCalled();

$this->reorder($menu, 'item1', 'item1');
}
}
4 changes: 4 additions & 0 deletions src/Form/Type/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'multiple' => true,
'help' => 'bitbag_sylius_cms_plugin.ui.display_for_taxons.help',
])
->add('template', TemplateBlockAutocompleteChoiceType::class, [
'label' => false,
'mapped' => false,
])
;
}

Expand Down
19 changes: 19 additions & 0 deletions src/Form/Type/TemplateBlockAutocompleteChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Form\Type;

final class TemplateBlockAutocompleteChoiceType extends AbstractTemplateAutocompleteChoiceType
{
public function getBlockPrefix(): string
{
return 'bitbag_template_block_autocomplete_choice';
}
}
1 change: 1 addition & 0 deletions src/Form/Type/TemplateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'bitbag_sylius_cms_plugin.ui.type',
'choices' => [
'bitbag_sylius_cms_plugin.ui.page' => 'page',
'bitbag_sylius_cms_plugin.ui.block' => 'block',
],
])
->add('contentElements', CollectionType::class, [
Expand Down
6 changes: 6 additions & 0 deletions src/Menu/ContentManagementMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

final class ContentManagementMenuBuilder
{
public function __construct(private MenuReorderInterface $menuReorder)
{
}

public function buildMenu(MenuBuilderEvent $menuBuilderEvent): void
{
$menu = $menuBuilderEvent->getMenu();
Expand Down Expand Up @@ -62,5 +66,7 @@ public function buildMenu(MenuBuilderEvent $menuBuilderEvent): void
->setLabel('bitbag_sylius_cms_plugin.ui.media')
->setLabelAttribute('icon', 'file')
;

$this->menuReorder->reorder($menu, 'bitbag_cms', 'marketing');
}
}
38 changes: 38 additions & 0 deletions src/Menu/MenuReorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Menu;

use Knp\Menu\ItemInterface;

final class MenuReorder implements MenuReorderInterface
{
public function reorder(ItemInterface $menu, string $newItemKey, string $targetItemKey): void
{
$menuItems = $menu->getChildren();

$newMenuItem = $menu->getChild($newItemKey);
unset($menuItems[$newItemKey]);

$targetPosition = array_search($targetItemKey, array_keys($menuItems), true);

if (null !== $newMenuItem && false !== $targetPosition) {
$menuItems = array_slice($menuItems, 0, $targetPosition + 1, true) +
[$newItemKey => $newMenuItem] +
array_slice($menuItems, $targetPosition + 1, null, true);

$menuItems = array_filter($menuItems, static function ($item) {
return $item instanceof ItemInterface;
});

$menu->setChildren($menuItems);
}
}
}
18 changes: 18 additions & 0 deletions src/Menu/MenuReorderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Menu;

use Knp\Menu\ItemInterface;

interface MenuReorderInterface
{
public function reorder(ItemInterface $menu, string $newItemKey, string $targetItemKey): void;
}
14 changes: 7 additions & 7 deletions src/Resources/assets/admin/js/bitbag/bitbag-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ export class HandleTemplate {
$(document).ready(() => {
const cmsLoadTemplate = $('[data-bb-cms-load-template]');
const cmsPageTemplate = $('#bitbag_sylius_cms_plugin_page_template');
const cmsBlockTemplate = $('#bitbag_sylius_cms_plugin_block_template');

cmsLoadTemplate.on('click', function (e) {
e.preventDefault();

if (!cmsPageTemplate.val()) {
if (!cmsPageTemplate.val() && !cmsBlockTemplate.val()) {
return;
}

$('#load-template-confirmation-modal').modal('show');
});

$('#load-template-confirmation-button').on('click', function (e) {
const templateId = cmsPageTemplate.val();
$('#load-template-confirmation-button').on('click', function () {
const templateId = cmsPageTemplate.val() ?? cmsBlockTemplate.val();
if (!templateId) {
return;
}
Expand All @@ -34,9 +35,6 @@ export class HandleTemplate {
$.ajax({
url: endpointUrl,
type: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
success: function(data) {
if (data.status === 'success') {
$('[id^="bitbag_sylius_cms_plugin_"][id$="contentElements"]')
Expand All @@ -47,7 +45,9 @@ export class HandleTemplate {
$('[data-form-collection="add"]').trigger('click');
});

const elements = $('[id^="bitbag_sylius_cms_plugin_page_contentElements_"][id$="_type"]');
const elements = $('[id^="bitbag_sylius_cms_plugin_"][id*="_contentElements_"][id$="_type"]').filter(function() {
return /_page_|_block_/.test(this.id);
});

$.each(data.content, function (index, element) {
elements.eq(index).val(element.type);
Expand Down
21 changes: 18 additions & 3 deletions src/Resources/config/routing/admin/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bitbag_sylius_cms_plugin_admin_template:
type: sylius.resource

bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase:
path: /ajax/templates/search
path: /ajax/templates/page/search
methods: [GET]
defaults:
_format: json
Expand All @@ -30,7 +30,22 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase:
phrase: $phrase
type: page

bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id:
bitbag_sylius_cms_plugin_admin_ajax_template_block_by_name_phrase:
path: /ajax/templates/block/search
methods: [GET]
defaults:
_format: json
_controller: bitbag_sylius_cms_plugin.controller.template::indexAction
_sylius:
serialization_groups: [Autocomplete]
permission: true
repository:
method: findTemplatesByNamePart
arguments:
phrase: $phrase
type: block

bitbag_sylius_cms_plugin_admin_ajax_template_by_id:
path: /ajax/templates/id
methods: [GET]
defaults:
Expand All @@ -43,7 +58,7 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id:
method: find
arguments: [id: $id]

bitbag_sylius_cms_plugin_admin_ajax_template_page_content_by_id:
bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id:
path: /ajax/templates/content/{id}
methods: [GET]
defaults:
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<defaults public="true" />

<service id="bitbag_sylius_cms_plugin.menu.content_management" class="BitBag\SyliusCmsPlugin\Menu\ContentManagementMenuBuilder">
<argument type="service" id="bitbag_sylius_cms_plugin.menu.reorder" />
<tag name="kernel.event_listener" event="sylius.menu.admin.main" method="buildMenu" />
</service>

Expand Down
9 changes: 9 additions & 0 deletions src/Resources/config/services/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>

<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">
<services>
<defaults public="true" />

<service id="bitbag_sylius_cms_plugin.menu.reorder" class="BitBag\SyliusCmsPlugin\Menu\MenuReorder" />
</services>
</container>
Loading
Loading