浏览代码

Fix #189 - the bundle assumes __toString for related entities

Thomas Rabaix 14 年之前
父节点
当前提交
9f37a2ebd6

+ 3 - 0
Admin/BaseFieldDescription.php

@@ -30,6 +30,9 @@ use Sonata\AdminBundle\Admin\AdminInterface;
  *   - name (o) : the name used (label in the form, title in the list)
  *   - link_parameters (o) : add link parameter to the related Admin class when
  *                           the Admin.generateUrl is called
+ *   - code : the method name to retrieve the related value
+ *   - associated_tostring : the method to retrieve the "string" representation
+ *                           of the collection element.
  *
  * Form Field options :
  *   - form_field_type (o): the widget class to use to render the field

+ 1 - 1
Resources/views/CRUD/edit_orm_many_to_one.html.twig

@@ -14,7 +14,7 @@ file that was distributed with this source code.
 {% block field %}
 
     {% if not field_description.hasassociationadmin%}
-        {{ value }}
+        {{ value|render_relation_element(field_description) }}
     {% elseif field_description.options.edit == 'inline' %}
         {% for nested_field_description in field_description.associationadmin.formfielddescriptions %}
             {{ nested_field_description|render_form_element(field_element, value) }}

+ 3 - 1
Resources/views/CRUD/edit_orm_one_to_many.html.twig

@@ -13,7 +13,9 @@ file that was distributed with this source code.
 
 {% block field %}
     {% if not field_description.hasassociationadmin%}
-        {# TODO : add logic here #}
+        {% for element in value%}
+            {{ element|render_relation_element(field_description) }}
+        {% endfor %}
     {% else %}
         <div id="field_container_{{ field_element.vars.id }}">
             <span id="field_widget_{{ field_element.vars.id }}" >

+ 1 - 3
Resources/views/CRUD/edit_orm_one_to_one.html.twig

@@ -13,14 +13,12 @@ file that was distributed with this source code.
 
 {% block field %}
     {% if not field_description.hasassociationadmin%}
-        {# TODO : add some logic here #}
+        {{ value|render_relation_element(field_description) }}
     {% elseif field_description.options.edit == 'inline' %}
         {% for nested_field_description in field_description.associationadmin.formfielddescriptions %}
             {{ nested_field_description|render_form_element(field_element, value) }}
         {% endfor %}
-
     {% else %}
-
         <div id="field_container_{{ field_element.vars.id }}">
 
             {% if field_description.options.edit == 'list' %}

+ 9 - 1
Resources/views/CRUD/list_orm_many_to_many.html.twig

@@ -12,5 +12,13 @@ file that was distributed with this source code.
 {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
 
 {% block field%}
-
+    {% if field_description.hasassociationadmin%}
+        {% for element in value%}
+            <a href="{{ field_description.associationadmin.generateUrl('edit', {'id': field_description.associationadmin.id(element)}) }}">{{ element|render_relation_element(field_description) }}</a>
+        {% endfor %}
+    {% else %}
+        {% for element in value%}
+            {{ element|render_relation_element(field_description) }}
+        {% endfor %}
+    {% endif %}
 {% endblock %}

+ 1 - 1
Resources/views/CRUD/list_orm_many_to_one.html.twig

@@ -16,7 +16,7 @@ file that was distributed with this source code.
         {% if field_description.hasAssociationAdmin and field_description.associationadmin.isGranted('EDIT')%}
             <a href="{{ field_description.associationadmin.generateUrl('edit', {'id': field_description.associationadmin.id(value)}) }}">{{ value }}</a>
         {% else %}
-            {{ value }}
+            {{ value|render_relation_element(field_description) }}
         {% endif %}
     {% endif %}
 {% endblock %}

+ 9 - 1
Resources/views/CRUD/list_orm_one_to_many.html.twig

@@ -12,5 +12,13 @@ file that was distributed with this source code.
 {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
 
 {% block field%}
-
+    {% if field_description.hasassociationadmin%}
+        {% for element in value%}
+            <a href="{{ field_description.associationadmin.generateUrl('edit', {'id': field_description.associationadmin.id(element)}) }}">{{ element|render_relation_element(field_description) }}</a>
+        {% endfor %}
+    {% else %}
+        {% for element in value%}
+            {{ element|render_relation_element(field_description) }}
+        {% endfor %}
+    {% endif %}
 {% endblock %}

+ 1 - 2
Resources/views/CRUD/list_orm_one_to_one.html.twig

@@ -12,12 +12,11 @@ file that was distributed with this source code.
 {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
 
 {% block field%}
-
     {% if field_description.hasAssociationAdmin and field_description.associationadmin.id(value) %}
         {% if field_description.hasAssociationAdmin and field_description.associationadmin.isGranted('EDIT')%}
             <a href="{{ field_description.associationadmin.generateUrl('edit', {'id': field_description.associationadmin.id(value)}) }}">{{ value }}</a>
         {% else %}
-            {{ value }}
+            {{ value|render_relation_element(field_description) }}
         {% endif %}
     {% endif %}
 {% endblock %}

+ 22 - 4
Twig/Extension/SonataAdminExtension.php

@@ -40,10 +40,11 @@ class SonataAdminExtension extends \Twig_Extension
     public function getFilters()
     {
         return array(
-            'render_list_element'    => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
-            'render_form_element'    => new \Twig_Filter_Method($this, 'renderFormElement', array('is_safe' => array('html'))),
-            'render_filter_element'  => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
-            'render_view_element'    => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
+            'render_list_element'     => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
+            'render_form_element'     => new \Twig_Filter_Method($this, 'renderFormElement', array('is_safe' => array('html'))),
+            'render_filter_element'   => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
+            'render_view_element'     => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
+            'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement', array('is_safe' => array('html'))),
         );
     }
 
@@ -245,4 +246,21 @@ class SonataAdminExtension extends \Twig_Extension
             'base_template'     => $fieldDescription->getOption('base_template', $base_template)
         )));
     }
+
+    /**
+     * @throws \RunTimeException
+     * @param $element
+     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
+     * @return mixed
+     */
+    public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
+    {
+        $method = $fieldDescription->getOption('associated_tostring', '__toString');
+
+        if (!method_exists($element, $method)) {
+            throw new \RunTimeException(sprintf('You must define an `associated_tostring` option or create a `%s::__toString` method to the field option %s from service %s is ', get_class($element), $fieldDescription->getName(), $fieldDescription->getAdmin()->getCode()));
+        }
+
+        return call_user_func(array($element, $method));
+    }
 }