Kunstmaan/KunstmaanPagePartBundle — pull request #97 by jockri (2013-10-10 08:43:20+00:00)
Title: allow pagepart FormType definition as string (service definition)

In some cases you want to pass some extra parameters to the pagepart FormType class. The changes in this PR makes this possible. The `getDefaultAdminType` function of the pagepart can now return a FormType object, or the service definition name (string).

Example to illustrate this:

``` php
class MyPagePart
{
// ...
public function getDefaultAdminType()
{
// return new TestPagePartAdminType();
return "mycomp_mybundle.testpageparttype";
}
// ...
}
```
```
mycomp_mybundle.testpageparttype:
class: MyComp\MyBundle\Form\PageParts\TestPagePartAdminType
calls:
- [ setRequest, [@request=] ]
tags:
- { name: form.type, alias: mycomp_mybundle_testpageparttype }
```
``` php
class TestPagePartAdminType extends AbstractPagePart
{
/**
* @var string
*/
private $locale;

/**
* Set the request because we need the locale
*
* @param Request $request
*/
public function setRequest(Request $request = null) {
if ($request instanceof Request) {
$this->locale = $request->getLocale();
}
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);

$builder->add('person', 'entity', array(
'class' => 'MyComp\MyBundle\Entity\Person',
'expanded' => false,
'multiple' => false,
'query_builder' => function(EntityRepository $repository) {
// Only the persons who speak this language
return $repository->getAllPersons($this->locale);
}
));
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'mycomp_mybundle_testpageparttype';
}

// ...
}
```