Skip to content

Commit a9e7f2e

Browse files
committed
feat: implement dependency injection
1 parent 0cb1ccb commit a9e7f2e

2 files changed

Lines changed: 83 additions & 9 deletions

File tree

src/Plugin/IslandoraMiradorPlugin/MiradorImageTools.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Drupal\islandora_mirador\Plugin\IslandoraMiradorPlugin;
44

55
use Drupal\islandora_mirador\IslandoraMiradorPluginPluginBase;
6+
use Drupal\Core\Config\ConfigFactoryInterface;
7+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
69

710
/**
811
* Plugin implementation of the islandora_mirador.
@@ -13,15 +16,49 @@
1316
* description = @Translation("MIrador image manipluation..")
1417
* )
1518
*/
16-
class MiradorImageTools extends IslandoraMiradorPluginPluginBase {
19+
class MiradorImageTools extends IslandoraMiradorPluginPluginBase implements ContainerFactoryPluginInterface {
20+
/**
21+
* The config factory.
22+
*
23+
* @var \Drupal\Core\Config\ConfigFactoryInterface
24+
*/
25+
protected $configFactory;
26+
27+
/**
28+
* Constructs a TextOverlay object.
29+
*
30+
* @param array $configuration
31+
* A configuration array containing information about the plugin instance.
32+
* @param string $plugin_id
33+
* The plugin_id for the plugin instance.
34+
* @param mixed $plugin_definition
35+
* The plugin implementation definition.
36+
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
37+
* The config factory.
38+
*/
39+
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
40+
parent::__construct($configuration, $plugin_id, $plugin_definition);
41+
$this->configFactory = $config_factory;
42+
}
1743

1844
/**
1945
* {@inheritdoc}
2046
*/
47+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48+
return new static(
49+
$configuration,
50+
$plugin_id,
51+
$plugin_definition,
52+
$container->get('config.factory')
53+
);
54+
}
55+
56+
/**
57+
* {@InheritDoc}
58+
*/
2159
public function windowConfigAlter(array &$windowConfig) {
2260
// Get the config to check if this plugin is enabled.
23-
$config = \Drupal::service('config.factory')
24-
->get('islandora_mirador.settings');
61+
$config = $this->configFactory->get('islandora_mirador.settings');
2562
$enabled_plugins = $config->get('mirador_enabled_plugins');
2663

2764
if (!empty($enabled_plugins['miradorImageToolsPlugin'])) {
@@ -35,4 +72,4 @@ public function windowConfigAlter(array &$windowConfig) {
3572
}
3673
}
3774

38-
}
75+
}

src/Plugin/IslandoraMiradorPlugin/TextOverlay.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Drupal\islandora_mirador\Plugin\IslandoraMiradorPlugin;
44

55
use Drupal\islandora_mirador\IslandoraMiradorPluginPluginBase;
6+
use Drupal\Core\Config\ConfigFactoryInterface;
7+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
69

710
/**
811
* Plugin implementation of the islandora_mirador.
@@ -13,17 +16,51 @@
1316
* description = @Translation("Mirador text overlay plugin for text selection and accessibility.")
1417
* )
1518
*/
16-
class TextOverlay extends IslandoraMiradorPluginPluginBase {
19+
class TextOverlay extends IslandoraMiradorPluginPluginBase implements ContainerFactoryPluginInterface {
20+
/**
21+
* The config factory.
22+
*
23+
* @var \Drupal\Core\Config\ConfigFactoryInterface
24+
*/
25+
protected $configFactory;
26+
27+
/**
28+
* Constructs a TextOverlay object.
29+
*
30+
* @param array $configuration
31+
* A configuration array containing information about the plugin instance.
32+
* @param string $plugin_id
33+
* The plugin_id for the plugin instance.
34+
* @param mixed $plugin_definition
35+
* The plugin implementation definition.
36+
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
37+
* The config factory.
38+
*/
39+
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
40+
parent::__construct($configuration, $plugin_id, $plugin_definition);
41+
$this->configFactory = $config_factory;
42+
}
1743

1844
/**
1945
* {@inheritdoc}
2046
*/
47+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48+
return new static(
49+
$configuration,
50+
$plugin_id,
51+
$plugin_definition,
52+
$container->get('config.factory')
53+
);
54+
}
55+
56+
/**
57+
* {@InheritDoc}
58+
*/
2159
public function windowConfigAlter(array &$windowConfig) {
2260
// Get the config to check if this plugin is enabled.
23-
$config = \Drupal::service('config.factory')
24-
->get('islandora_mirador.settings');
61+
$config = $this->configFactory->get('islandora_mirador.settings');
2562
$enabled_plugins = $config->get('mirador_enabled_plugins');
26-
63+
2764
if (!empty($enabled_plugins['textOverlayPlugin'])) {
2865
// Enabled config - checkbox is checked.
2966
$windowConfig['textOverlay'] = [
@@ -41,4 +78,4 @@ public function windowConfigAlter(array &$windowConfig) {
4178
}
4279
}
4380

44-
}
81+
}

0 commit comments

Comments
 (0)