CommerceTwigExtension.php in Commerce Core 8.2
File
src/TwigExtension/CommerceTwigExtension.php
View source
<?php
namespace Drupal\commerce\TwigExtension;
use Drupal\Core\Entity\ContentEntityInterface;
class CommerceTwigExtension extends \Twig_Extension {
public function getFilters() {
return [
new \Twig_SimpleFilter('commerce_entity_render', [
$this,
'renderEntity',
]),
];
}
public function getName() {
return 'commerce.twig_extension';
}
public static function renderEntity($entity, $view_mode = 'default') {
if (empty($entity)) {
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);
}
}