Skip to content
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

Add image form type #130

Merged
merged 1 commit into from
Jan 9, 2013
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions DependencyInjection/LiipImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@ public function load(array $configs, ContainerBuilder $container)
}

$container->setParameter('liip_imagine.cache.resolver.base_path', $config['cache_base_path']);

$resources = $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : array();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the standard pattern for adding form resources in other Bundles too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, it's done like that in the SymfonyCmfRoutingExtraBundle too. That is the only way i know. Do you know a better one ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see .. i dont know .. i was just wondering :)

/cc @bschussek

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok ;)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah @lsmith77, do you know a better one? :P (which translates to I don't know either)

$resources[] = 'LiipImagineBundle:Form:form_div_layout.html.twig';
$container->setParameter('twig.form.resources', $resources);
}
}
50 changes: 50 additions & 0 deletions Form/Type/ImageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Liip\ImagineBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* ImageType
*
* @author Emmanuel Vella <vella.emmanuel@gmail.com>
*/
class ImageType extends AbstractType
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['image_path'] = $options['image_path'];
$view->vars['image_filter'] = $options['image_filter'];
$view->vars['image_attr'] = $options['image_attr'];
$view->vars['link_url'] = $options['link_url'];
$view->vars['link_attr'] = $options['link_attr'];
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired(array(
'image_path',
'image_filter',
));

$resolver->setDefaults(array(
'image_attr' => array(),
'link_url' => null,
'link_attr' => array(),
));
}

public function getParent()
{
return 'file';
}

public function getName()
{
return 'liip_imagine_image';
}
}
10 changes: 10 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<parameter key="liip_imagine.cache.resolver.web_path.class">Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver</parameter>
<parameter key="liip_imagine.cache.resolver.no_cache.class">Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheResolver</parameter>

<!-- Form types -->

<parameter key="liip_imagine.form.type.image.class">Liip\ImagineBundle\Form\Type\ImageType</parameter>

</parameters>

<services>
Expand Down Expand Up @@ -155,5 +159,11 @@
<argument type="service" id="filesystem" />
</service>

<!-- Form types -->

<service id="liip_imagine.form.type.image" class="%liip_imagine.form.type.image.class%">
<tag name="form.type" alias="liip_imagine_image" />
</service>

</services>
</container>
19 changes: 19 additions & 0 deletions Resources/views/Form/form_div_layout.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% block liip_imagine_image_widget %}
{% spaceless %}
{% if image_path %}
<div>
{% if link_url %}
<a href="{{ link_url }}" {% for attrname, attrvalue in link_attr %}{{ attrname }}="{{ attrvalue }}" {% endfor %}>
{% endif %}

<img src="{{ image_path|imagine_filter(image_filter) }}" {% for attrname, attrvalue in image_attr %}{{ attrname }}="{{ attrvalue }}" {% endfor %} />

{% if link_url %}
</a>
{% endif %}
</div>
{% endif %}

{{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock %}