By slucero on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
9.1.x
Introduced in version:
9.1.x-beta8
Description:
A new update event (patternkit.pattern_update) is fired for event subscribers when the Pattern entity referenced by a block is updated. The new event is intended to allow developers and pattern library maintainers to alter existing content within a site to match new schema structures applied during the update process.
An example scenario where the property name "text" in the "@patternkit/atoms/example/src/example" pattern is to be changed to "title" might be addressed with an event subscriber as shown below:
my_module.services.yml
services:
my_module.example_update_subscriber:
class: Drupal\my_module\EventSubscriber\ExampleUpdateSubscriber
tags:
- { name: event_subscriber }
my_module/src/EventSubscriber/ExampleUpdateSubscriber
namespace Drupal\my_module\EventSubscriber;
use Drupal\patternkit\Event\PatternUpdateEvent;
use Drupal\patternkit\PatternkitEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* An event subscriber for the patternkit pattern update process.
*
* @see \Drupal\patternkit\PatternkitEvents
* @see \Drupal\patternkit\Event\PatternUpdateEvent
*/
class ExampleUpdateSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[PatternkitEvents::PATTERN_UPDATE] = ['onPatternUpdate', 200];
return $events;
}
/**
* React to the pattern update process and alter content if necessary.
*
* @param \Drupal\patternkit\Event\PatternUpdateEvent $event
* The pattern update event containing all update information.
*/
public function onPatternUpdate(PatternUpdateEvent $event): void {
$original_pattern = $event->getOriginalPattern();
// Abort here if this isn't an applicable pattern.
if ($original_pattern->getAssetId() !== '@patternkit/atoms/example/src/example') {
return;
}
$content = $event->getContent();
// Rename the text field if it exists in the content.
if (isset($content['text'])) {
$content['title'] = $content['text'];
unset($content['text']);
$event->setContent($content);
}
}
}
Impacts:
Module developers
Themers
Site templates, recipes and distribution developers