-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove controller manipulation from EventListener/ControllerListener
Add customAction which handles all custom actions (and calls the right method based on provided action name) Generate attempt, execute, success and error methods for each action. Rename action.php.twig.twig => action.php.twig Remove unnecesary setVariables from CutomBuilderTemplate.php
- Loading branch information
Showing
5 changed files
with
120 additions
and
79 deletions.
There are no files selected for viewing
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
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
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
98 changes: 98 additions & 0 deletions
98
Resources/templates/CommonAdmin/CustomAction/action.php.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{% use '../CommonAdmin/security_action.php.twig' %} | ||
{% use '../CommonAdmin/csrf_protection.php.twig' %} | ||
{% block attemptAction %} | ||
/** | ||
* This function handles common actions behaviour like | ||
* checking CSRF protection token or credentials. | ||
* | ||
* To customize your action look into: | ||
* execute{{ action.name|capitalize }}() - holds action logic | ||
* success{{ action.name|capitalize }}() - called if action was successfull | ||
* error{{ action.name|capitalize }}() - called if action errored | ||
*/ | ||
public function attempt{{ action.name|capitalize }}($pk) | ||
{ | ||
try { | ||
${{ builder.ModelClass }} = $this->getObject($pk); | ||
|
||
{{ block('security_action_with_object') }} | ||
|
||
{% if action.csrfProtected %} | ||
if ('POST' == $this->get('request')->getMethod()) { | ||
{{ block('csrf_action_check_token') }} | ||
{% endif %} | ||
$this->execute{{ action.name|capitalize }}(${{ builder.ModelClass }}); | ||
|
||
return $this->success{{ action.name|capitalize }}(${{ builder.ModelClass }}); | ||
{% if action.csrfProtected %} | ||
} | ||
{% endif %} | ||
} catch (\Exception $e) { | ||
return $this->error{{ action.name|capitalize }}(${{ builder.ModelClass }}, $e); | ||
} | ||
|
||
return $this->render('{{ namespace_prefix }}{{ bundle_name }}:{{ builder.BaseGeneratorName }}Custom:index.html.twig', $this->getAdditionalRenderParameters(${{ builder.ModelClass }}) + array( | ||
"{{ builder.ModelClass }}" => ${{ builder.ModelClass }}, | ||
"title" => $this->get('translator')->trans( | ||
"{{ action.options.title|default("action.options.title") }}", | ||
array('%name%' => '{{ action.name }}'), | ||
'{{ action.options.i18n|default('Admingenerator') }}' | ||
) | ||
)); | ||
} | ||
{% endblock %} | ||
|
||
{% block executeAction %} | ||
/** | ||
* This function is for you to customize what action actually does | ||
*/ | ||
public function execute{{ action.name|capitalize }}(${{ builder.ModelClass }}) | ||
{ | ||
// By default action does nothing | ||
// Overwrite this function! | ||
} | ||
{% endblock %} | ||
|
||
{% block successAction %} | ||
/** | ||
* This is called when action is successfull | ||
* Default behavior is redirecting to list with success message | ||
* | ||
* @param \{{ model }} ${{ builder.ModelClass }} Your \{{ model }} object | ||
* @return Response Must return a response! | ||
*/ | ||
public function success{{ action.name|capitalize }}(${{ builder.ModelClass }}) | ||
{ | ||
$this->get('session')->getFlashBag()->add('success', | ||
$this->get('translator')->trans( | ||
"{{ action.options.success|default("action.options.success") }}", | ||
array('%name%' => '{{ action.name }}'), | ||
'{{ action.options.i18n|default('Admingenerator') }}' | ||
)); | ||
|
||
return new RedirectResponse($this->generateUrl("{{ builder.routePrefixWithSubfolder }}_{{ bundle_name }}{{ builder.BaseGeneratorName ? "_" ~ builder.BaseGeneratorName : "" }}_list")); | ||
} | ||
{% endblock %} | ||
|
||
{% block errorAction %} | ||
/** | ||
* This is called when action throws an exception | ||
* Default behavior is redirecting to list with error message | ||
* | ||
* @param \{{ model }} ${{ builder.ModelClass }} Your \{{ model }} object | ||
* @param \Exception $e Exception | ||
* @return Response Must return a response! | ||
*/ | ||
public function error{{ action.name|capitalize }}(${{ builder.ModelClass }}, $e) | ||
{ | ||
$this->get('session')->getFlashBag()->add('error', | ||
$this->get('translator')->trans( | ||
"{{ action.options.error|default("action.options.error") }}", | ||
array('%name%' => '{{ action.name }}'), | ||
'{{ action.options.i18n|default('Admingenerator') }}' | ||
)); | ||
|
||
return new RedirectResponse($this->generateUrl("{{ builder.routePrefixWithSubfolder }}_{{ bundle_name }}{{ builder.BaseGeneratorName ? "_" ~ builder.BaseGeneratorName : "" }}_list")); | ||
} | ||
{% endblock %} | ||
|
41 changes: 0 additions & 41 deletions
41
Resources/templates/CommonAdmin/CustomAction/action.php.twig.twig
This file was deleted.
Oops, something went wrong.