예제 #1
0
 public function testBcDefaultFormatter()
 {
     $formatter = new RawFormatter();
     $env = $this->getMock('\\Twig_Environment');
     $pool = new Pool();
     $pool->add('foo', $formatter, $env);
     $this->assertSame('foo', $pool->getDefaultFormatter());
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if (is_array($options['format_field'])) {
         list($formatField, $formatPropertyPath) = $options['format_field'];
         $options['format_field_options']['property_path'] = $formatPropertyPath;
     } else {
         $formatField = $options['format_field'];
         $options['format_field_options']['property_path'] = $formatField;
     }
     $options['format_field_options']['data'] = $this->pool->getDefaultFormatter();
     if (is_array($options['source_field'])) {
         list($sourceField, $sourcePropertyPath) = $options['source_field'];
         $options['source_field_options']['property_path'] = $sourcePropertyPath;
     } else {
         $sourceField = $options['source_field'];
         $options['source_field_options']['property_path'] = $sourceField;
     }
     $builder->add($formatField, 'choice', $options['format_field_options']);
     // If there's only one possible format, do not display the choices
     $formatChoices = $builder->get($formatField)->getOption('choices');
     if (count($formatChoices) === 1) {
         // Remove the choice field
         unset($options['format_field_options']['choices']);
         $builder->remove($formatField);
         // Replace it with an hidden field
         $builder->add($formatField, 'hidden', $options['format_field_options']);
     }
     $builder->add($sourceField, 'textarea', $options['source_field_options']);
     /*
      * The listener option only work if the source field is after the current field
      */
     if ($options['listener']) {
         if (!$options['event_dispatcher'] instanceof EventDispatcherInterface) {
             throw new \RuntimeException('The event_dispatcher option must be an instance of EventDispatcherInterface');
         }
         $listener = new FormatterListener($this->pool, $options['format_field_options']['property_path'], $options['source_field_options']['property_path'], $options['target_field']);
         $options['event_dispatcher']->addListener(FormEvents::SUBMIT, array($listener, 'postSubmit'));
     }
 }