Skip to content
Snippets Groups Projects
Unverified Commit 6567f314 authored by Piet de Nooijer's avatar Piet de Nooijer Committed by GitHub
Browse files

Merge pull request #64 from pdenooijer/feature/entity_type_build

Add support for hook_entity_type_build
parents 05433750 f2064fd2
No related branches found
No related tags found
No related merge requests found
......@@ -34,6 +34,7 @@ use Drupal\hook_event_dispatcher\Event\EntityField\EntityFieldAccessEvent;
use Drupal\hook_event_dispatcher\Event\EntityType\EntityBaseFieldInfoAlterEvent;
use Drupal\hook_event_dispatcher\Event\EntityType\EntityBaseFieldInfoEvent;
use Drupal\hook_event_dispatcher\Event\EntityType\EntityBundleFieldInfoAlterEvent;
use Drupal\hook_event_dispatcher\Event\EntityType\EntityTypeBuildEvent;
use Drupal\hook_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\hook_event_dispatcher\Event\Form\FormBaseAlterEvent;
use Drupal\hook_event_dispatcher\Event\Form\FormIdAlterEvent;
......@@ -410,6 +411,18 @@ function hook_event_dispatcher_entity_bundle_field_info_alter(array &$fields, En
$manager->register($event);
}
/**
* Implements hook_entity_type_build().
*
* {@inheritdoc}
*/
function hook_event_dispatcher_entity_type_build(array &$entityTypes) {
/* @var \Drupal\hook_event_dispatcher\Manager\HookEventDispatcherManagerInterface $manager */
$manager = \Drupal::service('hook_event_dispatcher.manager');
$event = new EntityTypeBuildEvent($entityTypes);
$manager->register($event);
}
/**
* Implements hook_views_data().
*
......
<?php
namespace Drupal\hook_event_dispatcher\Event\EntityType;
use Drupal\hook_event_dispatcher\Event\EventInterface;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;
/**
* Class EntityTypeBuildEvent.
*/
class EntityTypeBuildEvent extends Event implements EventInterface {
/**
* Field info.
*
* @var array
*/
private $entityTypes;
/**
* EntityTypeBuildEvent constructor.
*
* @param array $entityTypes
* Extra field info.
*/
public function __construct(array &$entityTypes) {
$this->entityTypes = &$entityTypes;
}
/**
* {@inheritdoc}
*/
public function getDispatcherType() {
return HookEventDispatcherInterface::ENTITY_TYPE_BUILD;
}
/**
* Get the entity types.
*
* @return \Drupal\Core\Entity\EntityTypeInterface[]
* Entity types info.
*/
public function &getEntityTypes() {
return $this->entityTypes;
}
}
......@@ -215,6 +215,18 @@ interface HookEventDispatcherInterface {
*/
const ENTITY_BUNDLE_FIELD_INFO_ALTER = 'hook_event_dispatcher.entity_bundle.field_info_alter';
/**
* Add to entity type definitions..
*
* @Event
*
* @see hook_event_dispatcher_entity_type_build()
* @see hook_entity_type_build()
*
* @var string
*/
const ENTITY_TYPE_BUILD = 'hook_event_dispatcher.entity_type.build';
// FORM EVENTS.
/**
* Perform alterations before a form is rendered.
......
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\EntityType;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\hook_event_dispatcher\Event\EntityType\EntityTypeBuildEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use Drupal\Tests\UnitTestCase;
/**
* Class EntityTypeBuildEventTest.
*
* @package Drupal\Tests\hook_event_dispatcher\Unit\EntityType
*
* @group hook_event_dispatcher
*/
class EntityTypeBuildEventTest extends UnitTestCase {
/**
* The manager.
*
* @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
*/
private $manager;
/**
* {@inheritdoc}
*/
public function setUp() {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder->set('hook_event_dispatcher.manager', $this->manager);
$builder->compile();
\Drupal::setContainer($builder);
}
/**
* Test the EntityTypeBuildEvent.
*/
public function testEntityTypeBuildEvent() {
$this->manager->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_TYPE_BUILD => static function (EntityTypeBuildEvent $event) {
$entityTypes = &$event->getEntityTypes();
$entityTypes['my_custom_entity']->set('admin_permission', 'my custom permission');
},
]);
$entityType = $this->createMock(EntityTypeInterface::class);
$entityType->expects($this->once())
->method('set')
->with($this->equalTo('admin_permission'), $this->equalTo('my custom permission'));
$entityTypes = [
'my_custom_entity' => $entityType,
];
hook_event_dispatcher_entity_type_build($entityTypes);
/** @var \Drupal\hook_event_dispatcher\Event\EntityType\EntityTypeBuildEvent $event */
$event = $this->manager->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_TYPE_BUILD);
$this->assertEquals($entityTypes, $event->getEntityTypes());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment