
| Current Path : /var/www/html/store/web/modules/contrib/commerce/src/TwigExtension/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/store/web/modules/contrib/commerce/src/TwigExtension/CommerceTwigExtension.php |
<?php
namespace Drupal\commerce\TwigExtension;
use Drupal\Core\Entity\ContentEntityInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
/**
* Provides the Commerce Twig extensions.
*/
class CommerceTwigExtension extends AbstractExtension {
/**
* @inheritdoc
*/
public function getFilters() {
return [
new TwigFilter('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);
}
}