Skip to content

Commit

Permalink
Reuse propertyAccessor inside AdminHelper. Fix CS. Fixes after rebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed Mar 9, 2016
1 parent fca0393 commit 33d9692
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
34 changes: 17 additions & 17 deletions Admin/AdminHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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.
Expand Down Expand Up @@ -118,7 +118,7 @@ public function appendFormFieldElement(AdminInterface $admin, $subject, $element
//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);
Expand Down Expand Up @@ -249,7 +249,7 @@ protected function getEntityClassName(AdminInterface $admin, $elements)
}

/**
* 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
Expand All @@ -260,7 +260,7 @@ protected function getEntityClassName(AdminInterface $admin, $elements)
*/
public function getElementAccessPath($elementId, $entity)
{
$propertyAccessor = new PropertyAccessor();
$propertyAccessor = $this->pool->getPropertyAccessor();

$idWithoutUniqueIdentifier = implode('_', explode('_', substr($elementId, strpos($elementId, '_') + 1)));

Expand Down Expand Up @@ -312,29 +312,29 @@ public function getElementAccessPath($elementId, $entity)
}

/**
* 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;
}
}
}
2 changes: 1 addition & 1 deletion Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class Pool
{
/**
* @var ContainerInterface|null
* @var ContainerInterface
*/
protected $container;

Expand Down
3 changes: 2 additions & 1 deletion Form/ChoiceList/ModelChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ protected function load($choices)
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);
}
}

Expand Down
23 changes: 13 additions & 10 deletions Resources/doc/reference/action_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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. |
Expand Down Expand Up @@ -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...)
Expand Down

0 comments on commit 33d9692

Please sign in to comment.