Ver código fonte

add global variables

Thomas Rabaix 10 anos atrás
pai
commit
cac0f0da03

+ 32 - 0
DependencyInjection/Compiler/GlobalVariablesCompilerPass.php

@@ -0,0 +1,32 @@
+<?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\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+
+/**
+ * GlobalVariablesCompilerPass
+ *
+ * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+class GlobalVariablesCompilerPass implements CompilerPassInterface
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function process(ContainerBuilder $container)
+    {
+        $container->getDefinition('twig')
+            ->addMethodCall('addGlobal', array('sonata_admin', new Reference('sonata.admin.twig.global')));
+    }
+}

+ 5 - 0
Resources/config/core.xml

@@ -71,5 +71,10 @@
             <argument type="service" id="event_dispatcher" />
             <argument type="service" id="event_dispatcher" />
             <tag name="sonata.admin.extension" global="true" />
             <tag name="sonata.admin.extension" global="true" />
         </service>
         </service>
+
+        <!-- twig -->
+        <service id="sonata.admin.twig.global" class="Sonata\AdminBundle\Twig\GlobalVariables" >
+            <argument type="service" id="service_container" />
+        </service>
     </services>
     </services>
 </container>
 </container>

+ 60 - 0
Twig/GlobalVariables.php

@@ -0,0 +1,60 @@
+<?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\Twig;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Sonata\AdminBundle\Admin\Pool;
+
+/**
+ * GlobalVariables
+ *
+ * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+class GlobalVariables
+{
+    protected $container;
+
+    /**
+     * @param ContainerInterface $container
+     */
+    public function __construct(ContainerInterface $container)
+    {
+        $this->container = $container;
+    }
+
+    /**
+     * @return Pool
+     */
+    public function getAdminPool()
+    {
+        return $this->container->get('sonata.admin.pool');
+    }
+
+    /**
+     * @param string $code
+     * @param string $action
+     * @param array  $parameters
+     * @param mixed  $absolute
+     *
+     * @return string
+     */
+    public function url($code, $action, $parameters = array(), $absolute = false)
+    {
+        if ($pipe = strpos('|', $code)) {
+            // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
+            // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
+            $action = $code.'.'.$action;
+            $code = substr($code, 0, $pipe);
+        }
+
+        return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action, $parameters, $absolute);
+    }
+}