Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.

Commit 9ccf455

Browse files
committed
Merge pull request #172 from drupal-media/twig-rebase
Adding Twig extension for embedding entities in templates
2 parents 9837318 + b85dccd commit 9ccf455

9 files changed

Lines changed: 241 additions & 13 deletions

entity_embed.services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ services:
22
plugin.manager.entity_embed.display:
33
class: Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
44
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler']
5+
entity_embed.twig.entity_embed_twig_extension:
6+
class: Drupal\entity_embed\Twig\EntityEmbedTwigExtension
7+
arguments: ['@entity.manager', '@module_handler', '@plugin.manager.entity_embed.display']
8+
tags:
9+
- { name: twig.extension }

src/Tests/EntityEmbedHooksTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,8 @@ class EntityEmbedHooksTest extends EntityEmbedTestBase {
2424
*/
2525
protected $state;
2626

27-
/**
28-
* Modules to enable.
29-
*
30-
* @var array
31-
*/
32-
public static $modules = array(
33-
'entity_embed',
34-
'entity_embed_test',
35-
'node',
36-
);
37-
3827
protected function setUp() {
3928
parent::setUp();
40-
4129
$this->state = $this->container->get('state');
4230
}
4331

src/Tests/EntityEmbedTestBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class EntityEmbedTestBase extends WebTestBase {
2222
*
2323
* @var array
2424
*/
25-
public static $modules = ['entity_embed', 'node', 'ckeditor'];
25+
public static $modules = ['entity_embed', 'entity_embed_test', 'node', 'ckeditor'];
2626

2727
/**
2828
* The test user.

src/Tests/EntityEmbedTwigTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\system\Tests\EntityEmbedTwigTest.
6+
*/
7+
8+
namespace Drupal\entity_embed\Tests;
9+
10+
/**
11+
* Tests Twig extension provided by entity_embed.
12+
*
13+
* @group entity_embed
14+
*/
15+
class EntityEmbedTwigTest extends EntityEmbedTestBase {
16+
17+
protected function setUp() {
18+
parent::setUp();
19+
\Drupal::service('theme_handler')->install(array('test_theme'));
20+
}
21+
22+
/**
23+
* Tests that the provided Twig extension loads the service appropriately.
24+
*/
25+
public function testTwigExtensionLoaded() {
26+
$twig_service = \Drupal::service('twig');
27+
28+
$ext = $twig_service->getExtension('entity_embed.twig.entity_embed_twig_extension');
29+
30+
// @todo why is the string
31+
// 'Drupal\\entity_embed\\Twig\\EntityEmbedTwigExtension'
32+
// and not '\Drupal\entity_embed\Twig\EntityEmbedTwigExtension' ?
33+
$this->assertEqual(get_class($ext), 'Drupal\\entity_embed\\Twig\\EntityEmbedTwigExtension', 'Extension loaded successfully.');
34+
}
35+
36+
/**
37+
* Tests that the Twig extension's filter produces expected output.
38+
*/
39+
public function testEntityEmbedTwigFunction() {
40+
// Test embedding a node using entity ID.
41+
$this->drupalGet('entity_embed_twig_test/id');
42+
$this->assertText($this->node->body->value, 'Embedded node exists in page');
43+
44+
// Test embedding using 'Label' display plugin.
45+
$this->drupalGet('entity_embed_twig_test/label_plugin');
46+
$this->assertText($this->node->title->value, 'Title of the embedded node exists in page.');
47+
$this->assertNoText($this->node->body->value, 'Body of embedded node does not exists in page when "Label" plugin is used.');
48+
$this->assertLinkByHref('node/' . $this->node->id(), 0, 'Link to the embedded node exists when "Label" plugin is used.');
49+
50+
// Test embedding using 'Label' display plugin without linking to the node.
51+
$this->drupalGet('entity_embed_twig_test/label_plugin_no_link');
52+
$this->assertText($this->node->title->value, 'Title of the embedded node exists in page.');
53+
$this->assertNoText($this->node->body->value, 'Body of embedded node does not exists in page when "Label" plugin is used.');
54+
$this->assertNoLinkByHref('node/' . $this->node->id(), 0, 'Link to the embedded node does not exists.');
55+
}
56+
57+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\entity_embed\Twig\EntityEmbedTwigExtension.
6+
*/
7+
8+
namespace Drupal\entity_embed\Twig;
9+
10+
use Drupal\Core\Entity\EntityManagerInterface;
11+
use Drupal\entity_embed\EntityHelperTrait;
12+
use Drupal\Core\Extension\ModuleHandlerInterface;
13+
use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;
14+
15+
/**
16+
* Provide entity embedding function within Twig templates.
17+
*/
18+
class EntityEmbedTwigExtension extends \Twig_Extension {
19+
use EntityHelperTrait;
20+
21+
/**
22+
* Constructs a new EntityEmbedTwigExtension.
23+
*
24+
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
25+
* The entity manager service.
26+
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
27+
* The module handler.
28+
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager
29+
* The plugin manager.
30+
*/
31+
public function __construct(EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, EntityEmbedDisplayManager $plugin_manager) {
32+
$this->setEntityManager($entity_manager);
33+
$this->setModuleHandler($module_handler);
34+
$this->setDisplayPluginManager($plugin_manager);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function getName() {
41+
return 'entity_embed.twig.entity_embed_twig_extension';
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getFunctions() {
48+
return array(
49+
new \Twig_SimpleFunction('entity_embed', array($this, 'getRenderArray')),
50+
);
51+
}
52+
53+
/**
54+
* Return the render array for an entity.
55+
*
56+
* @param string $entity_type
57+
* The machine name of an entity_type like 'node'.
58+
* @param string $entity_id
59+
* The entity ID or entity UUID.
60+
* @param string $display_plugin
61+
* (optional) The display plugin to be used to render the entity.
62+
* @param array $display_settings
63+
* (optional) A list of settings for the display plugin.
64+
*
65+
* @return array
66+
* A render array from entity_view().
67+
*/
68+
public function getRenderArray($entity_type, $entity_id, $display_plugin = 'default', array $display_settings = []) {
69+
$entity = $this->loadEntity($entity_type, $entity_id);
70+
$context = array(
71+
'data-entity-type' => $entity_type,
72+
'data-entity-id' => $entity_id,
73+
'data-entity-embed-display' => $display_plugin,
74+
'data-entity-embed-settings' => $display_settings,
75+
);
76+
return $this->renderEntityEmbed($entity, $context);
77+
}
78+
79+
}

tests/modules/entity_embed_test/entity_embed_test.module

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77

88
use Drupal\Core\Entity\EntityInterface;
99

10+
/**
11+
* Implements hook_theme().
12+
*/
13+
function entity_embed_test_theme($existing, $type, $theme, $path) {
14+
$items['entity_embed_twig_test'] = array(
15+
'template' => 'entity_embed_twig_test',
16+
'variables' => array(
17+
'entity_type' => '',
18+
'id' => '',
19+
'display_plugin' => 'default',
20+
'display_settings' => array(),
21+
),
22+
);
23+
return $items;
24+
}
25+
26+
1027
/**
1128
* Implements hook_entity_embed_display_plugins_alter().
1229
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
entity_embed_test.entity_embed_twig_test.id:
2+
path: '/entity_embed_twig_test/id'
3+
defaults:
4+
_controller: '\Drupal\entity_embed_test\EntityEmbedTestTwigController::idRender'
5+
requirements:
6+
_access: 'TRUE'
7+
8+
entity_embed_test.entity_embed_twig_test.label_plugin:
9+
path: '/entity_embed_twig_test/label_plugin'
10+
defaults:
11+
_controller: '\Drupal\entity_embed_test\EntityEmbedTestTwigController::labelPluginRender'
12+
requirements:
13+
_access: 'TRUE'
14+
15+
entity_embed_test.entity_embed_twig_test.label_plugin_no_link:
16+
path: '/entity_embed_twig_test/label_plugin_no_link'
17+
defaults:
18+
_controller: '\Drupal\entity_embed_test\EntityEmbedTestTwigController::labelPluginNoLinkRender'
19+
requirements:
20+
_access: 'TRUE'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\entity_embed_test\EntityEmbedTestTwigController.
6+
*/
7+
8+
namespace Drupal\entity_embed_test;
9+
10+
/**
11+
* Controller routines for Twig theme test routes.
12+
*/
13+
class EntityEmbedTestTwigController {
14+
15+
/**
16+
* Menu callback for testing entity_embed twig extension using entity ID.
17+
*/
18+
public function idRender() {
19+
return array(
20+
'#theme' => 'entity_embed_twig_test',
21+
'#entity_type' => 'node',
22+
'#id' => '1',
23+
);
24+
}
25+
26+
/**
27+
* Menu callback for testing entity_embed twig extension using 'label' display plugin.
28+
*/
29+
public function labelPluginRender() {
30+
return array(
31+
'#theme' => 'entity_embed_twig_test',
32+
'#entity_type' => 'node',
33+
'#id' => '1',
34+
'#display_plugin' => 'entity_reference:entity_reference_label',
35+
);
36+
}
37+
38+
/**
39+
* Menu callback for testing entity_embed twig extension using 'label' display plugin without linking to the node.
40+
*/
41+
public function labelPluginNoLinkRender() {
42+
return array(
43+
'#theme' => 'entity_embed_twig_test',
44+
'#entity_type' => 'node',
45+
'#id' => '1',
46+
'#display_plugin' => 'entity_reference:entity_reference_label',
47+
'#display_settings' => array('link' => 0),
48+
);
49+
}
50+
51+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{#
2+
/**
3+
* Template for testing entity_embed twig extension.
4+
*
5+
* Available variables:
6+
* - entity_type: Machine name of the entity type.
7+
* - id: ID or UUID of the entity to be embedded.
8+
* - display_plugin: Machine name of the display plugin.
9+
* - display_settings: An array of settings to be passed to the selected plugin.
10+
#}
11+
{{ entity_embed(entity_type, id, display_plugin, display_settings) }}

0 commit comments

Comments
 (0)