You are here

class EntityEmbedTwigExtension in Entity Embed 8

Provide entity embedding function within Twig templates.

Hierarchy

Expanded class hierarchy of EntityEmbedTwigExtension

1 file declares its use of EntityEmbedTwigExtension
EntityEmbedTwigTest.php in tests/src/Functional/EntityEmbedTwigTest.php
1 string reference to 'EntityEmbedTwigExtension'
entity_embed.services.yml in ./entity_embed.services.yml
entity_embed.services.yml
1 service uses EntityEmbedTwigExtension
entity_embed.twig.entity_embed_twig_extension in ./entity_embed.services.yml
Drupal\entity_embed\Twig\EntityEmbedTwigExtension

File

src/Twig/EntityEmbedTwigExtension.php, line 12

Namespace

Drupal\entity_embed\Twig
View source
class EntityEmbedTwigExtension extends \Twig_Extension {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity embed builder service.
   *
   * @var \Drupal\entity_embed\EntityEmbedBuilderInterface
   */
  protected $builder;

  /**
   * Constructs a new EntityEmbedTwigExtension.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\entity_embed\EntityEmbedBuilderInterface $builder
   *   The Entity embed builder service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityEmbedBuilderInterface $builder) {
    $this->entityTypeManager = $entity_type_manager;
    $this->builder = $builder;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('entity_embed.builder'));
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'entity_embed.twig.entity_embed_twig_extension';
  }

  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('entity_embed', [
        $this,
        'getRenderArray',
      ]),
    ];
  }

  /**
   * Return the render array for an entity.
   *
   * @param string $entity_type
   *   The machine name of an entity_type like 'node'.
   * @param string $entity_id
   *   The entity ID.
   * @param string $display_plugin
   *   (optional) The Entity Embed Display plugin to be used to render the
   *   entity.
   * @param array $display_settings
   *   (optional) A list of settings for the Entity Embed Display plugin.
   *
   * @return array
   *   A render array from entity_view().
   */
  public function getRenderArray($entity_type, $entity_id, $display_plugin = 'default', array $display_settings = []) {
    $entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->load($entity_id);
    $context = [
      'data-entity-type' => $entity_type,
      'data-entity-uuid' => $entity
        ->uuid(),
      'data-entity-embed-display' => $display_plugin,
      'data-entity-embed-display-settings' => $display_settings,
    ];
    return $this->builder
      ->buildEntityEmbed($entity, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityEmbedTwigExtension::$builder protected property The entity embed builder service.
EntityEmbedTwigExtension::$entityTypeManager protected property The entity type manager service.
EntityEmbedTwigExtension::create public static function
EntityEmbedTwigExtension::getFunctions public function
EntityEmbedTwigExtension::getName public function
EntityEmbedTwigExtension::getRenderArray public function Return the render array for an entity.
EntityEmbedTwigExtension::__construct public function Constructs a new EntityEmbedTwigExtension.