Skip to content

Minor Corrections and Additional check to prevent unnecessary exception #1905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions cookbook/form/create_form_type_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ tag:

services:
acme_demo_bundle.image_type_extension:
class: Acme\DemoBundle\Form\Type\ImageTypeExtension
class: Acme\DemoBundle\Form\Extension\ImageTypeExtension
tags:
- { name: form.type_extension, alias: file }

.. code-block:: xml

<service id="acme_demo_bundle.image_type_extension" class="Acme\DemoBundle\Form\Type\ImageTypeExtension">
<service id="acme_demo_bundle.image_type_extension" class="Acme\DemoBundle\Form\Extension\ImageTypeExtension">
<tag name="form.type_extension" alias="file" />
</service>

.. code-block:: php

$container
->register('acme_demo_bundle.image_type_extension', 'Acme\DemoBundle\Form\Type\ImageTypeExtension')
->register('acme_demo_bundle.image_type_extension', 'Acme\DemoBundle\Form\Extension\ImageTypeExtension')
->addTag('form.type_extension', array('alias' => 'file'));

The ``alias`` key of the tag is the type of field that this extension should
Expand Down Expand Up @@ -186,7 +186,6 @@ it in the view::
namespace Acme\DemoBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Util\PropertyPath;
Expand Down Expand Up @@ -219,16 +218,22 @@ it in the view::
*
* @param \Symfony\Component\Form\FormView $view
* @param \Symfony\Component\Form\FormInterface $form
* @param array $options
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (array_key_exists('image_path', $options)) {
$parentData = $form->getParent()->getData();

$propertyPath = new PropertyPath($options['image_path']);
$imageUrl = $propertyPath->getValue($parentData);
if (null !== $parentData) {
$propertyPath = new PropertyPath($options['image_path']);
$imageUrl = $propertyPath->getValue($parentData);
} else {
$imageUrl = null;
}

// set an "image_url" variable that will be available when rendering this field
$view->set('image_url', $imageUrl);
$view->vars['image_url'] = $imageUrl;
}
}

Expand Down