Skip to content

Commit

Permalink
feature #1857 Added a new "file" type for list/show views (javieregui…
Browse files Browse the repository at this point in the history
…luz)

This PR was merged into the 1.16.x-dev branch.

Discussion
----------

Added a new "file" type for list/show views

This fixes #1714.

Commits
-------

342116b Added a new "file" type for list/show views
  • Loading branch information
javiereguiluz committed Oct 16, 2017
2 parents 77b0265 + 342116b commit 0dd151a
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 11 deletions.
76 changes: 74 additions & 2 deletions doc/book/list-search-show-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ These are the options that you can define for each field:
``boolean``, ``date``, ``datetime``, ``datetimetz``, ``decimal``, ``float``,
``guid``, ``integer``, ``json_array``, ``object``, ``simple_array``,
``smallint``, ``string``, ``text``, ``time``.
* Any of the custom EasyAdmin types: ``email``, ``image``, ``raw``, ``tel``,
``toggle``, ``url`` (they are explained later in this chapter).
* Any of the custom EasyAdmin types: ``email``, ``file``, ``image``, ``raw``,
``tel``, ``toggle``, ``url`` (they are explained later in this chapter).

The fields of the ``list`` and ``search`` views define another option:

Expand Down Expand Up @@ -724,6 +724,78 @@ value for different properties or different views:
The base paths defined for a property always have priority over the one defined
globally for the entity.

File Data Type
~~~~~~~~~~~~~~

If any of your properties stores the URL or path of a file, this type allows you
to display a link to the actual file. In most cases, you just need to set the
``type`` property to ``file``:

.. code-block:: yaml
easy_admin:
entities:
Product:
class: AppBundle\Entity\Product
list:
fields:
- { property: 'instructions', type: 'file' }
# ...
# ...
In the above example, the ``instructions`` property is displayed as an ``<a>``
HTML element whose ``href`` attribute is the value stored in the property.

If the property stores relative paths, define the ``base_path`` option to set the
absolute or relative path to be prefixed to the file path:

.. code-block:: yaml
easy_admin:
entities:
Product:
class: AppBundle\Entity\Product
list:
fields:
- { property: 'instructions', type: 'file', base_path: '/pdf/' }
# ...
# ...
The file base path can also be set in the entity, to avoid repeating its
value for different properties or different views:

.. code-block:: yaml
easy_admin:
entities:
Product:
class: AppBundle\Entity\Product
file_base_path: 'http://static.acme.org/pdf/'
list:
fields:
- { property: 'instructions', type: 'file' }
# ...
# ...
The base paths defined for a property always have priority over the one defined
globally for the entity.

The name of the file is displayed by default as the text of the ``<a>`` link.
If this is undesired, define the ``filename`` property to set the ``<a>`` text
explicitly:

.. code-block:: yaml
easy_admin:
entities:
Product:
class: AppBundle\Entity\Product
list:
fields:
- { property: 'instructions', type: 'file', filename: 'View instructions (PDF)' }
# ...
# ...
Raw Data Type
~~~~~~~~~~~~~

Expand Down
8 changes: 8 additions & 0 deletions src/Configuration/NormalizerConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ private function normalizePropertyConfig(array $backendConfig)
}
}

// for 'file' type fields, if the entity defines an 'file_base_path'
// option, but the field does not, use the value defined by the entity
if (isset($fieldConfig['type']) && 'file' === $fieldConfig['type']) {
if (!isset($fieldConfig['base_path']) && isset($entityConfig['file_base_path'])) {
$fieldConfig['base_path'] = $entityConfig['file_base_path'];
}
}

// fields that don't define the 'property' name are special form design elements
$fieldName = isset($fieldConfig['property']) ? $fieldConfig['property'] : '_easyadmin_form_design_element_'.$designElementIndex;
$fields[$fieldName] = $fieldConfig;
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/TemplateConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TemplateConfigPass implements ConfigPassInterface
'field_datetimetz' => '@EasyAdmin/default/field_datetimetz.html.twig',
'field_decimal' => '@EasyAdmin/default/field_decimal.html.twig',
'field_email' => '@EasyAdmin/default/field_email.html.twig',
'field_file' => '@EasyAdmin/default/field_file.html.twig',
'field_float' => '@EasyAdmin/default/field_float.html.twig',
'field_guid' => '@EasyAdmin/default/field_guid.html.twig',
'field_id' => '@EasyAdmin/default/field_id.html.twig',
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/views/default/field_file.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<a href="{{ asset(value) }}" target="_blank">
{{ filename|easyadmin_truncate }}
</a>
23 changes: 23 additions & 0 deletions src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ public function renderEntityField(\Twig_Environment $twig, $view, $entityName, $
return $this->renderImageField($templateParameters);
}

if ('file' === $fieldType) {
return $this->renderFileField($templateParameters);
}

if (in_array($fieldType, array('array', 'simple_array'))) {
return $this->renderArrayField($templateParameters);
}
Expand Down Expand Up @@ -211,6 +215,25 @@ private function renderImageField(array $templateParameters)
return $this->twig->render($templateParameters['field_options']['template'], $templateParameters);
}

private function renderFileField(array $templateParameters)
{
// avoid displaying broken links when the entity defines no file path
if (empty($templateParameters['value'])) {
return $this->twig->render($templateParameters['entity_config']['templates']['label_empty'], $templateParameters);
}

// add the base path only to files that are not absolute URLs (http or https) or protocol-relative URLs (//)
if (0 === preg_match('/^(http[s]?|\/\/)/i', $templateParameters['value'])) {
$templateParameters['value'] = isset($templateParameters['field_options']['base_path'])
? rtrim($templateParameters['field_options']['base_path'], '/').'/'.ltrim($templateParameters['value'], '/')
: '/'.ltrim($templateParameters['value'], '/');
}

$templateParameters['filename'] = isset($templateParameters['field_options']['filename']) ? $templateParameters['field_options']['filename'] : basename($templateParameters['value']);

return $this->twig->render($templateParameters['field_options']['template'], $templateParameters);
}

private function renderArrayField(array $templateParameters)
{
if (empty($templateParameters['value'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ easy_admin:
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
- { property: 'field1', type: 'image', base_path: '/field_img/' }
- { property: 'field1', type: 'image', base_path: '/field_path/' }
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
easy_admin:
entities:
Category:
image_base_path: '/entity_img/'
image_base_path: '/entity_path/'
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
easy_admin:
entities:
Category:
image_base_path: '/entity_img/'
image_base_path: '/entity_path/'
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
- { property: 'field1', type: 'image', base_path: '/field_img/' }
- { property: 'field1', type: 'image', base_path: '/field_path/' }
11 changes: 11 additions & 0 deletions tests/Configuration/fixtures/configurations/input/admin_147.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TEST
# field defines its own file base_path

# CONFIGURATION
easy_admin:
entities:
Category:
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
- { property: 'field1', type: 'file', base_path: '/field_path/' }
12 changes: 12 additions & 0 deletions tests/Configuration/fixtures/configurations/input/admin_148.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# TEST
# field desn't define its own file base_path but the parent entity does

# CONFIGURATION
easy_admin:
entities:
Category:
file_base_path: '/entity_path/'
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
- { property: 'field1', type: 'file' }
12 changes: 12 additions & 0 deletions tests/Configuration/fixtures/configurations/input/admin_149.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# TEST
# field defines its own file base_path and overrides entity configuration

# CONFIGURATION
easy_admin:
entities:
Category:
file_base_path: '/entity_path/'
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
- { property: 'field1', type: 'file', base_path: '/field_path/' }
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ easy_admin:
scale: 0
unique: false
property: field1
base_path: /field_img/
base_path: /field_path/
actions:
edit:
name: edit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
easy_admin:
entities:
Category:
image_base_path: /entity_img/
image_base_path: /entity_path/
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
Expand All @@ -27,7 +27,7 @@ easy_admin:
scale: 0
unique: false
property: field1
base_path: /entity_img/
base_path: /entity_path/
actions:
edit:
name: edit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
easy_admin:
entities:
Category:
image_base_path: /entity_img/
image_base_path: /entity_path/
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
Expand All @@ -27,7 +27,7 @@ easy_admin:
scale: 0
unique: false
property: field1
base_path: /field_img/
base_path: /field_path/
actions:
edit:
name: edit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
easy_admin:
entities:
Category:
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
field1:
base_path: /field_path/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
easy_admin:
entities:
Category:
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
field1:
base_path: /entity_path/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
easy_admin:
entities:
Category:
class: AppTestBundle\Entity\UnitTests\Category
list:
fields:
field1:
base_path: /field_path/

0 comments on commit 0dd151a

Please sign in to comment.