Commit f0153cf1 authored by bojanz's avatar bojanz

Issue #2905337 by bojanz: Add a commerce_entity_render twig filter

parent 3b8b5218
......@@ -50,6 +50,11 @@ services:
class: Drupal\commerce\Config\ConfigUpdater
arguments: ['@entity_type.manager', '@config.storage', '@config.factory']
commerce.twig_extension:
class: Drupal\commerce\TwigExtension\CommerceTwigExtension
tags:
- { name: twig.extension }
cache_context.country:
class: Drupal\commerce\Cache\Context\CountryCacheContext
arguments: ['@commerce.current_country']
......
......@@ -22,7 +22,7 @@ class PriceTwigExtension extends \Twig_Extension {
* @inheritdoc
*/
public function getName() {
return 'commerce_price.twig_extenstion';
return 'commerce_price.twig_extension';
}
/**
......
<?php
namespace Drupal\commerce\TwigExtension;
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Provides the Commerce Twig extensions.
*/
class CommerceTwigExtension extends \Twig_Extension {
/**
* @inheritdoc
*/
public function getFilters() {
return [
new \Twig_SimpleFilter('commerce_entity_render', [$this, 'renderEntity']),
];
}
/**
* @inheritdoc
*/
public function getName() {
return 'commerce.twig_extension';
}
/**
* Renders an entity in the given view mode.
*
* Example: {{ order_item.getPurchasableEntity|commerce_entity_render }}
*
* @param mixed $entity
* The entity.
* @param string $view_mode
* The view mode.
*
* @return array
* A renderable array for the rendered entity.
*
* @throws \InvalidArgumentException
*/
public static function renderEntity($entity, $view_mode = 'default') {
if (empty($entity)) {
// Nothing to render.
return [];
}
if (!($entity instanceof ContentEntityInterface)) {
throw new \InvalidArgumentException('The "commerce_entity_render" filter must be given a content entity.');
}
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
return $view_builder->view($entity, $view_mode);
}
}
<?php
/**
* @file
* Test module for Commerce.
*/
/**
* Implements hook_theme().
*/
function commerce_test_theme($existing, $type, $theme, $path) {
return [
'render_entity' => [
'template' => 'commerce-test-entity-render',
'variables' => [
'entity' => NULL,
],
],
];
}
<?php
namespace Drupal\Tests\commerce\Kernel;
use Drupal\commerce_price\Entity\Currency;
/**
* Tests the commerce twig filters.
*
* @group commerce
*/
class CommerceTwigExtensionTest extends CommerceKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'commerce_test',
];
/**
* Tests trying to render a config entity.
*/
public function testRenderConfigEntity() {
$theme = [
'#theme' => 'render_entity',
'#entity' => Currency::load('USD'),
];
$this->setExpectedException('InvalidArgumentException');
$this->render($theme);
}
/**
* Tests rendering a content entity.
*/
public function testRenderContentEntity() {
$account = $this->createUser();
$theme = [
'#theme' => 'render_entity',
'#entity' => $account,
];
$this->render($theme);
$this->assertText('Member for');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment