Skip to content

Commit

Permalink
Add Twig ToolbarExtension to render administration toolbar
Browse files Browse the repository at this point in the history
Fix documentation

Add missing twig extensions package

Fix toolbar rendering to use toolbar_view()

Add is_granted() to access_toolbar
  • Loading branch information
eko committed May 28, 2014
1 parent 7889ad8 commit 26a7519
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 1 deletion.
1 change: 1 addition & 0 deletions DependencyInjection/EkinoDrupalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function load(array $config, ContainerBuilder $container)
$loader->load('services.xml');
$loader->load('session.xml');
$loader->load('user_hook.xml');
$loader->load('twig.xml');

$this->configureEntityRepositories($container, $config);
$this->configureTablesPrefix($container, $config);
Expand Down
12 changes: 12 additions & 0 deletions Resources/config/twig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="ekino_drupal.twig.toolbar_extension" class="Ekino\Bundle\DrupalBundle\Twig\ToolbarExtension">
<tag name="twig.extension" />
</service>
</services>
</container>
14 changes: 14 additions & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Ekino Drupal Bundle
===================

The ``EkinoDrupalBundle`` tries to deeply integrate Symfony2 with Drupal and Drupal with Symfony2. Of course this is done without
altering the Drupal's core.

Reference Guide
---------------

.. toctree::
:maxdepth: 1
:numbered:

reference/twig
34 changes: 34 additions & 0 deletions Resources/doc/reference/twig.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.. index::
single: Extensions Twig

Extensions Twig
===============

We provide a Twig extension to help rendering Drupal parts into your Symfony Twig templates.

ToolbarExtension
----------------

You can render the administration Drupal toolbar parts by adding the following HTML to your Twig template:

.. code-block:: html

{% if is_granted('PERMISSION_DRUPAL_ACCESS_TOOLBAR') %}
<div id="toolbar" class="toolbar overlay-displace-top clearfix toolbar-processed">
<div class="toolbar-menu clearfix">
{{ ekino_drupal_toolbar_render_item('toolbar_home') }}
{{ ekino_drupal_toolbar_render_item('toolbar_user') }}
{{ ekino_drupal_toolbar_render_item('toolbar_menu') }}
{{ ekino_drupal_toolbar_render_item('toolbar_toggle') }}
</div>
</div>
{% endif %}

Please ensure that your user has the correct role to "access toolbar" in Drupal administration.

As an additional note, you will also need to load the following stylesheets:

.. code-block:: html

<link rel="stylesheet" type="text/css" href="{{ asset('modules/system/system.base.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ asset('modules/toolbar/toolbar.css') }}" />
97 changes: 97 additions & 0 deletions Tests/Twig/ToolbarExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of the Ekino Drupal package.
*
* (c) 2011 Ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace {
/**
* Fake Drupal toolbar_view() function
*
* @return array
*/
function toolbar_view()
{
return array('test' => 'value');
}

/**
* Fake Drupal render() function
*
* @param string $item
*
* @return string
*/
function render($item)
{
return sprintf('<h1>%s</h1>', $item);
}
}

namespace Ekino\Bundle\DrupalBundle\Tests\Twig {
use Ekino\Bundle\DrupalBundle\Twig\ToolbarExtension;

/**
* Class ToolbarExtensionTest
*
* @author Vincent Composieux <vincent.composieux@gmail.com>
*/
class ToolbarExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ToolbarExtension
*/
protected $extension;

/**
* Sets up Twig toolbar extension
*/
public function setUp()
{
$this->extension = new ToolbarExtension();
}

/**
* Tests the getFunctions() method
*/
public function testGetFunctions()
{
$functions = $this->extension->getFunctions();

$this->assertTrue(is_array($functions), 'Should return an array of functions');

foreach ($functions as $function) {
$this->assertInstanceOf('\Twig_SimpleFunction', $function);
}
}

/**
* Tests the renderToolbarItem() method
*/
public function testRenderToolbarItem()
{
// When
$result = $this->extension->renderToolbarItem('test');

// Then
$this->assertEquals('<h1>value</h1>', $result, 'Should return render() function');
}

/**
* Tests the getName() method
*/
public function testGetName()
{
// When
$name = $this->extension->getName();

// Then
$this->assertEquals('ekino_drupal_toolbar_extension', $name, 'Should return correct Twig extension name');
}
}
}
61 changes: 61 additions & 0 deletions Twig/ToolbarExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Ekino Drupal package.
*
* (c) 2011 Ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ekino\Bundle\DrupalBundle\Twig;

/**
* Twig toolbar extension class
*
* @author Vincent Composieux <vincent.composieux@gmail.com>
*/
class ToolbarExtension extends \Twig_Extension
{
/**
* @var array
*/
protected $toolbar;

/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('ekino_drupal_toolbar_render_item', array($this, 'renderToolbarItem'), array(
'is_safe' => array('html')
)),
);
}

/**
* Renders HTML for a Drupal administration toolbar item
*
* @param $item
*
* @return string
*/
public function renderToolbarItem($item)
{
if (null === $this->toolbar) {
$this->toolbar = toolbar_view();
}

return render($this->toolbar[$item]);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'ekino_drupal_toolbar_extension';
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"php": ">=5.3.2",
"friendsofsymfony/user-bundle": "*",
"symfony/http-foundation": ">=2.1,<3.0-dev",
"symfony/http-kernel": ">=2.1,<3.0-dev"
"symfony/http-kernel": ">=2.1,<3.0-dev",
"twig/extensions": "1.0.*"
},
"autoload": {
"psr-0": { "Ekino\\Bundle\\DrupalBundle": "" }
Expand Down

0 comments on commit 26a7519

Please sign in to comment.