class PrepareLayout in Drupal 9
Same name and namespace in other branches
- 10 core/modules/layout_builder/src/EventSubscriber/PrepareLayout.php \Drupal\layout_builder\EventSubscriber\PrepareLayout
An event subscriber to prepare section storage via the \Drupal\layout_builder\Event\PrepareLayoutEvent.
Hierarchy
- class \Drupal\layout_builder\EventSubscriber\PrepareLayout implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of PrepareLayout
See also
\Drupal\layout_builder\Event\PrepareLayoutEvent
\Drupal\layout_builder\Element\LayoutBuilder::prepareLayout()
1 string reference to 'PrepareLayout'
- layout_builder.services.yml in core/
modules/ layout_builder/ layout_builder.services.yml - core/modules/layout_builder/layout_builder.services.yml
1 service uses PrepareLayout
- layout_builder.element.prepare_layout in core/
modules/ layout_builder/ layout_builder.services.yml - Drupal\layout_builder\EventSubscriber\PrepareLayout
File
- core/
modules/ layout_builder/ src/ EventSubscriber/ PrepareLayout.php, line 20
Namespace
Drupal\layout_builder\EventSubscriberView source
class PrepareLayout implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The layout tempstore repository.
*
* @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
*/
protected $layoutTempstoreRepository;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new PrepareLayout.
*
* @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
* The tempstore repository.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[LayoutBuilderEvents::PREPARE_LAYOUT][] = [
'onPrepareLayout',
10,
];
return $events;
}
/**
* Prepares a layout for use in the UI.
*
* @param \Drupal\layout_builder\Event\PrepareLayoutEvent $event
* The prepare layout event.
*/
public function onPrepareLayout(PrepareLayoutEvent $event) {
$section_storage = $event
->getSectionStorage();
// If the layout has pending changes, add a warning.
if ($this->layoutTempstoreRepository
->has($section_storage)) {
$this->messenger
->addWarning($this
->t('You have unsaved changes.'));
}
else {
// If the layout is an override that has not yet been overridden, copy the
// sections from the corresponding default.
if ($section_storage instanceof OverridesSectionStorageInterface && !$section_storage
->isOverridden()) {
$sections = $section_storage
->getDefaultSectionStorage()
->getSections();
foreach ($sections as $section) {
$section_storage
->appendSection($section);
}
}
// Add storage to tempstore regardless of what the storage is.
$this->layoutTempstoreRepository
->set($section_storage);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PrepareLayout:: |
protected | property | The layout tempstore repository. | |
PrepareLayout:: |
protected | property | The messenger service. | |
PrepareLayout:: |
public static | function | ||
PrepareLayout:: |
public | function | Prepares a layout for use in the UI. | |
PrepareLayout:: |
public | function | Constructs a new PrepareLayout. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |