-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Twig ToolbarExtension to render administration toolbar
Fix documentation Add missing twig extensions package Fix toolbar rendering to use toolbar_view() Add is_granted() to access_toolbar
- Loading branch information
Showing
7 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') }}" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters