Selaa lähdekoodia

Added 'attributes' parameter for 'url' field type (#4386)

Evgeniy Sokolov 8 vuotta sitten
vanhempi
commit
0bc2aed877

+ 7 - 0
Resources/doc/reference/field_types.rst

@@ -118,6 +118,7 @@ Parameter                               Description
 ======================================  ==================================================================
 **hide_protocol**                       remove protocol part from the link text
 **url**                                 URL address (e.g. ``http://example.com``)
+**attributes**                          array of html tag attributes (e.g. ``array('target' => '_blank')``)
 **route.name**                          route name (e.g. ``acme_blog_homepage``)
 **route.parameters**                    array of route parameters (e.g. ``array('type' => 'example', 'display' => 'full')``)
 **route.absolute**                      boolean value, create absolute or relative url address based on ``route.name`` and  ``route.parameters`` (default ``false``)
@@ -133,6 +134,12 @@ Parameter                               Description
             // `<a href="http://example.com">http://example.com</a>`
             ->add('targetUrl', 'url')
 
+            // Output for value `http://example.com`:
+            // `<a href="http://example.com" target="_blank">example.com</a>`
+            ->add('targetUrl', 'url', array(
+                'attributes' => array('target' => '_blank')
+            ))
+
             // Output for value `http://example.com`:
             // `<a href="http://example.com">example.com</a>`
             ->add('targetUrl', 'url', array(

+ 8 - 1
Resources/views/CRUD/list_url.html.twig

@@ -42,7 +42,14 @@ file that was distributed with this source code.
             {% set value = value|replace({'http://': '', 'https://': ''}) %}
         {% endif %}
 
-        <a href="{{ url_address }}">{{ value }}</a>
+        <a
+            href="{{ url_address }}"
+            {%- for attribute, value in field_description.options.attributes|default([]) %}
+                {{ attribute }}="{{ value|escape('html_attr') }}"
+            {%- endfor -%}
+        >
+            {{- value -}}
+        </a>
     {% endif %}
 {% endspaceless %}
 {% endblock %}

+ 6 - 1
Resources/views/CRUD/show_url.html.twig

@@ -42,7 +42,12 @@ file that was distributed with this source code.
             {% set value = value|replace({'http://': '', 'https://': ''}) %}
         {% endif %}
 
-        <a href="{{ url_address }}">
+        <a
+            href="{{ url_address }}"
+            {%- for attribute, value in field_description.options.attributes|default([]) %}
+                {{ attribute }}="{{ value|escape('html_attr') }}"
+            {%- endfor -%}
+        >
             {%- if field_description.options.safe -%}
                 {{- value|raw -}}
             {%- else -%}

+ 28 - 0
Tests/Twig/Extension/SonataAdminExtensionTest.php

@@ -1032,6 +1032,22 @@ EOT
                 'https://example.com',
                 array(),
             ),
+            array(
+                '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
+                <a href="https://example.com" target="_blank">https://example.com</a>
+                </td>',
+                'url',
+                'https://example.com',
+                array('attributes' => array('target' => '_blank')),
+            ),
+            array(
+                '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
+                <a href="https://example.com" target="_blank" class="fooLink">https://example.com</a>
+                </td>',
+                'url',
+                'https://example.com',
+                array('attributes' => array('target' => '_blank', 'class' => 'fooLink')),
+            ),
             array(
                 '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
                 <a href="http://example.com">example.com</a>
@@ -1567,6 +1583,18 @@ EOT
                 'http://example.com',
                 array('safe' => false),
             ),
+            array(
+                '<th>Data</th> <td><a href="http://example.com" target="_blank">http://example.com</a></td>',
+                'url',
+                'http://example.com',
+                array('safe' => false, 'attributes' => array('target' => '_blank')),
+            ),
+            array(
+                '<th>Data</th> <td><a href="http://example.com" target="_blank" class="fooLink">http://example.com</a></td>',
+                'url',
+                'http://example.com',
+                array('safe' => false, 'attributes' => array('target' => '_blank', 'class' => 'fooLink')),
+            ),
             array(
                 '<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>',
                 'url',