Code snippets for symfony 1.x


Conditional object actions for the admin generator

The list view of the admin generator currently always displays all defined object actions for each object. There is no way to display an object action for an object only if some condition on this object is met.

In order to extend the admin generator with this functionality only a small enhancement is required. You can either apply this change per module or create a new admin generator theme as described in the Symfony book.

The templates/_list_td_actions.php has to be extended to look roughly like this, depending on whether you already have your own modifications in there:

<?php if ($this->getParameterValue('list.object_actions')): ?>
<ul class="sf_admin_td_actions">
<?php foreach ($this->getParameterValue('list.object_actions') as $actionName => $params): ?>
  <?php if ( isset( $params['condition'] ) ): ?>
    [?php if ( <?php echo ( isset( $params['condition']['invert'] ) && $params['condition']['invert'] ? '!' : '') . '$' . $this->getSingularName(  ) . '->' . $params['condition']['function'] ?>( <?php echo ( isset( $params['condition']['params'] ) ? $params['condition']['params'] : '' ) ?> ) ):  ?]
  <?php endif; ?>
      <?php echo $this->addCredentialCondition($this->getLinkToAction($actionName, $params, true), $params) ?>
  <?php if ( isset( $params['condition'] ) ): ?>
    [?php endif; ?]
  <?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>

With this enhancement you can now use conditions for your actions in the generator.yml. The syntax should be pretty self-explanatory. An example would look like this:

  subscribe:    { name: Notify when changed, action: subscribe, icon: pencil_add.png }
      function: isUserSubscribed
      params:   "$sf_user, 'test'"
      invert:   true

As you can see each object action now also takes a condition parameter, which again takes a number of parameter.


by Georg Sorst on 2007-12-07, tagged actions  admin  condition  conditional  generator  object 

Comments on this snippet

gravatar icon
#1 excessive demon on 2008-12-17 at 02:34

excessive demon ?? :-O

gravatar icon
#2 Daniel Zint on 2009-05-14 at 09:58

How did you get this snippet to work? I always get the error "Call to undefined method sfPartialView::getParameterValue." I have overwritten the _list_td_actions.php of the module with the code listet above - also modified the generator.yml properly. What could be wrong? Thank you!

gravatar icon
#3 alcaeus on 2009-07-18 at 11:40

Works like a charm in my custom theme - thanks a lot!

gravatar icon
#4 Kevin White on 2009-10-20 at 06:22

This looks like it would address a need I have at the moment. However, I am running into the same problem as Daniel. Any clues as to what I might be doing wrong?

gravatar icon
#5 Vitali Carbivnicii on 2010-02-25 at 03:25

Thanks! It worked just fine for me :)

gravatar icon
#6 David Gomez on 2010-04-22 at 03:56

Same problem as Daniel working in a fix...

gravatar icon
#7 Florian Vilpoix on 2010-08-04 at 11:40

The snippet doesnot work with Symfony 1.4. You must changed it like this :

Works only with a generator theme : // list_td_actions.php <td> <?php if ($this->configuration->getValue('list.object_actions')): ?> <ul class="sf_admin_td_actions"> <?php foreach ($this->configuration->getValue('list.object_actions') as $name => $params): ?> <?php if ( isset( $params['condition'] ) ): ?> [?php if ( <?php echo ( isset( $params['condition']['invert'] ) && $params['condition']['invert'] ? '!' : '') . '$' . $this->getSingularName( ) . '->' . $params['condition']['function'] ?>( <?php echo ( isset( $params['condition']['params'] ) ? $params['condition']['params'] : '' ) ?> ) ): ?] <?php endif; ?> <li class="sf_admin_action<?php echo $params['class_suffix'] ?>"> <?php echo $this->addCredentialCondition($this->getLinkToAction($name, $params, true), $params) ?> </li> <?php if ( isset( $params['condition'] ) ): ?> [?php endif; ?] <?php endif; ?> <?php endforeach; ?> </ul> <?php endif; ?> </td>

// generator.yml : config: subscribe: { name: Notify when changed, action: subscribe, icon: pencil_add.png, condition: { function: isUserSubscribed, params: "$sf_user, 'test'", invert: true } }

gravatar icon
#8 Wojciech Tekiela on 2010-12-15 at 12:28

Doesn't work for me. In symfony 1.4 with Doctrine after implementing this snippet I've got Undefined property: sfPartialView::$configuration error. Any ideas?

gravatar icon
#9 Florian Vilpoix on 2011-01-13 at 10:49

You have to use an admin generator template : For this, copy the directory lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin into a new directory into lib/data/generator/sfDoctrineModule/yourTemplate

Then in your module generator.yml : theme: yourTemplate

Replace the content of lib/data/generator/sfDoctrineModule/yourTemplate/template/templates/_list_td_actions.php with my previous post

clear cache, and enjoy :)

gravatar icon
#10 Samrat Gavale on 2012-06-27 at 02:20

Florian's solution works for me. Thanks a lot!