Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Last active December 19, 2019 22:29
Show Gist options
  • Save EclipseGc/8fedea274215893ecd6edb1bf263e934 to your computer and use it in GitHub Desktop.
Save EclipseGc/8fedea274215893ecd6edb1bf263e934 to your computer and use it in GitHub Desktop.
diff --git a/src/DependencyCalculatorEvents.php b/src/DependencyCalculatorEvents.php
index 957e455..0df63e7 100644
--- a/src/DependencyCalculatorEvents.php
+++ b/src/DependencyCalculatorEvents.php
@@ -38,6 +38,19 @@ final class DependencyCalculatorEvents {
*/
const FILTER_FIELDS = "depcalc_filter_fields";
+ /**
+ * The event fired against config entities for dependency calculation.
+ *
+ * The event listener method receives a
+ * \Drupal\depcalc\Event\FilterDependencyConfigEntityEvent instance.
+ *
+ * @Event
+ *
+ * @see \Drupal\depcalc\Event\FilterDependencyConfigEntityEvent
+ * @see \Drupal\depcalc\EventSubscriber\DependencyCollector\ConfigEntityDependencyCollector::onCalculateDependencies
+ */
+ const FILTER_CONFIG_ENTITIES = "depcalc_filter_config_entities";
+
/**
* Name of the event fired when dependencies from a Layout Builder component are calculated.
*
diff --git a/src/Event/FilterDependencyConfigEntityEvent.php b/src/Event/FilterDependencyConfigEntityEvent.php
new file mode 100644
index 0000000..bdb9a1a
--- /dev/null
+++ b/src/Event/FilterDependencyConfigEntityEvent.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Drupal\depcalc\Event;
+
+use Drupal\depcalc\DependentEntityWrapper;
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * Class FilterDependencyConfigEntityEvent
+ */
+class FilterDependencyConfigEntityEvent extends Event {
+
+ /**
+ * Whether or not to calculate this config entity as a dependency.
+ *
+ * @var bool
+ */
+ protected $calculate = TRUE;
+
+ /**
+ * The dependent entity wrapper.
+ *
+ * @var \Drupal\depcalc\DependentEntityWrapper
+ */
+ protected $wrapper;
+
+ /**
+ * FilterDependencyConfigEntityEvent constructor.
+ *
+ * @param \Drupal\depcalc\DependentEntityWrapper $wrapper
+ * The entity wrapper for calculation.
+ */
+ public function __construct(DependentEntityWrapper $wrapper) {
+ $this->wrapper = $wrapper;
+ }
+
+ /**
+ * Get the wrapper of the entity we are considering calculating.
+ *
+ * @return \Drupal\depcalc\DependentEntityWrapper
+ * The entity wrapper for calculation.
+ */
+ public function getWrapper() {
+ return $this->wrapper;
+ }
+
+ /**
+ * Get the entity we are considering calculating.
+ *
+ * @return \Drupal\Core\Entity\EntityInterface
+ * The entity for calculation.
+ */
+ public function getEntity() {
+ return $this->wrapper->getEntity();
+ }
+
+ /**
+ * Set whether if this config entity should be calculated for dependencies.
+ *
+ * @param bool $calculate
+ * Whether or not to calculate this entity.
+ */
+ public function setCalculable(bool $calculate) {
+ $this->calculate = $calculate;
+ }
+
+ /**
+ * Whether this config entity should be dependency calculated.
+ *
+ * @return bool
+ * Whether or not to calculate this entity.
+ */
+ public function isCalculable() {
+ return $this->calculate;
+ }
+
+}
diff --git a/src/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollector.php b/src/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollector.php
index 383c7f9..1adbd9c 100644
--- a/src/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollector.php
+++ b/src/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollector.php
@@ -7,6 +7,8 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\depcalc\DependencyCalculatorEvents;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\depcalc\Event\CalculateEntityDependenciesEvent;
+use Drupal\depcalc\Event\FilterDependencyConfigEntityEvent;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* The config dependency collector.
@@ -43,10 +45,14 @@ class ConfigEntityDependencyCollector extends BaseDependencyCollector {
*
* @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
* The dependency calculation event.
+ * @param string $event_name
+ * The name of the event.
+ * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
+ * The event dispatcher.
*
* @throws \Exception
*/
- public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
+ public function onCalculateDependencies(CalculateEntityDependenciesEvent $event, string $event_name, EventDispatcherInterface $dispatcher) {
$entity = $event->getEntity();
if ($entity instanceof ConfigEntityInterface) {
$wrapper = $event->getWrapper();
@@ -68,6 +74,11 @@ class ConfigEntityDependencyCollector extends BaseDependencyCollector {
$sub_entity = $this->configManager->loadConfigEntityByName($dependency);
if ($sub_entity) {
$sub_wrapper = new DependentEntityWrapper($sub_entity);
+ $config_dependency_event = new FilterDependencyConfigEntityEvent($sub_wrapper);
+ $dispatcher->dispatch(DependencyCalculatorEvents::FILTER_CONFIG_ENTITIES, $config_dependency_event);
+ if (!$config_dependency_event->isCalculable()) {
+ continue;
+ }
$local_dependencies = [];
$sub_dependencies = $this->getCalculator()->calculateDependencies($sub_wrapper, $event->getStack(), $local_dependencies);
unset($sub_dependencies['module']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment