Skip to content

Commit

Permalink
improve sf3.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Dec 7, 2015
1 parent 5443959 commit ab159e9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ public function setContainer(ContainerInterface $container = null)
*/
protected function configure()
{
$adminCode = $this->container->get('request')->get('_sonata_admin');
$request = $this->getRequest();

$adminCode = $request->get('_sonata_admin');

if (!$adminCode) {
throw new \RuntimeException(sprintf(
'There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`',
get_class($this),
$this->container->get('request')->get('_route')
$request->get('_route')
));
}

Expand All @@ -139,8 +141,6 @@ protected function configure()
$rootAdmin = $rootAdmin->getParent();
}

$request = $this->container->get('request');

$rootAdmin->setRequest($request);

if ($request->get('uniqid')) {
Expand Down
9 changes: 9 additions & 0 deletions DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ public function load(array $configs, ContainerBuilder $container)
'integer' => '',
'datetime' => 'sonata-medium-date',
'date' => 'sonata-medium-date',

// SF3+
'Symfony\Component\Form\Extension\Core\Type\ChoiceType' => '',
'Symfony\Component\Form\Extension\Core\Type\DateType' => 'sonata-medium-date',
'Symfony\Component\Form\Extension\Core\Type\DateTimeType' => 'sonata-medium-date',
'Symfony\Component\Form\Extension\Core\Type\EmailType' => '',
'Symfony\Component\Form\Extension\Core\Type\IntegerType' => '',
'Symfony\Component\Form\Extension\Core\Type\TextareaType' => '',
'Symfony\Component\Form\Extension\Core\Type\TextType' => '',
);

$container->getDefinition('sonata.admin.form.extension.field')
Expand Down
11 changes: 9 additions & 2 deletions Form/Extension/Field/Type/FormTypeFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
protected function getClass(FormBuilderInterface $formBuilder)
{
foreach ($this->getTypes($formBuilder) as $type) {
if (isset($this->defaultClasses[$type->getName()])) {
return $this->defaultClasses[$type->getName()];

if (!method_exists($type, 'getName')) { // SF3.0+
$name = get_class($type);
} else {
$name = $type->getName();
}

if (isset($this->defaultClasses[$name])) {
return $this->defaultClasses[$name];
}
}

Expand Down
3 changes: 2 additions & 1 deletion Resources/views/CRUD/base_edit_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<form
{% if sonata_admin.adminPool.getOption('form_type') == 'horizontal' %}class="form-horizontal"{% endif %}
role="form"
action="{% block sonata_form_action_url %}{{ admin.generateUrl(url, {'id': admin.id(object), 'uniqid': admin.uniqid, 'subclass': app.request.get('subclass')}) }}{% endblock %}" {{ form_enctype(form) }}
action="{% block sonata_form_action_url %}{{ admin.generateUrl(url, {'id': admin.id(object), 'uniqid': admin.uniqid, 'subclass': app.request.get('subclass')}) }}{% endblock %}"
{% if form.vars.multipart %} enctype="multipart/form-data"{% endif %}
method="POST"
{% if not sonata_admin.adminPool.getOption('html5_validate') %}novalidate="novalidate"{% endif %}
>
Expand Down
9 changes: 7 additions & 2 deletions Resources/views/Form/form_admin_fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,20 @@ file that was distributed with this source code.

{% block choice_widget_collapsed %}
{% spaceless %}
{% if required and empty_value is none and not empty_value_in_choices and not multiple %}

{# this line only work with sf<3, as empty_value_in_choices and empty_value are now deprecated#}
{% if required and empty_value is defined and empty_value_in_choices is defined and empty_value is none and not empty_value_in_choices and not multiple %}
{% set required = false %}
{% endif %}

{% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
{% if (sortable is defined) and sortable and multiple %}
{{ block('sonata_type_choice_multiple_sortable') }}
{% else %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} >
{% if empty_value is not none %}

{# this line only work with sf<3, as empty_value_in_choices and empty_value are now deprecated#}
{% if empty_value is defined and empty_value is not none %}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>
{% if not sonata_admin.admin %}
{{- empty_value|trans({}, translation_domain) -}}
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Menu/sonata_menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block root %}
{%- set listAttributes = item.childrenAttributes|merge({'class': 'sidebar-menu'}) %}
{%- set request = item.extra('request') %}
{%- set request = item.extra('request') ?: app.request %}
{{ block('list') -}}
{% endblock %}

Expand Down

0 comments on commit ab159e9

Please sign in to comment.