diff --git a/inmail.install b/inmail.install index 9f6f0c2..bb72393 100644 --- a/inmail.install +++ b/inmail.install @@ -34,16 +34,31 @@ function inmail_get_requirements() { /** @var \Drupal\inmail\Entity\PluginConfigEntity $entity */ foreach ($entities as $entity) { // Add requirements specific entity and plugin keys. + $plugin_id = $entity->getPluginId(); $entity_key = $plugin_type . '_entity_' . $entity->id(); - $plugin_key = $plugin_type . '_plugin_' . $entity->getPluginId(); + $plugin_key = $plugin_type . '_plugin_' . $plugin_id; + try { + /** @var \Drupal\inmail\PluginRequirementsInterface $plugin */ + $plugin = $entity->getPluginInstance(); - // Check entity instance requirements. - $requirements[$entity_key] = $entity->checkInstanceRequirements(); + // Check entity instance requirements. + $requirements[$entity_key] = $plugin->checkInstanceRequirements(); + // Add title in case requirements are not empty. + if (!empty($requirements[$entity_key])) { + $requirements[$entity_key]['title'] = $entity->label() . ' (' . $entity->id() . ')'; + } - // Check plugin requirements only if it is not already checked. - if (!isset($requirements[$plugin_key])) { - $plugin = $entity->getPluginInstance(); - $requirements[$plugin_key] = $plugin::checkPluginRequirements(); + // Check plugin requirements only if it is not already checked. + if (!isset($requirements[$plugin_key])) { + $requirements[$plugin_key] = $plugin::checkPluginRequirements(); + } + } + catch (\Drupal\Component\Plugin\Exception\PluginException $e) { + $requirements[$plugin_key] = [ + 'title' => t('Missing plugin @id', ['@id' => $plugin_id]), + 'description' => t('Inmail @type plugin @id is missing and cannot be used.', ['@type' => $plugin_type, '@id' => $plugin_id]), + 'severity' => REQUIREMENT_ERROR, + ]; } } } diff --git a/src/Entity/AnalyzerConfig.php b/src/Entity/AnalyzerConfig.php index 90e4873..e287449 100644 --- a/src/Entity/AnalyzerConfig.php +++ b/src/Entity/AnalyzerConfig.php @@ -43,18 +43,11 @@ namespace Drupal\inmail\Entity; class AnalyzerConfig extends PluginConfigEntity { /** - * Constructs a new AnalyzerConfig. + * The Inmail plugin type. * - * @param array $values - * An array of values to set, keyed by property name. If the entity type - * has bundles, the bundle key has to be specified. - * @param string $entity_type - * The type of the entity to create. + * @var string */ - public function __construct(array $values, $entity_type) { - parent::__construct($values, $entity_type); - $this->pluginType = 'analyzer'; - } + protected $pluginType = 'analyzer'; /** * The weight of the analyzer configuration. diff --git a/src/Entity/DelivererConfig.php b/src/Entity/DelivererConfig.php index 0cbed0a..797620c 100644 --- a/src/Entity/DelivererConfig.php +++ b/src/Entity/DelivererConfig.php @@ -42,16 +42,10 @@ namespace Drupal\inmail\Entity; class DelivererConfig extends PluginConfigEntity { /** - * Constructs a new DelivererConfig. + * The Inmail plugin type. * - * @param array $values - * An array of values to set, keyed by property name. If the entity type - * has bundles, the bundle key has to be specified. - * @param string $entity_type - * The type of the entity to create. + * @var string */ - public function __construct(array $values, $entity_type) { - parent::__construct($values, $entity_type); - $this->pluginType = 'deliverer'; - } + protected $pluginType = 'deliverer'; + } diff --git a/src/Entity/HandlerConfig.php b/src/Entity/HandlerConfig.php index 0344acb..5e97708 100644 --- a/src/Entity/HandlerConfig.php +++ b/src/Entity/HandlerConfig.php @@ -42,17 +42,10 @@ class HandlerConfig extends PluginConfigEntity { // @todo Implement HandlerConfig::calculateDependencies() https://www.drupal.org/node/2379929 /** - * Constructs a new HandlerConfig. + * The Inmail plugin type. * - * @param array $values - * An array of values to set, keyed by property name. If the entity type - * has bundles, the bundle key has to be specified. - * @param string $entity_type - * The type of the entity to create. + * @var string */ - public function __construct(array $values, $entity_type) { - parent::__construct($values, $entity_type); - $this->pluginType = 'handler'; - } + protected $pluginType = 'handler'; } diff --git a/src/Entity/PluginConfigEntity.php b/src/Entity/PluginConfigEntity.php index 125a6c6..1fe4c63 100644 --- a/src/Entity/PluginConfigEntity.php +++ b/src/Entity/PluginConfigEntity.php @@ -3,7 +3,6 @@ namespace Drupal\inmail\Entity; use Drupal\Core\Config\Entity\ConfigEntityBase; -use Drupal\inmail\PluginRequirementsInterface; /** * Defines a config entity skeleton for plugin configuration. @@ -46,13 +45,6 @@ abstract class PluginConfigEntity extends ConfigEntityBase { protected $pluginInstance; /** - * The Inmail plugin type - deliverer, analyzer, handler. - * - * @var string - */ - protected $pluginType; - - /** * Returns the plugin ID. * * @return string @@ -90,12 +82,15 @@ abstract class PluginConfigEntity extends ConfigEntityBase { /** * Returns the plugin instance. * - * @return \Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerInterface|\Drupal\inmail\Plugin\inmail\Deliverer\DelivererInterface|\Drupal\inmail\Plugin\inmail\Handler\HandlerInterface - * The instantiated plugin. + * @return \Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerInterface|\Drupal\inmail\Plugin\inmail\Deliverer\DelivererInterface|\Drupal\inmail\Plugin\inmail\Handler\HandlerInterface The instantiated plugin. + * The instantiated plugin. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + * Throws an exception in case of missing plugin. */ public function getPluginInstance() { if (empty($this->pluginInstance)) { - return \Drupal::service('plugin.manager.inmail.' . $this->pluginType)->createInstance($this->plugin, $this->configuration); + $this->pluginInstance = \Drupal::service('plugin.manager.inmail.' . $this->pluginType)->createInstance($this->plugin, $this->configuration); } return $this->pluginInstance; @@ -112,27 +107,6 @@ abstract class PluginConfigEntity extends ConfigEntityBase { } /** - * Checks requirements of the current configuration instance. - * - * @return array - * Returns the structured verbose output if requirements are not met. - * In case of no instance requirements, an empty array is returned. - */ - public function checkInstanceRequirements() { - $requirements = []; - if ($plugin = $this->getPluginInstance()) { - $requirements = $plugin->checkInstanceRequirements(); - } - - // Add title in case requirements are not empty. - if (!empty($requirements)) { - $requirements['title'] = $this->label(); - } - - return $requirements; - } - - /** * Flag determining whether a plugin is available to be used in processing. * * @return bool diff --git a/tests/src/Kernel/ProcessorTest.php b/tests/src/Kernel/ProcessorTest.php index 6ba43a4..126cc42 100644 --- a/tests/src/Kernel/ProcessorTest.php +++ b/tests/src/Kernel/ProcessorTest.php @@ -90,10 +90,9 @@ EOF; 'severity' => REQUIREMENT_ERROR, ], $plugin::checkPluginRequirements()); $this->assertEquals([ - 'title' => t('Unavailable Analyzer'), 'description' => t('Wrong instance configuration.'), 'severity' => REQUIREMENT_ERROR, - ], $unavailable_analyzer->checkInstanceRequirements()); + ], $plugin->checkInstanceRequirements()); $this->assertEquals(FALSE, $unavailable_analyzer->isAvailable()); // The body message has not changed. It implies UnavailableAnalyzer // did not run.