phpdoctrinesymfony-formssymfony-1.4doctrine-query

Customizing layout to sfWidgetFormDoctrineChoice


I am using Symfony 1.4 sfWidgetFormDoctrineChoice

I have added the checkboxes to the form, which pulls the Model data successfully. What I want to do is also include a thumbnail next to the checkbox, along with the title.

$this->setWidget('bulkUploadVideos', new sfWidgetFormDoctrineChoice(array(
    'model' => 'MediaAsset',
    'query' => Doctrine_Query::create()->select('u.url')->from('MediaAsset u')->orderBy('id DESC'),
    'add_empty' => false,
    'multiple' => true,
    'expanded' => true
   )
));

This does a fantastic job of pulling the query into a list of checkboxes arranged like so:

⧠ Greenjeans

⧠ Mr Magoo

⧠ Droopy

In the Media Assets table, I also have an image url that I want to include in the layout. SO it would look like this:

|-img thumbnial- | ⧠ Greenjeans

|-img thumbnail- | ⧠ Mr. Magoo

|-img thumbnial- | ⧠ Droopy

I thought maybe using a formatter class, but I do not see any change in the form.

lib/form/formatters/sfWidgetFormSchemaFormatterAllVideos.class.php

<?php 
class sfWidgetFormSchemaFormatterAllVideos extends sfWidgetFormSchemaFormatter {
  protected
    $rowFormat       = "<span class=\"my-label-class\">%label%</span>\n  <span>%error%%field%%help%%hidden_fields%</span>`n",
    $errorRowFormat  = "<span class=\"my-error-class\" colspan=\"2\">\n%errors%</span>\n",
    $helpFormat      = '<br />%help%',
    $decoratorFormat = "<div class='custom-video-layout'>\n  %content%</div>";
}

and then i put this at the bottom of my MediaAssetsForm.class.php

public function configure() {
    parent::configure();
...
..
...
$this->getWidgetSchema()->setFormFormatterName('AllVideos');

Alas, the page layout looks exactly the same. Am I incorrectly calling the Formatter, or is there a much easier way of doing this?

Which btw, still does not answer the question of how I query the image url from the table into the output for each checkbox. That's the main problem I would like to solve. Thumbnails of each record in the form.


Solution

  • The formatter is used to render the whole form, what you need is to change the rendering of one of the widgets.

    The sfwidgetFormDoctrineChoice has an option renderer which takes a formatter as an argument. The one that you need is the sfWidgetFormSelectCheckbox. What I would do is:

    1. create your own class which will extend the sfWidgetFormSelectCheckbox class. E.g.

      class sfWidgetFormMySelectWithThumbs extends sfWidgetFormSelectCheckbox {
      }
      
    2. Extend the configure function so it takes another option which will hold an array of your thumbnails.

      public function configure($options = array(), $arguments = array()) {
          parent::configure($options, $arguments);
      
          $this->addOption('thumbnails', array());
      } 
      
    3. Change the formatChoices function so it adds the image in front of the checkbox (you can copy and modify the original formatChoices function).

      ...
      $sources = $this->getOption('thumbnails');
      ...
      
      $inputs[$id] = array(
          'input' => sprintf('| %s | %s',
              $this->renderTag('img', array('src' => $sources[$key])),
              $this->renderTag('input', array_merge($baseAttributes, $attributes))
          ),
          'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
      );
      
    4. Use the formatter class in your widget:

       $this->setWidget('bulkUploadVideos', new sfWidgetFormDoctrineChoice(array(
          ...
          'renderer' => new sfWidgetFormMySelectWithThumbs(array('thumbnails' => $thumbanils))
          )
      ));
      

      Of course you need to retrieve the list of thumbnails as an array where the array keys are the same as the id's used for values for the checkboxes, but that shouldn't be an issue.