Hello, I’m Julian

Symfony Sortable I18n-field in Admin-List


Works with Symfony 1.0. This Article shows how to make a I18n Field in a Symfony-Admin-List Sortable. In this example i use the "name"-field as I18n-field.

First, open the file apps/[APPNAME]/modules/[MODULENAME]/action/actions.class.php and add the function:

    protected function addSortCriteria($c)
      {
        if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/[MODULENAME]/sort'))
         {
           if ($sort_column=='name'){      
             if (strlen(sfContext::getInstance()->getUser()->getCulture())==2) {
               $culture=sfContext::getInstance()->getUser()->getCulture();
            }else{
              $culture="de";
            }
            $culture=strtolower($culture);        
            $c->addJoin([MODULENAME]I18nPeer::ID,[MODULENAME]Peer::ID);
            $c->add([MODULENAME]I18nPeer::CULTURE, $culture);
            $sort_column=[MODULENAME]I18nPeer::NAME;
          }else{
            $sort_column = [MODULENAME]Peer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
          }
          if ($this->getUser()->getAttribute('type', null, 'sf_admin/[MODULENAME]/sort') == 'asc')
          {
            $c->addAscendingOrderByColumn($sort_column);
          }
          else
          {
            $c->addDescendingOrderByColumn($sort_column);
          }
        }
      }

Create a __toString function in /lib/model/[MODULENAME].php:

    public function __toString () {
      if (strlen(sfContext::getInstance()->getUser()->getCulture())==2) {
        $culture=sfContext::getInstance()->getUser()->getCulture();
      }else{
        $culture="de";
      }
      $culture=strtolower($culture);
      $culture[0]=strtoupper($culture[0]);
      return $this->__call("getNameI18n".$culture,"");
    }
    public function __call($m, $a)
    {
      $data = @split('I18n', $m, 2);
      if( count($data) != 2 )
        throw new Exception('Tried to call unknown method '.get_class($this).'::'.$m);
      list( $method, $culture ) = $data;
      $this->setCulture( $culture );
      return call_user_func_array(array($this, $method), $a);
    }

Next, create a File "_name.php" in apps/[APPNAME]/modules/[MODULENAME]/templates/_name.php:

    <?php
      echo $[MODELNAME]->__toString();
    ?>

In generator.yml add the _name-field to the display section in list:

    ...
    list:
      display: [_name, ... ]
    ...

Next, copy the _list_th_tabular.php file from the cache-directory to your apps/[APPNAME]/modules/[MODULENAME]/templates directory and change the -tag of the name-field to something like this:

    ...
    <th id="sf_admin_list_th_name">
            <?php if ($sf_user->getAttribute('sort', null, 'sf_admin/[MODULENAME]/sort') == 'name'): ?>
          <?php echo link_to(__('Name'), '[MODULENAME]/list?sort=name&type='.($sf_user->getAttribute('type', 'asc', 'sf_admin/[MODULENAME]/sort') == 'asc' ? 'desc' : 'asc')) ?>
          (<?php echo __($sf_user->getAttribute('type', 'asc', 'sf_admin/[MODULENAME]/sort')) ?>)
          <?php else: ?>
          <?php echo link_to(__('Name'), '[MODULENAME]/list?sort=name&type=asc') ?>
          <?php endif; ?>        
    </th>
    ...

That's it

Date: Mon, 18 May 2009 11:50:00
Author: Julian Stricker

PREVIOUS NEXT

Blog Comments powered by Disqus.