class ExampleEntityExtraFieldInfoSubscribers in Hook Event Dispatcher 8
Class ExampleEntityExtraFieldInfoSubscribers.
Don't forget to define your class as a service and tag it as an "event_subscriber":
services: hook_event_dispatcher.example_entity_extra_field_info_subscribers: class:'\Drupal\hook_event_dispatcher\Example\ExampleEntityExtraFieldInfoSubscribers' tags:
- { name: 'event_subscriber' }
Hierarchy
- class \Drupal\hook_event_dispatcher\Example\ExampleEntityExtraFieldInfoSubscribers implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of ExampleEntityExtraFieldInfoSubscribers
File
- src/
Example/ ExampleEntityExtraFieldInfoSubscribers.php, line 22
Namespace
Drupal\hook_event_dispatcher\ExampleView source
class ExampleEntityExtraFieldInfoSubscribers implements EventSubscriberInterface {
/**
* Entity extra field info.
*
* @param \Drupal\hook_event_dispatcher\Event\EntityExtra\EntityExtraFieldInfoEvent $event
* The event.
*/
public function fieldInfo(EntityExtraFieldInfoEvent $event) {
// Set the field info directly.
$fieldInfo = [];
$event
->setFieldInfo($fieldInfo);
$entityType = 'node';
$bundle = 'page';
$fieldName = 'field_test';
$testFieldInfo = [];
// Add a single display info.
$event
->addDisplayFieldInfo($entityType, $bundle, $fieldName, $testFieldInfo);
// Add a single form info.
$event
->addFormFieldInfo($entityType, $bundle, $fieldName, $testFieldInfo);
}
/**
* Entity extra field info.
*
* @param \Drupal\hook_event_dispatcher\Event\EntityExtra\EntityExtraFieldInfoAlterEvent $event
* The event.
*/
public function fieldInfoAlter(EntityExtraFieldInfoAlterEvent $event) {
$fieldInfo =& $event
->getFieldInfo();
// Manipulate the field info.
$fieldInfo['node']['test']['display']['field_test']['weight'] = -20;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO => 'fieldInfo',
HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO_ALTER => 'fieldInfoAlter',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExampleEntityExtraFieldInfoSubscribers:: |
public | function | Entity extra field info. | |
ExampleEntityExtraFieldInfoSubscribers:: |
public | function | Entity extra field info. | |
ExampleEntityExtraFieldInfoSubscribers:: |
public static | function | Returns an array of event names this subscriber wants to listen to. |