Commit 1f32d12e authored by e0ipso's avatar e0ipso
Browse files

feat: add a user example

parent 0abba3a2
......@@ -3,19 +3,53 @@
namespace Drupal\typed_entity_example\WrappedEntities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\typed_entity\WrappedEntities\WrappedEntityBase;
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The wrapped entity for the article content type.
*/
class Article extends WrappedEntityBase {
final class Article extends WrappedEntityBase {
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
private $messenger;
/**
* Article constructor.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(EntityInterface $entity, MessengerInterface $messenger) {
parent::__construct($entity);
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, EntityInterface $entity) {
return new static($entity);
return new static(
$entity,
$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();
}
}
<?php
namespace Drupal\typed_entity_example\WrappedEntities;
use Drupal\typed_entity\WrappedEntities\WrappedEntityBase;
/**
* Wraps the user entity.
*/
final class User extends WrappedEntityBase {
public function nickname() {
// According to our stakeholders the nickname is the part before the @ in
// the registration email.
$email = $this->getEntity()->mail->value;
$parts = explode('@', $email);
return reset($parts);
}
}
......@@ -29,4 +29,10 @@ function typed_entity_example_node_view_alter(array &$build, EntityInterface $en
return $article->label();
}, $articles),
];
$author = $repository_collector->wrap($entity)
->owner()
->nickname();
$build['owner'] = [
'#markup' => '<p>' . t('The author is: %author', ['%author' => $author]) . '</p>',
];
}
......@@ -12,3 +12,12 @@ services:
entity_type_id: node
bundle: article
wrapper_class: Drupal\typed_entity_example\WrappedEntities\Article
typed_entity_example.user:
class: Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase
parent: Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase
public: true
tags:
-
name: typed_entity_repository
entity_type_id: user
wrapper_class: Drupal\typed_entity_example\WrappedEntities\User
......@@ -4,18 +4,12 @@ namespace Drupal\typed_entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryBase;
use Drupal\typed_entity\WrappedEntities\WrappedEntityInterface;
use Drupal\typed_entity\TypedRepositories\TypedEntityRepositoryInterface;
final class RepositoryCollector {
/**
* The separator between the entity type ID and the bundle name.
*
* @var string
*/
const SEPARATOR = ':';
/**
* The collected repositories.
*
......@@ -57,8 +51,8 @@ final class RepositoryCollector {
public function addRepository(
TypedEntityRepositoryInterface $repository,
string $entity_type_id,
string $bundle,
string $wrapper_class
string $wrapper_class,
string $bundle = ''
) {
if (empty($entity_type_id)) {
// We get an empty entity type ID when processing the parent service. We
......@@ -67,11 +61,7 @@ final class RepositoryCollector {
}
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$repository->init($entity_type, $bundle, $wrapper_class);
$identifier = implode(
static::SEPARATOR,
array_filter([$entity_type_id, $bundle])
);
$this->repositories[$identifier] = $repository;
$this->repositories[$repository->id()] = $repository;
}
/**
......@@ -90,7 +80,7 @@ final class RepositoryCollector {
*/
public function repositoryFromEntity(EntityInterface $entity): TypedEntityRepositoryInterface {
$identifier = implode(
static::SEPARATOR,
TypedEntityRepositoryBase::SEPARATOR,
array_filter([$entity->getEntityTypeId(), $entity->bundle()])
);
$repository = $this->repositories[$identifier];
......
......@@ -13,6 +13,13 @@ use UnexpectedValueException;
class TypedEntityRepositoryBase implements TypedEntityRepositoryInterface {
/**
* The separator between the entity type ID and the bundle name.
*
* @var string
*/
const SEPARATOR = ':';
/**
* The entity type manager.
*
......@@ -100,10 +107,22 @@ class TypedEntityRepositoryBase implements TypedEntityRepositoryInterface {
public function init(EntityTypeInterface $entity_type, string $bundle, string $wrapper_class): void {
$this->validateArguments($entity_type, $bundle, $wrapper_class);
$this->entityType = $entity_type;
$this->bundle = $bundle;
$this->bundle = $entity_type->getKey('bundle')
? $bundle
: $entity_type->id();
$this->wrapperClass = $wrapper_class;
}
/**
* {@inheritdoc}
*/
public function id(): string {
return implode(
static::SEPARATOR,
array_filter([$this->entityType->id(), $this->bundle])
);
}
/**
* {@inheritdoc}
*/
......@@ -134,10 +153,10 @@ class TypedEntityRepositoryBase implements TypedEntityRepositoryInterface {
->getBundleInfo($entity_type->id());
// When the entity type supports bundles, the bundle parameter is mandatory.
if (empty($bundle)) {
if (!empty($bundle_info)) {
if ($entity_type->getKey('bundle')) {
throw new UnexpectedValueException('Missing bundle for entity type "' . $entity_type->id() . '"');
}
return;
$bundle = $entity_type->id();
}
// Unless the entity is bundle-less the bundle should be valid for the given
// entity type.
......
......@@ -60,4 +60,12 @@ interface TypedEntityRepositoryInterface {
*/
public function getQuery(): QueryInterface;
/**
* Build the repository identifier.
*
* @return string
* The identifier.
*/
public function id(): string;
}
......@@ -6,6 +6,7 @@ namespace Drupal\typed_entity\WrappedEntities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\RepositoryCollector;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WrappedEntityBase implements WrappedEntityInterface {
......@@ -23,6 +24,13 @@ abstract class WrappedEntityBase implements WrappedEntityInterface {
$this->entity = $entity;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, EntityInterface $entity) {
return new static($entity);
}
/**
* {@inheritdoc}
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment