Customizing actions in symfony's admin generator
A quick tip about symfony's admin generator: if you want to have a custom action, with the following generator.yml bit for example (actions prefixed with an underscore are builtin admin-gen actions):
form:
actions:
_delete: ~
_cancel: ~
_save: ~
_save_and_add: ~
custom: ~
You can totally customize the link generated for this action (to add javascript for example) using the generator helper in your module's lib/ directory:
[php]
public function linkToCustom($object, $params)
{
return '<a href="#" onclick="console.log(\'woohoo\'); return false;">log woohoo</a>';
}
You get passed the current object as an argument, and whatever parameters you passed in the generator.yml:
custom: { label: 'WOOHOO' }
[php]
public function linkToCustom($object, $params)
{
return '<a href="'.url_for($this->getUrlForAction('custom')).'" onclick="console.log(\'woohoo\'); return false;">'.__($params['label']).'</a>';
}
Also, as you can see that standard helpers are available too.
Comments
Amazing - you must have the power to read my mind - I was trying to work out how to add an alert js box to the action.
Nice one.
John
mmm on symfony 1.2.4 object actions do not call linkToCustom()
Instead it just adds a normal link_to in the list_td_actions template:
<?php echo link_to(__('Custom'), 'load/departLoad?id='.$load->getId(), array(), 'messages') ?>
Hi John, this tip only works for "form" actions, that is actions added to the "edit" and "new" pages. For actions in the list page, you have to use the "params" option:
they are passed as the third arguments of the link_to call.
Thanks Geoffrey that worked a treat!
This worked for me:
object_actions: _edit: ~ _delete: ~ emailPassword: name: Email Password action: emailPassword params: onclick="if (confirm('Are you sure you want to send an email to this user containing login information?') == 0){ return false; }"Chez moi j'avais mon lien "Annuler" qui pointait vers l'url "monapp//List_Cancel"... Problématique non ?
Ca venait du fait que l'action "cancel" (dans le generator.yml) n'est plus utilisée, mais c'est "list" à la place.
Cordialement,
Hi:
config:
actions: ~ fields: description: ~ lastrun: label: 'Last run on' list: title: 'Email configurations' display: [ description, enabled, lastrun, destinataries, _scheduling, _includedReports ] filter: display: [ description, _enabled ] form: display: [description, enabled, destinataries, _time, _schedulingEditable] edit: title: 'Editing email configuration' actions: _delete: ~ _cancel: ~ _save: { label: Save, action: saveConfiguration } new: actions: saveConfiguration: { label: Save, action: saveConfiguration } _save: ~I have a couple of problems:
1 - The cancel buttons renders as a link pointing to a route "/backend_dev.php/email/3/List_cancel" which doesn't exist.
2 - I can't seem to get a hold of the save action which gets triggered upon pressing the button.