You are here

class CommerceTwigExtension in Commerce Core 8.2

Provides the Commerce Twig extensions.

Hierarchy

  • class \Drupal\commerce\TwigExtension\CommerceTwigExtension extends \Drupal\commerce\TwigExtension\Twig_Extension

Expanded class hierarchy of CommerceTwigExtension

1 string reference to 'CommerceTwigExtension'
commerce.services.yml in ./commerce.services.yml
commerce.services.yml
1 service uses CommerceTwigExtension
commerce.twig_extension in ./commerce.services.yml
Drupal\commerce\TwigExtension\CommerceTwigExtension

File

src/TwigExtension/CommerceTwigExtension.php, line 10

Namespace

Drupal\commerce\TwigExtension
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceTwigExtension::getFilters public function @inheritdoc
CommerceTwigExtension::getName public function @inheritdoc
CommerceTwigExtension::renderEntity public static function Renders an entity in the given view mode.