Browse Source

Reuse propertyAccessor inside AdminHelper. Fix CS. Fixes after rebase.

Konstantin.Myakshin 9 years ago
parent
commit
33d969268d
4 changed files with 33 additions and 29 deletions
  1. 17 17
      Admin/AdminHelper.php
  2. 1 1
      Admin/Pool.php
  3. 2 1
      Form/ChoiceList/ModelChoiceList.php
  4. 13 10
      Resources/doc/reference/action_list.rst

+ 17 - 17
Admin/AdminHelper.php

@@ -19,7 +19,7 @@ use Sonata\AdminBundle\Util\FormViewIterator;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\Form\FormView;
 use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
-use Symfony\Component\PropertyAccess\PropertyAccessor;
+use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
 
 /**
  * Class AdminHelper.
@@ -118,7 +118,7 @@ class AdminHelper
         //Child form not found (probably nested one)
         //if childFormBuilder was not found resulted in fatal error getName() method call on non object
         if (!$childFormBuilder) {
-            $propertyAccessor = new PropertyAccessor();
+            $propertyAccessor = $this->pool->getPropertyAccessor();
             $entity = $admin->getSubject();
 
             $path = $this->getElementAccessPath($elementId, $entity);
@@ -249,7 +249,7 @@ class AdminHelper
     }
 
     /**
-     * get access path to element which works with PropertyAccessor.
+     * Get access path to element which works with PropertyAccessor.
      *
      * @param string $elementId expects string in format used in form id field. (uniqueIdentifier_model_sub_model or uniqueIdentifier_model_1_sub_model etc.)
      * @param mixed  $entity
@@ -260,7 +260,7 @@ class AdminHelper
      */
     public function getElementAccessPath($elementId, $entity)
     {
-        $propertyAccessor = new PropertyAccessor();
+        $propertyAccessor = $this->pool->getPropertyAccessor();
 
         $idWithoutUniqueIdentifier = implode('_', explode('_', substr($elementId, strpos($elementId, '_') + 1)));
 
@@ -312,29 +312,29 @@ class AdminHelper
     }
 
     /**
-     * check if given path exists in $entity.
+     * Check if given path exists in $entity.
      *
-     * @param PropertyAccessor $propertyAccessor
-     * @param mixed            $entity
-     * @param string           $path
+     * @param PropertyAccessorInterface $propertyAccessor
+     * @param mixed                     $entity
+     * @param string                    $path
      *
      * @return bool
      *
      * @throws \RuntimeException
      */
-    private function pathExists(PropertyAccessor $propertyAccessor, $entity, $path)
+    private function pathExists(PropertyAccessorInterface $propertyAccessor, $entity, $path)
     {
-        //sf2 <= 2.3 did not have isReadable method for PropertyAccessor
+        // Symfony <= 2.3 did not have isReadable method for PropertyAccessor
         if (method_exists($propertyAccessor, 'isReadable')) {
             return $propertyAccessor->isReadable($entity, $path);
-        } else {
-            try {
-                $propertyAccessor->getValue($entity, $path);
+        }
 
-                return true;
-            } catch (NoSuchPropertyException $e) {
-                return false;
-            }
+        try {
+            $propertyAccessor->getValue($entity, $path);
+
+            return true;
+        } catch (NoSuchPropertyException $e) {
+            return false;
         }
     }
 }

+ 1 - 1
Admin/Pool.php

@@ -23,7 +23,7 @@ use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
 class Pool
 {
     /**
-     * @var ContainerInterface|null
+     * @var ContainerInterface
      */
     protected $container;
 

+ 2 - 1
Form/ChoiceList/ModelChoiceList.php

@@ -159,7 +159,8 @@ class ModelChoiceList extends SimpleChoiceList
                 try {
                     $value = (string) $entity;
                 } catch (\Exception $e) {
-                    throw new RuntimeException(sprintf('Unable to convert the entity "%s" to string, provide "property" option or implement "__toString()" method in your entity.', ClassUtils::getClass($entity)), 0, $e);
+                    throw new RuntimeException(sprintf('Unable to convert the entity "%s" to string, provide '
+                        .'"property" option or implement "__toString()" method in your entity.', ClassUtils::getClass($entity)), 0, $e);
                 }
             }
 

+ 13 - 10
Resources/doc/reference/action_list.rst

@@ -41,7 +41,7 @@ Customizing the fields displayed on the list page
 -------------------------------------------------
 
 You can customize the columns displayed on the list through the ``configureListFields`` method.
-Here is an example from Sonata E-Commerce Product Admin:
+Here is an example:
 
 .. code-block:: php
 
@@ -71,13 +71,10 @@ Here is an example from Sonata E-Commerce Product Admin:
                 'currency' => $this->currencyDetector->getCurrency()->getLabel()
             ))
 
-            // here we specify which method is used to render the label
+            // Here we specify which property is used to render the label of each entity in the list
             ->add('productCategories', null, array(
-                'associated_property' => 'category'
-            ))
-            ->add('productCollections', null, array(
-                'associated_property' => 'collection'
-            ))
+                'associated_property' => 'name')
+            )
 
             // you may also use dotted-notation to access
             // specific properties of a relation to the entity
@@ -107,8 +104,10 @@ Options
 - ``template`` (o): the template used to render the field
 - ``label`` (o): the name used for the column's title
 - ``link_parameters`` (o): add link parameter to the related Admin class when the ``Admin::generateUrl`` is called
-- ``code`` (o): the method name to retrieve the related value
-- ``associated_tostring`` (o): (deprecated, use ``associated_property`` option) the method to retrieve the "string" representation of the collection element.
+- ``code`` (o): the method name to retrieve the related value (for example,
+  if you have an `array` type field, you would like to show info prettier
+  than `[0] => 'Value'`; useful when simple getter is not enough).
+  Notice: works with string-like types (string, text, html)
 - ``associated_property`` (o): property path to retrieve the "string" representation of the collection element, or a closure with the element as argument and return a string.
 - ``identifier`` (o): if set to true a link appears on the value to edit the element
 
@@ -152,6 +151,10 @@ Available types and associated options
 +-----------+----------------+-----------------------------------------------------------------------+
 | string    |                | Renders a simple string.                                              |
 +-----------+----------------+-----------------------------------------------------------------------+
+| text      |                | See 'string'                                                          |
++-----------+----------------+-----------------------------------------------------------------------+
+| html      |                | Renders string as html                                                |
++-----------+----------------+-----------------------------------------------------------------------+
 | time      |                | Renders a datetime's time with format ``H:i:s``.                      |
 +-----------+----------------+-----------------------------------------------------------------------+
 | trans     | catalogue      | Translates the value with catalogue ``catalogue`` if defined.         |
@@ -510,7 +513,7 @@ To do:
 Visual configuration
 --------------------
 
-You have the possibility to configure your List View to customize the render without overring to whole template.
+You have the possibility to configure your List View to customize the render without overriding to whole template.
 You can :
 
 - `header_style`: Customize the style of header (width, color, background, align...)