diff --git a/core/modules/node/node.views_execution.inc b/core/modules/node/node.views_execution.inc index ead8702..acdadd8 100644 --- a/core/modules/node/node.views_execution.inc +++ b/core/modules/node/node.views_execution.inc @@ -5,6 +5,7 @@ * Provide views runtime hooks for node.module. */ +use Drupal\views\Analyzer; use Drupal\views\ViewExecutable; /** diff --git a/core/modules/views/lib/Drupal/views/EventSubscriber/ViewsImportValidation.php b/core/modules/views/lib/Drupal/views/EventSubscriber/ViewsImportValidation.php new file mode 100644 index 0000000..c6c3ed3 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/EventSubscriber/ViewsImportValidation.php @@ -0,0 +1,81 @@ +analyzer = $analyzer; + } + + + /** + * Validates imported views. + * + * @param \Drupal\Core\Config\ConfigImporterEvent $event + * The event to process + * + * @throws \Drupal\Core\Config\ConfigException + */ + public function onConfigImporterValidate(ConfigImporterEvent $event) { + foreach (array('create', 'update') as $op) { + foreach ($event->getConfigImporter()->getUnprocessed($op) as $name) { + // Only validate views. + if (strpos($name, 'views.view') === 0) { + $context = new ConfigContext(new EventDispatcher()); + $config = new Config($name, $event->getConfigImporter()->getStorageComparer()->getTargetStorage(), $context); + $view = new View($config->get(), 'view'); + + $executable = $view->get('executable'); + $messages = $this->analyzer->getMessages($executable); + foreach ($messages as $info) { + if ($info['type'] == 'error') { + throw new ConfigException($config['message']); + } + } + } + } + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events = array(); + $events[ConfigImporter::ID . '.validate'] = array('onConfigImporterValidate'); + return $events; + } + +} diff --git a/core/modules/views/views.services.yml b/core/modules/views/views.services.yml index 1629da5..eec0bd1 100644 --- a/core/modules/views/views.services.yml +++ b/core/modules/views/views.services.yml @@ -87,3 +87,8 @@ services: class: Drupal\views\ViewsAccessCheck tags: - { name: 'access_check' } + views.config_import.validator: + class: Drupal\views\EventSubscriber\ViewsImportValidation + arguments: ['@views.analyzer'] + tags: + - { name: event_subscriber }