VersionWarning.php in CMS Content Sync 8
File
modules/cms_content_sync_developer/src/EventSubscriber/VersionWarning.php
View source
<?php
namespace Drupal\cms_content_sync_developer\EventSubscriber;
use Drupal\Component\Render\FormattableMarkup;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Link;
use Drupal\Core\Url;
class VersionWarning implements EventSubscriberInterface {
protected $config_factory;
protected $current_user;
protected $messenger;
public function __construct(ConfigFactory $config_factory, AccountProxyInterface $current_user, MessengerInterface $messenger) {
$this->config_factory = $config_factory;
$this->current_user = $current_user;
$this->messenger = $messenger;
}
public function showVersionWarning(GetResponseEvent $event) {
$current_user = $this->current_user;
if ($current_user
->hasPermission('administer cms content sync')) {
$config = $this->config_factory;
$messenger = $this->messenger;
$developer_config = $config
->getEditable('cms_content_sync.developer');
$version_mismatch = $developer_config
->get('version_mismatch');
if (!empty($version_mismatch)) {
$links = [];
foreach ($version_mismatch as $flow_id => $flow) {
$links[$flow_id] = Link::fromTextAndUrl($flow_id, Url::fromRoute('entity.cms_content_sync_flow.edit_form', [
'cms_content_sync_flow' => $flow_id,
], [
'absolute' => TRUE,
]))
->toString();
}
$mismatching_flow_labels = implode(',', $links);
$message = new TranslatableMarkup("You have to update the related flow(s) @flows to keep the content synchronization intact. Failing to update the config may break the synchronization and lead to damaged or missing content.", [
'@flows' => new FormattableMarkup($mismatching_flow_labels, []),
]);
$messenger
->addWarning($message);
}
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'showVersionWarning',
];
return $events;
}
}
Classes
Name |
Description |
VersionWarning |
A subscriber triggering a config when certain configuration changes. |