Skip to content

Commit

Permalink
Add route option to save action (#1160)
Browse files Browse the repository at this point in the history
* Add route option to save action

* Remove router from save action factory
  • Loading branch information
gseidel committed Feb 13, 2021
1 parent 518d4dd commit 5d775d0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion assets/node_modules/@enhavo/app/Action/Model/SaveAction.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 42 additions & 1 deletion src/Enhavo/Bundle/AppBundle/Action/Type/SaveActionType.php
Expand Up @@ -11,10 +11,50 @@

use Enhavo\Bundle\AppBundle\Action\AbstractActionType;
use Enhavo\Bundle\AppBundle\Action\ActionTypeInterface;
use Enhavo\Bundle\AppBundle\Util\ArrayUtil;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class SaveActionType extends AbstractActionType implements ActionTypeInterface
{
/** @var RouterInterface */
private $router;

/**
* CreateAction constructor.
* @param TranslatorInterface $translator
* @param RouterInterface $router
*/
public function __construct(TranslatorInterface $translator, RouterInterface $router)
{
parent::__construct($translator);
$this->router = $router;
}

public function createViewData(array $options, $resource = null)
{
$data = parent::createViewData($options, $resource);

$url = null;
if ($options['route']) {
$url = $this->getUrl($options, $resource);
}

$data = ArrayUtil::merge($data, [
'url' => $url
]);

return $data;
}

private function getUrl(array $options, $resource = null)
{
$parameters['id'] = $resource->getId();
$parameters = array_merge_recursive($parameters, $options['route_parameters']);
return $this->router->generate($options['route'], $parameters);
}

public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
Expand All @@ -24,7 +64,8 @@ public function configureOptions(OptionsResolver $resolver)
'label' => 'label.save',
'translation_domain' => 'EnhavoAppBundle',
'icon' => 'save',
'route' => null
'route' => null,
'route_parameters' => [],
]);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Enhavo/Bundle/AppBundle/Viewer/Viewer/CreateViewer.php
Expand Up @@ -158,8 +158,7 @@ private function createActions($options)
$default = [
'save' => [
'type' => 'save',
'route' => sprintf('%s_%s_create', $metadata->getApplicationName(), $this->getUnderscoreName($metadata)),
],
]
];

return $default;
Expand Down

0 comments on commit 5d775d0

Please sign in to comment.