Skip to content

Commit

Permalink
Allow to use display_* templates without field_description (#7110)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Apr 24, 2021
1 parent 6d5c068 commit 7805e4a
Show file tree
Hide file tree
Showing 34 changed files with 325 additions and 83 deletions.
3 changes: 2 additions & 1 deletion src/Resources/views/CRUD/_email_link.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

{# NEXT_MAJOR: Remove this template #}
{% deprecated 'The "_email_link.html.twig" is deprecated since sonata-project/admin-bundle 3.x and will be removed in 4.0.' %}
{%- if value is empty -%}
 
{%- elseif field_description.option('as_string', false) -%}
Expand Down
7 changes: 6 additions & 1 deletion src/Resources/views/CRUD/display_boolean.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set inverse = inverse|default(field_description.option('inverse', false)) %}
{% endif %}

{% if value %}
{% set text = 'label_type_yes'|trans({}, 'SonataAdminBundle') %}
{% else %}
{% set text = 'label_type_no'|trans({}, 'SonataAdminBundle') %}
{% endif %}

{% if field_description.option('inverse', false) ? not value : value %}
{% if inverse|default(false) ? not value : value %}
{% set class = 'label-success' %}
{% else %}
{% set class = 'label-danger' %}
Expand Down
77 changes: 47 additions & 30 deletions src/Resources/views/CRUD/display_choice.html.twig
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{%- apply spaceless %}
{% if field_description.option('choices') is not null %}
{% if field_description.option('multiple', false) and value is iterable %}

{% set result = '' %}
{% set delimiter = field_description.option('delimiter', ', ') %}

{% for val in value %}
{% if result is not empty %}
{% set result = result ~ delimiter %}
{% endif %}

{% if field_description.option('choices')[val] is defined %}
{% set choice = field_description.option('choices')[val] %}
{% else %}
{% set choice = val %}
{% endif %}
{% if field_description.option('catalogue') %}
{% set result = result ~ choice|trans({}, field_description.option('catalogue')) %}
{% else %}
{% set result = result ~ choice %}
{% endif %}
{% endfor %}

{% set value = result %}

{% elseif value in field_description.option('choices')|keys %}
{% if field_description.option('catalogue') is null %}
{% set value = field_description.option('choices')[value] %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set choices = choices|default(field_description.option('choices')) %}
{% set multiple = multiple|default(field_description.option('multiple', false)) %}
{% set delimiter = delimiter|default(field_description.option('delimiter', ', ')) %}
{% set translation_domain = translation_domain|default(field_description.option('catalogue')) %}
{% set safe = safe|default(field_description.option('safe', false)) %}
{% endif %}

{% set choices = choices|default([]) %}
{% if multiple|default(false) and value is iterable %}

{% set result = '' %}
{% for val in value %}
{% if result is not empty %}
{% set result = result ~ delimiter|default(', ') %}
{% endif %}

{% if choices[val] is defined %}
{% set choice = choices[val] %}
{% else %}
{% set value = field_description.option('choices')[value]|trans({}, field_description.option('catalogue')) %}
{% set choice = val %}
{% endif %}
{% if translation_domain|default(null) is null %}
{% set result = result ~ choice %}
{% else %}
{% set result = result ~ choice|trans({}, translation_domain) %}
{% endif %}
{% endfor %}

{% set value = result %}

{% elseif value in choices|keys %}
{% if translation_domain|default(null) is null %}
{% set value = choices[value] %}
{% else %}
{% set value = choices[value]|trans({}, translation_domain) %}
{% endif %}
{% endif %}

{% if field_description.option('safe', false) %}
{% if safe|default(false) %}
{{ value|raw }}
{% else %}
{{ value }}
Expand Down
7 changes: 6 additions & 1 deletion src/Resources/views/CRUD/display_currency.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set currency = currency|default(field_description.option('currency')) %}
{% endif %}

{%- if value is null -%}
&nbsp;
{%- else -%}
{{ field_description.option('currency') }} {{ value }}
{{ currency|default(null) }} {{ value }}
{%- endif -%}
{% endapply -%}
8 changes: 7 additions & 1 deletion src/Resources/views/CRUD/display_date.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set format = format|default(field_description.option('format', 'F j, Y')) %}
{% set timezone = timezone|default(field_description.option('timezone')) %}
{% endif %}

{%- if value is empty -%}
&nbsp;
{%- else -%}
<time datetime="{{ value|date('Y-m-d', 'UTC') }}" title="{{ value|date('Y-m-d', 'UTC') }}">
{{ value|date(field_description.option('format', 'F j, Y'), field_description.option('timezone')) }}
{{ value|date(format|default('F j, Y'), timezone|default(null)) }}
</time>
{%- endif -%}
{% endapply -%}
10 changes: 7 additions & 3 deletions src/Resources/views/CRUD/display_datetime.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set format = format|default(field_description.option('format')) %}
{% set timezone = timezone|default(field_description.option('timezone')) %}
{% endif %}

{%- if value is empty -%}
&nbsp;
{%- else -%}
{% set format = field_description.option('format') %}
{% set timezone = field_description.option('timezone') %}
<time datetime="{{ value|date('c', 'UTC') }}" title="{{ value|date('c', 'UTC') }}">
{{ value|date(format, timezone) }}
{{ value|date(format|default(null), timezone|default(null)) }}
</time>
{%- endif -%}
{% endapply -%}
38 changes: 38 additions & 0 deletions src/Resources/views/CRUD/display_email.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set as_string = format|default(field_description.option('as_string')) %}
{% set subject = subject|default(field_description.option('subject')) %}
{% set body = body|default(field_description.option('body')) %}
{% endif %}

{%- if value is empty -%}
&nbsp;
{%- elseif as_string|default(false) -%}
{{ value }}
{%- else -%}
{% set parameters = {} %}

{% if subject|default(null) is not empty %}
{% set parameters = parameters|merge({'subject': subject}) %}
{% endif %}
{% if body|default(null) is not empty %}
{% set parameters = parameters|merge({'body': body}) %}
{% endif %}

<a href="mailto:{{ value }}{% if parameters|length > 0 %}?{{- parameters|url_encode -}}{% endif %}">
{{- value -}}
</a>
{%- endif -%}
{% endapply -%}
28 changes: 25 additions & 3 deletions src/Resources/views/CRUD/display_html.html.twig
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set truncate = truncate|default(field_description.option('truncate')) %}
{% set strip = strip|default(field_description.option('strip', false)) %}
{% endif %}

{%- if value is empty -%}
&nbsp;
{% else %}
{%- if field_description.option('truncate') -%}
{% set truncate = field_description.option('truncate') %}
{%- if truncate|default(null) -%}
{% set length = truncate.length|default(30) %}
{# NEXT_MAJOR: Remove this #}
{% if truncate.preserve is defined %}
{% deprecated 'The "truncate.preserve" option is deprecated since sonata-project/admin-bundle 3.65, to be removed in 4.0. Use "truncate.cut" instead.' %}
{% endif %}
{# NEXT_MAJOR: Remove this and uncomment the following line #}
{% set cut = truncate.cut is defined ? truncate.cut : (truncate.preserve is defined ? truncate.preserve != true : true) %}
{# {% set cut = truncate.cut|default(true) %} #}
{# NEXT_MAJOR: Remove this #}
{% if truncate.separator is defined %}
{% deprecated 'The "truncate.separator" option is deprecated since sonata-project/admin-bundle 3.65, to be removed in 4.0. Use "truncate.ellipsis" instead.' %}
{% endif %}
{# NEXT_MAJOR: Remove this and uncomment the following line #}
{% set ellipsis = truncate.ellipsis is defined ? truncate.ellipsis : (truncate.separator is defined ? truncate.separator : '...') %}
{# {% set ellipsis = truncate.ellipsis|default('...') %} #}
{{ value|striptags|u.truncate(length, ellipsis, cut)|raw }}
{%- else -%}
{%- if field_description.option('strip', false) -%}
{%- if strip|default(false) -%}
{% set value = value|striptags %}
{%- endif -%}
{{ value|raw }}
Expand Down
8 changes: 7 additions & 1 deletion src/Resources/views/CRUD/display_time.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set format = format|default(field_description.option('format', 'H:i:s')) %}
{% set timezone = timezone|default(field_description.option('timezone')) %}
{% endif %}

{%- if value is empty -%}
&nbsp;
{%- else -%}
<time datetime="{{ value|date('H:i:sP', 'UTC') }}" title="{{ value|date('H:i:sP', 'UTC') }}">
{{ value|date(field_description.option('format', 'H:i:s'), field_description.option('timezone')) }}
{{ value|date(format|default('H:i:s'), timezone|default(null)) }}
</time>
{%- endif -%}
{% endapply -%}
23 changes: 19 additions & 4 deletions src/Resources/views/CRUD/display_trans.html.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{%- apply spaceless %}
{% set value_format = field_description.option('format', '%s') %}
{% set translation_domain = field_description.option('catalogue', admin.translationDomain) %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set value_format = value_format|default(field_description.option('format', '%s')) %}
{% set translation_domain = translation_domain|default(field_description.option('catalogue', admin.translationDomain)) %}
{% set safe = safe|default(field_description.option('safe', false)) %}
{% endif %}

{% set value = value_format|format(value)|trans({}, translation_domain) %}
{% set value = value_format|default('%s')|format(value)|trans({}, translation_domain|default('messages')) %}

{% if field_description.option('safe', false) %}
{% if safe|default(false) %}
{{ value|raw }}
{% else %}
{{ value }}
Expand Down
45 changes: 30 additions & 15 deletions src/Resources/views/CRUD/display_url.html.twig
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{%- apply spaceless %}
{# NEXT_MAJOR: Remove this BC-layer #}
{% if field_description is defined %}
{% set url = url|default(field_description.option('url')) %}
{% set route = route|default(field_description.option('route')) %}
{% set hide_protocol = hide_protocol|default(field_description.option('hide_protocol', false)) %}
{% set attributes = attributes|default(field_description.option('attributes', [])) %}
{% set safe = safe|default(field_description.option('safe', false)) %}
{% endif %}

{% if value is empty %}
&nbsp;
{% else %}
{% if field_description.option('url') %}
{% if url|default(null) %}
{# target url is string #}
{% set url_address = field_description.option('url') %}
{% elseif field_description.option('route') is not null and field_description.option('route').name not in ['edit', 'show'] %}
{% set url_address = url %}
{% elseif route|default(null) is not null %}
{# target url is Symfony route #}
{% set parameters = field_description.option('route').parameters|default([]) %}

{# route with paramter related to object ID #}
{% if field_description.option('route').identifier_parameter_name is defined %}
{% set parameters = parameters|merge({(field_description.option('route').identifier_parameter_name):(admin.normalizedidentifier(object))}) %}
{% endif %}
{% set parameters = route.parameters|default([]) %}

{% if field_description.option('route').absolute|default(false) %}
{% set url_address = url(field_description.option('route').name, parameters) %}
{% if route.absolute|default(false) %}
{% set url_address = url(route.name, parameters) %}
{% else %}
{% set url_address = path(field_description.option('route').name, parameters) %}
{% set url_address = path(route.name, parameters) %}
{% endif %}
{% else %}
{# value is url #}
{% set url_address = value %}
{% endif %}

{% if field_description.option('hide_protocol', false) %}
{% if hide_protocol|default(false) %}
{% set value = value|replace({'http://': '', 'https://': ''}) %}
{% endif %}

<a
href="{{ url_address }}"
{%- for attribute, value in field_description.option('attributes', []) %}
{%- for attribute, value in attributes|default([]) %}
{{ attribute }}="{{ value|escape('html_attr') }}"
{%- endfor -%}
>
{%- if field_description.option('safe', false) -%}
{%- if safe|default(false) -%}
{{- value|raw -}}
{%- else -%}
{{- value -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ file that was distributed with this source code.
#}

{%- include '@SonataAdmin/CRUD/display_datetime.html.twig' with { value: revision.timestamp } -%}
{%- include '@SonataAdmin/CRUD/display_datetime.html.twig' with {
value: revision.timestamp,
format: field_description.option('format'),
timezone: field_description.option('timezone'),
} only -%}
5 changes: 4 additions & 1 deletion src/Resources/views/CRUD/list_boolean.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ file that was distributed with this source code.
{% endblock %}

{% block field %}
{%- include '@SonataAdmin/CRUD/display_boolean.html.twig' -%}
{%- include '@SonataAdmin/CRUD/display_boolean.html.twig' with {
value: value,
inverse: field_description.option('inverse'),
} only -%}
{% endblock %}
Loading

0 comments on commit 7805e4a

Please sign in to comment.