Skip to content

Commit

Permalink
Extracted menu builder from ProductCatalog package (#1344)
Browse files Browse the repository at this point in the history
Extracted menu builder from ProductCatalog
  • Loading branch information
mikadamczyk authored Sep 17, 2024
1 parent 2dc387b commit e3594d0
Show file tree
Hide file tree
Showing 34 changed files with 321 additions and 45 deletions.
3 changes: 3 additions & 0 deletions src/bundle/Resources/config/services/menu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ services:
# Menu Item Factory
#

Ibexa\Contracts\AdminUi\Menu\MenuItemFactoryInterface:
alias: Ibexa\AdminUi\Menu\MenuItemFactory

Ibexa\AdminUi\Menu\MenuItemFactory: ~

#
Expand Down
30 changes: 30 additions & 0 deletions src/bundle/Resources/translations/ibexa_menu.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@
<target state="new">Save and close</target>
<note>key: content_type_group_edit__sidebar_right__save_and_close</note>
</trans-unit>
<trans-unit id="02575ef0a9b0b7830fdaedc7ef55f20ed9a58a77" resname="copy_form__sidebar_right__cancel">
<source>Discard</source>
<target state="new">Discard</target>
<note>key: copy_form__sidebar_right__cancel</note>
</trans-unit>
<trans-unit id="e09060b71f77de9de7ea7f7758637b6138843433" resname="copy_form__sidebar_right__copy">
<source>Copy</source>
<target state="new">Copy</target>
<note>key: copy_form__sidebar_right__copy</note>
</trans-unit>
<trans-unit id="e6d711b654cc77e353dd44ec2ac0403cc6363379" resname="create_form__sidebar_right__cancel">
<source>Discard</source>
<target state="new">Discard</target>
<note>key: create_form__sidebar_right__cancel</note>
</trans-unit>
<trans-unit id="a4bbf87eeab7a1d31fdf3f517b5240ba1d8dd573" resname="create_form__sidebar_right__create">
<source>Save and close</source>
<target state="new">Save and close</target>
<note>key: create_form__sidebar_right__create</note>
</trans-unit>
<trans-unit id="65804c068820b5fa679357bc65c5862d0fc90701" resname="language_create__sidebar_right__cancel">
<source>Discard</source>
<target state="new">Discard</target>
Expand Down Expand Up @@ -481,6 +501,16 @@
<target state="new">Empty Trash</target>
<note>key: trash__sidebar_right__empty_trash</note>
</trans-unit>
<trans-unit id="b450183446de8118e16cebbd17b065bf55767c35" resname="update_form__sidebar_right__cancel">
<source>Discard</source>
<target state="new">Discard</target>
<note>key: update_form__sidebar_right__cancel</note>
</trans-unit>
<trans-unit id="e64b83da92e68d70aa32dd8bf1dedaf154d39a7d" resname="update_form__sidebar_right__update">
<source>Save and close</source>
<target state="new">Save and close</target>
<note>key: update_form__sidebar_right__update</note>
</trans-unit>
<trans-unit id="5ded8d45683caa8bb19649d5cc39bd76ebabfa63" resname="url_edit__sidebar_right__cancel">
<source>Discard changes</source>
<target state="new">Discard changes</target>
Expand Down
7 changes: 3 additions & 4 deletions src/contracts/Menu/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Ibexa\Contracts\AdminUi\Menu;

use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
use Ibexa\AdminUi\Menu\MenuItemFactory;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\Event;
Expand All @@ -19,17 +18,17 @@
*/
abstract class AbstractBuilder
{
/** @var \Ibexa\AdminUi\Menu\MenuItemFactory */
/** @var \Ibexa\Contracts\AdminUi\Menu\MenuItemFactoryInterface */
protected $factory;

/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */
protected $eventDispatcher;

/**
* @param \Ibexa\AdminUi\Menu\MenuItemFactory $factory
* @param \Ibexa\Contracts\AdminUi\Menu\MenuItemFactoryInterface $factory
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
*/
public function __construct(MenuItemFactory $factory, EventDispatcherInterface $eventDispatcher)
public function __construct(MenuItemFactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
{
$this->factory = $factory;
$this->eventDispatcher = $eventDispatcher;
Expand Down
125 changes: 125 additions & 0 deletions src/contracts/Menu/AbstractFormContextMenuBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\AdminUi\Menu;

use JMS\TranslationBundle\Model\Message;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

abstract class AbstractFormContextMenuBuilder extends AbstractBuilder
{
private string $formName;

public function __construct(
MenuItemFactoryInterface $factory,
EventDispatcherInterface $eventDispatcher,
string $formName
) {
parent::__construct($factory, $eventDispatcher);

$this->formName = $formName;
}

/**
* @return string should be lowercase alphanumeric + underscore (a-zA-Z0-9_) string
*/
abstract protected static function getSidebarType(): string;

protected static function getSidebarActionMessage(): string
{
return 'Submit';
}

protected function getConfigureEventName(): string
{
return sprintf(
'ibexa_admin_ui.menu_configure.%s_%s_sidebar_right',
static::getSidebarType(),
$this->formName,
);
}

/**
* @param array<string,mixed> $options
*/
protected function createStructure(array $options): ItemInterface
{
/** @var \Knp\Menu\ItemInterface|\Knp\Menu\ItemInterface[] $menu */
$menu = $this->factory->createItem('root');

$menu->addChild(
$this->createMenuItem(
$this->getActionItemId(),
[
'label' => self::getActionLabel(),
'attributes' => [
'class' => 'ibexa-btn--trigger',
'data-click' => $options['submit_selector'] ?? '#',
],
'translation_domain' => 'ibexa_menu',
]
)
);

$menu->addChild(
$this->createMenuItem(
$this->getCancelItemId(),
[
'label' => self::getCancelLabel(),
'route' => $options['cancel_route'] ?? null,
'routeParameters' => $options['cancel_route_params'] ?? [],
'translation_domain' => 'ibexa_menu',
]
)
);

return $menu;
}

private function getActionItemId(): string
{
return sprintf('%s__sidebar_right__%s', $this->formName, static::getSidebarType());
}

private function getCancelItemId(): string
{
return sprintf('%s__sidebar_right__cancel', $this->formName);
}

/**
* @return \JMS\TranslationBundle\Model\Message[]
*/
public static function getTranslationMessages(): array
{
$actionLabel = self::getActionLabel();
$cancelLabel = self::getCancelLabel();

return [
(new Message($actionLabel, 'ibexa_menu'))->setDesc(static::getSidebarActionMessage()),
(new Message($cancelLabel, 'ibexa_menu'))->setDesc('Discard'),
];
}

private static function getActionLabel(): string
{
return sprintf(
'%s_form__sidebar_right__%s',
static::getSidebarType(),
static::getSidebarType(),
);
}

private static function getCancelLabel(): string
{
return sprintf(
'%s_form__sidebar_right__cancel',
static::getSidebarType(),
);
}
}
27 changes: 27 additions & 0 deletions src/contracts/Menu/CopyFormContextMenuBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\AdminUi\Menu;

use JMS\TranslationBundle\Translation\TranslationContainerInterface;

/**
* Builds menu with "Copy" nad "Cancel" items.
*/
final class CopyFormContextMenuBuilder extends AbstractFormContextMenuBuilder implements TranslationContainerInterface
{
protected static function getSidebarType(): string
{
return 'copy';
}

protected static function getSidebarActionMessage(): string
{
return 'Copy';
}
}
27 changes: 27 additions & 0 deletions src/contracts/Menu/CreateFormContextMenuBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\AdminUi\Menu;

use JMS\TranslationBundle\Translation\TranslationContainerInterface;

/**
* Builds menu with "Create" nad "Cancel" items.
*/
final class CreateFormContextMenuBuilder extends AbstractFormContextMenuBuilder implements TranslationContainerInterface
{
protected static function getSidebarType(): string
{
return 'create';
}

protected static function getSidebarActionMessage(): string
{
return 'Save and close';
}
}
23 changes: 23 additions & 0 deletions src/contracts/Menu/MenuItemFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace Ibexa\Contracts\AdminUi\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;

interface MenuItemFactoryInterface extends FactoryInterface
{
/**
* Creates Location menu item only when user has content:read permission.
*
* @param array<mixed> $options
*
* @return \Knp\Menu\ItemInterface|null
*/
public function createLocationMenuItem(string $name, int $locationId, array $options = []): ?ItemInterface;
}
27 changes: 27 additions & 0 deletions src/contracts/Menu/UpdateFormContextMenuBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\AdminUi\Menu;

use JMS\TranslationBundle\Translation\TranslationContainerInterface;

/**
* Builds menu with "Update" nad "Cancel" items.
*/
final class UpdateFormContextMenuBuilder extends AbstractFormContextMenuBuilder implements TranslationContainerInterface
{
protected static function getSidebarType(): string
{
return 'update';
}

protected static function getSidebarActionMessage(): string
{
return 'Save and close';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace Ibexa\AdminUi\Menu\Admin\ContentType;

use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
use Ibexa\AdminUi\Menu\MenuItemFactory;
use Ibexa\Contracts\AdminUi\Menu\AbstractBuilder;
use Ibexa\Contracts\AdminUi\Menu\MenuItemFactoryInterface;
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
Expand All @@ -33,7 +33,7 @@ class ContentTypeGroupCreateRightSidebarBuilder extends AbstractBuilder implemen
private $translator;

public function __construct(
MenuItemFactory $factory,
MenuItemFactoryInterface $factory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace Ibexa\AdminUi\Menu\Admin\ContentType;

use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
use Ibexa\AdminUi\Menu\MenuItemFactory;
use Ibexa\Contracts\AdminUi\Menu\AbstractBuilder;
use Ibexa\Contracts\AdminUi\Menu\MenuItemFactoryInterface;
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
Expand All @@ -33,7 +33,7 @@ class ContentTypeGroupEditRightSidebarBuilder extends AbstractBuilder implements
private $translator;

public function __construct(
MenuItemFactory $factory,
MenuItemFactoryInterface $factory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace Ibexa\AdminUi\Menu\Admin\Language;

use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
use Ibexa\AdminUi\Menu\MenuItemFactory;
use Ibexa\Contracts\AdminUi\Menu\AbstractBuilder;
use Ibexa\Contracts\AdminUi\Menu\MenuItemFactoryInterface;
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
Expand All @@ -33,7 +33,7 @@ class LanguageCreateRightSidebarBuilder extends AbstractBuilder implements Trans
private $translator;

public function __construct(
MenuItemFactory $factory,
MenuItemFactoryInterface $factory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace Ibexa\AdminUi\Menu\Admin\Language;

use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
use Ibexa\AdminUi\Menu\MenuItemFactory;
use Ibexa\Contracts\AdminUi\Menu\AbstractBuilder;
use Ibexa\Contracts\AdminUi\Menu\MenuItemFactoryInterface;
use Ibexa\Contracts\Core\Repository\Exceptions as ApiExceptions;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
Expand All @@ -33,7 +33,7 @@ class LanguageEditRightSidebarBuilder extends AbstractBuilder implements Transla
private $translator;

public function __construct(
MenuItemFactory $factory,
MenuItemFactoryInterface $factory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator
) {
Expand Down
Loading

0 comments on commit e3594d0

Please sign in to comment.