Fast hack for not linking empty columns with the href as linktext in a symfony admin generator list

Flattr this
Tweet this: Fast hack for not linking empty columns with the href as linktext in a symfony admin generator list
Dent this: Fast hack for not linking empty columns with the href as linktext in a symfony admin generator list
Datum: 30.09.2010 18:17:09

Using = to make a column linked to the edit-page of an entry in the symfony-admin generator.yml is ugly if an entry's column is empty, because the link_to()-helper will then use the href of the link as link text. If prefere just an empty column.

This small hack requires replacing the default sfDoctrineGenerator with your own implementation and overwriting the renderField-method:

class myDoctrineGenerator extends sfDoctrineGenerator
{
  /**
   * Returns HTML code for a field.
   *
   * @param sfModelGeneratorConfigurationField $field The field
   *
   * @return string HTML code
   */
  public function renderField($field)
  {
    $html = $this->getColumnGetter($field->getName(), true);

    if ($renderer = $field->getRenderer())
    {
      $html = sprintf("$html ? call_user_func_array(%s, array_merge(array(%s), %s)) : ' '", $this->asPhp($renderer), $html, $this->asPhp($field->getRendererArguments()));
    }
    else if ($field->isComponent())
    {
      return sprintf("get_component('%s', '%s', array('type' => 'list', '%s' => \$%s))", $this->getModuleName(), $field->getName(), $this->getSingularName(), $this->getSingularName());
    }
    else if ($field->isPartial())
    {
      return sprintf("get_partial('%s/%s', array('type' => 'list', '%s' => \$%s))", $this->getModuleName(), $field->getName(), $this->getSingularName(), $this->getSingularName());
    }
    else if ('Date' == $field->getType())
    {
      $html = sprintf("false !== strtotime($html) ? format_date(%s, \"%s\") : ' '", $html, $field->getConfig('date_format', 'f'));
    }
    else if ('Boolean' == $field->getType())
    {
      $html = sprintf("get_partial('%s/list_field_boolean', array('value' => %s))", $this->getModuleName(), $html);
    }

    if ($field->isLink())
    {
      // ugly/simple hack to only print a non printing space if the value is empty
      $html = sprintf("''; \$linktitle = $html; if (!empty(\$linktitle)) { echo link_to(%s, '%s', \$%s); } else { echo ' '; }", $html, $this->getUrlForAction('edit'), $this->getSingularName());
    }

    return $html;
  }
}

and you need to change your generator.yml to use the new generator:

generator:
  class: myDoctrineGenerator

Trackbacks (0)

Trackbackurl: http://www.robo47.net/trackback/blogentry/205

Es sind keine Trackbacks vorhanden.


Kommentare (0)

Es sind noch keine Kommentare vorhanden.

Die Kommentare zu diesem Beitrag sind gesperrt.

You liked it ? Link it on your homepage or blog: