Bladeren bron

Fix tab id issue with non valid character

Thomas Rabaix 12 jaren geleden
bovenliggende
commit
1f80e66fd8

+ 2 - 2
Resources/views/CRUD/base_edit_form.html.twig

@@ -17,14 +17,14 @@
                 <div class="tabbable">
                 <div class="tabbable">
                     <ul class="nav nav-tabs">
                     <ul class="nav nav-tabs">
                         {% for name, form_group in admin.formgroups %}
                         {% for name, form_group in admin.formgroups %}
-                            <li class="{% if loop.first %}active{% endif %}"><a href="#{{ name }}" data-toggle="tab">{{ name }}</a></li>
+                            <li class="{% if loop.first %}active{% endif %}"><a href="#{{ name|sonata_slugify }}" data-toggle="tab">{{ admin.trans(name) }}</a></li>
                         {% endfor %}
                         {% endfor %}
                     </ul>
                     </ul>
             {% endblock %}
             {% endblock %}
 
 
                 <div class="tab-content">
                 <div class="tab-content">
                     {% for name, form_group in admin.formgroups %}
                     {% for name, form_group in admin.formgroups %}
-                        <div class="tab-pane {% if loop.first %} active{% endif %}" id="{{ name }}">
+                        <div class="tab-pane {% if loop.first %} active{% endif %}" id="{{ name|sonata_slugify }}">
                             <fieldset>
                             <fieldset>
                                 <div class="sonata-ba-collapsed-fields">
                                 <div class="sonata-ba-collapsed-fields">
                                     {% if form_group.description != false %}
                                     {% if form_group.description != false %}

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

@@ -0,0 +1,33 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace Sonata\AdminBundle\Tests\Twig\Extension;
+
+use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
+use Sonata\AdminBundle\Admin\Pool;
+
+class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
+{
+    public function testSlugify()
+    {
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+
+        $pool = new Pool($container, '','');
+
+        $s = new SonataAdminExtension($pool);
+
+        $this->assertEquals($s->slugify('test'), 'test');
+        $this->assertEquals($s->slugify('S§!@@#$#$alut'), 's-alut');
+        $this->assertEquals($s->slugify('Symfony2'), 'symfony2');
+        $this->assertEquals($s->slugify('test'), 'test');
+        $this->assertEquals($s->slugify('c\'est bientôt l\'été'), 'c-est-bientot-l-ete');
+        $this->assertEquals($s->slugify(urldecode('%2Fc\'est+bientôt+l\'été')), 'c-est-bientot-l-ete');
+    }
+}

+ 30 - 0
Twig/Extension/SonataAdminExtension.php

@@ -54,6 +54,7 @@ class SonataAdminExtension extends \Twig_Extension
             'render_view_element'     => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
             'render_view_element'     => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
             'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement'),
             'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement'),
             'sonata_urlsafeid'        => new \Twig_Filter_Method($this, 'getUrlsafeIdentifier'),
             'sonata_urlsafeid'        => new \Twig_Filter_Method($this, 'getUrlsafeIdentifier'),
+            'sonata_slugify'          => new \Twig_Filter_Method($this, 'slugify'),
         );
         );
     }
     }
 
 
@@ -232,4 +233,33 @@ class SonataAdminExtension extends \Twig_Extension
 
 
         return $admin->getUrlsafeIdentifier($model);
         return $admin->getUrlsafeIdentifier($model);
     }
     }
+
+    /**
+     * Slugify a text
+     *
+     * @param $text
+     *
+     * @return text
+     */
+    public function slugify($text)
+    {
+        // replace non letter or digits by -
+        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
+
+        // trim
+        $text = trim($text, '-');
+
+        // transliterate
+        if (function_exists('iconv')) {
+            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
+        }
+
+        // lowercase
+        $text = strtolower($text);
+
+        // remove unwanted characters
+        $text = preg_replace('~[^-\w]+~', '', $text);
+
+        return $text;
+    }
 }
 }