Article.php 2.07 KB
Newer Older
e0ipso's avatar
e0ipso committed
1
2
3
4
5
<?php

namespace Drupal\typed_entity_example\WrappedEntities;

use Drupal\Core\Entity\EntityInterface;
6
use Drupal\Core\Entity\EntityViewBuilderInterface;
e0ipso's avatar
e0ipso committed
7
use Drupal\Core\Messenger\MessengerInterface;
8
use Drupal\typed_entity\RepositoryManager;
e0ipso's avatar
e0ipso committed
9
use Drupal\typed_entity\WrappedEntities\WrappedEntityBase;
e0ipso's avatar
e0ipso committed
10
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
e0ipso's avatar
e0ipso committed
11
12
13
14
15
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * The wrapped entity for the article content type.
 */
16
final class Article extends WrappedEntityBase {
e0ipso's avatar
e0ipso committed
17
18
19
20
21
22
23
24
25
26
27

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  private $messenger;

  /**
   * Article constructor.
   *
e0ipso's avatar
e0ipso committed
28
29
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to wrap.
30
31
32
33
   * @param \Drupal\typed_entity\RepositoryManager $repository_manager
   *   The repository manager.
   * @param \Drupal\Core\Entity\EntityViewBuilderInterface $entity_view_builder
   *   The view builder.
e0ipso's avatar
e0ipso committed
34
35
36
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
37
38
  public function __construct(EntityInterface $entity, RepositoryManager $repository_manager, EntityViewBuilderInterface $entity_view_builder, MessengerInterface $messenger) {
    parent::__construct($entity, $repository_manager, $entity_view_builder);
e0ipso's avatar
e0ipso committed
39
40
    $this->messenger = $messenger;
  }
e0ipso's avatar
e0ipso committed
41
42
43
44
45

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, EntityInterface $entity) {
46
47
48
    $entity_view_builder = $container->get('entity_type.manager')
      ->getViewBuilder($entity->getEntityTypeId());
    $repository_manager = $container->get(RepositoryManager::class);
e0ipso's avatar
e0ipso committed
49
50
    return new static(
      $entity,
51
52
      $repository_manager,
      $entity_view_builder,
e0ipso's avatar
e0ipso committed
53
54
55
56
57
58
59
60
61
62
63
64
65
      $container->get('messenger')
    );
  }

  /**
   * {@inheritdoc}
   *
   * This is only overridden for educational purposes.
   */
  public function owner(): ?WrappedEntityInterface {
    $message = 'The owner was accessed for article:' . $this->getEntity()->id();
    $this->messenger->addMessage($message);
    return parent::owner();
e0ipso's avatar
e0ipso committed
66
67
68
  }

}