final class ExamplePreprocessorEventSubscriber in Hook Event Dispatcher 8
Class ExamplePreprocessorEventSubscriber.
Hierarchy
- class \Drupal\hook_event_dispatcher\Example\ExamplePreprocessorEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of ExamplePreprocessorEventSubscriber
File
- src/
Example/ ExamplePreprocessorEventSubscriber.php, line 12
Namespace
Drupal\hook_event_dispatcher\ExampleView source
final class ExamplePreprocessorEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
PagePreprocessEvent::name() => 'preprocessPage',
BlockPreprocessEvent::name() => 'preprocessBlock',
];
}
/**
* Preprocess a node page to set the node title as page title.
*
* @param \Drupal\hook_event_dispatcher\Event\Preprocess\PagePreprocessEvent $event
* Event.
*/
public function preprocessPage(PagePreprocessEvent $event) {
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\PageEventVariables $variables */
$variables = $event
->getVariables();
$node = $variables
->getNode();
if ($node === NULL) {
return;
}
$variables
->set('title', $node
->getTitle());
}
/**
* Preprocess blocks with field_contact_form and add contact-form-wrapper id.
*
* @param \Drupal\hook_event_dispatcher\Event\Preprocess\BlockPreprocessEvent $event
* Event.
*/
public function preprocessBlock(BlockPreprocessEvent $event) {
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\BlockEventVariables $variables */
$variables = $event
->getVariables();
if ($variables
->get('field_contact_form') === NULL) {
return;
}
$variables
->getByReference('attributes')['id'] = 'contact-form-wrapper';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExamplePreprocessorEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
ExamplePreprocessorEventSubscriber:: |
public | function | Preprocess blocks with field_contact_form and add contact-form-wrapper id. | |
ExamplePreprocessorEventSubscriber:: |
public | function | Preprocess a node page to set the node title as page title. |