Commit 34246f43 authored by alexpott's avatar alexpott

Issue #2096703 by mondrake, tim.plunkett | tstoeckler: Image toolkits should...

Issue #2096703 by mondrake, tim.plunkett | tstoeckler: Image toolkits should use PluginFormInterface and ContainerFactoryPluginInterface.
parent 6d8b0b81
......@@ -804,7 +804,8 @@ services:
- { name: event_subscriber }
image.toolkit.manager:
class: Drupal\Core\ImageToolkit\ImageToolkitManager
arguments: ['@container.namespaces', '@cache.discovery', '@config.factory', '@module_handler', '@image.toolkit.operation.manager', '@logger.channel.image']
arguments: ['@config.factory']
parent: default_plugin_manager
image.toolkit.operation.manager:
class: Drupal\Core\ImageToolkit\ImageToolkitOperationManager
arguments: ['@logger.channel.image']
......
......@@ -8,6 +8,8 @@
namespace Drupal\Core\ImageToolkit;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\Plugin\PluginBase;
use Psr\Log\LoggerInterface;
......@@ -22,6 +24,13 @@
*/
abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Image object this toolkit instance is tied to.
*
......@@ -57,13 +66,21 @@ abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterf
* The toolkit operation manager.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger) {
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->operationManager = $operation_manager;
$this->logger = $logger;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
/**
* {@inheritdoc}
*/
......
......@@ -7,9 +7,10 @@
namespace Drupal\Core\ImageToolkit;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* @defgroup image Image toolkits
......@@ -49,21 +50,7 @@
* @see \Drupal\Core\ImageToolkit\ImageToolkitManager
* @see plugin_api
*/
interface ImageToolkitInterface extends PluginInspectionInterface {
/**
* Retrieves the toolkit's settings form.
*
* @see system_image_toolkit_settings()
*/
public function settingsForm();
/**
* Handles submissions for toolkit's settings form.
*
* @see system_image_toolkit_settings_submit()
*/
public function settingsFormSubmit($form, FormStateInterface $form_state);
interface ImageToolkitInterface extends ContainerFactoryPluginInterface, PluginInspectionInterface, PluginFormInterface {
/**
* Sets the image object that this toolkit instance is tied to.
......
......@@ -11,8 +11,6 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Psr\Log\LoggerInterface;
/**
* Manages image toolkit plugins.
......@@ -31,20 +29,6 @@ class ImageToolkitManager extends DefaultPluginManager {
*/
protected $configFactory;
/**
* The image toolkit operation manager.
*
* @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface
*/
protected $operationManager;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructs the ImageToolkitManager object.
*
......@@ -53,22 +37,16 @@ class ImageToolkitManager extends DefaultPluginManager {
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
* The toolkit operation manager.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger) {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
parent::__construct('Plugin/ImageToolkit', $namespaces, $module_handler, 'Drupal\Core\ImageToolkit\Annotation\ImageToolkit');
$this->setCacheBackend($cache_backend, 'image_toolkit_plugins');
$this->configFactory = $config_factory;
$this->operationManager = $operation_manager;
$this->logger = $logger;
}
/**
......@@ -125,13 +103,4 @@ public function getAvailableToolkits() {
return $output;
}
/**
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = array()) {
$plugin_definition = $this->getDefinition($plugin_id);
$plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
return new $plugin_class($configuration, $plugin_id, $plugin_definition, $this->operationManager, $this->logger);
}
}
......@@ -21,7 +21,7 @@ class ImageToolkitForm extends ConfigFormBase {
/**
* An array containing currently available toolkits.
*
* @var array
* @var \Drupal\Core\ImageToolkit\ImageToolkitInterface[]
*/
protected $availableToolkits = array();
......@@ -87,12 +87,24 @@ public function buildForm(array $form, FormStateInterface $form_state) {
),
),
);
$form['image_toolkit_settings'][$id] += $toolkit->settingsForm();
$form['image_toolkit_settings'][$id] += $toolkit->buildConfigurationForm(array(), $form_state);
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
// Call the form validation handler for each of the toolkits.
foreach ($this->availableToolkits as $toolkit) {
$toolkit->validateConfigurationForm($form, $form_state);
}
}
/**
* {@inheritdoc}
*/
......@@ -103,7 +115,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
// Call the form submit handler for each of the toolkits.
foreach ($this->availableToolkits as $toolkit) {
$toolkit->settingsFormSubmit($form, $form_state);
$toolkit->submitConfigurationForm($form, $form_state);
}
parent::submitForm($form, $form_state);
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\ImageToolkit\ImageToolkitBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the GD2 toolkit for image manipulation within Drupal.
......@@ -50,6 +51,20 @@ class GDToolkit extends ImageToolkitBase {
*/
protected $preLoadInfo = NULL;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('image.toolkit.operation.manager'),
$container->get('logger.channel.image'),
$container->get('config.factory')
);
}
/**
* Sets the GD image resource.
*
......@@ -80,14 +95,14 @@ public function getResource() {
/**
* {@inheritdoc}
*/
public function settingsForm() {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['image_jpeg_quality'] = array(
'#type' => 'number',
'#title' => t('JPEG quality'),
'#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
'#min' => 0,
'#max' => 100,
'#default_value' => \Drupal::config('system.image.gd')->get('jpeg_quality'),
'#default_value' => $this->configFactory->get('system.image.gd')->get('jpeg_quality'),
'#field_suffix' => t('%'),
);
return $form;
......@@ -96,8 +111,8 @@ public function settingsForm() {
/**
* {@inheritdoc}
*/
public function settingsFormSubmit($form, FormStateInterface $form_state) {
\Drupal::config('system.image.gd')
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->get('system.image.gd')
->set('jpeg_quality', $form_state->getValue(array('gd', 'image_jpeg_quality')))
->save();
}
......@@ -164,7 +179,7 @@ public function save($destination) {
return FALSE;
}
if ($this->getType() == IMAGETYPE_JPEG) {
$success = $function($this->getResource(), $destination, \Drupal::config('system.image.gd')->get('jpeg_quality'));
$success = $function($this->getResource(), $destination, $this->configFactory->get('system.image.gd')->get('jpeg_quality'));
}
else {
// Always save PNG images with full transparency.
......
......@@ -63,6 +63,9 @@ function testToolkitSetupForm() {
$this->assertFieldByName('test[test_parameter]', '10');
// Test changing the test toolkit parameter.
$edit = array('test[test_parameter]' => '0');
$this->drupalPostForm(NULL, $edit, 'Save configuration');
$this->assertText(t('Test parameter should be different from 0.'), 'Validation error displayed.');
$edit = array('test[test_parameter]' => '20');
$this->drupalPostForm(NULL, $edit, 'Save configuration');
$this->assertEqual(\Drupal::config('system.image.test_toolkit')->get('test_parameter'), '20');
......
......@@ -8,8 +8,13 @@
namespace Drupal\image_test\Plugin\ImageToolkit;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\ImageToolkit\ImageToolkitBase;
use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface;
use Drupal\Core\State\StateInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a Test toolkit for image manipulation within Drupal.
......@@ -21,6 +26,13 @@
*/
class TestToolkit extends ImageToolkitBase {
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
*
......@@ -42,10 +54,48 @@ class TestToolkit extends ImageToolkitBase {
*/
protected $height;
/**
* Constructs a TestToolkit object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
* The toolkit operation manager.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\State\StateInterface $state
* The state key value store.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, StateInterface $state) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $operation_manager, $logger, $config_factory);
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public function settingsForm() {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('image.toolkit.operation.manager'),
$container->get('logger.channel.image'),
$container->get('config.factory'),
$container->get('state')
);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$this->logCall('settings', func_get_args());
$form['test_parameter'] = array(
'#type' => 'number',
......@@ -53,7 +103,7 @@ public function settingsForm() {
'#description' => $this->t('A toolkit parameter for testing purposes.'),
'#min' => 0,
'#max' => 100,
'#default_value' => \Drupal::config('system.image.test_toolkit')->get('test_parameter'),
'#default_value' => $this->configFactory->get('system.image.test_toolkit')->get('test_parameter'),
);
return $form;
}
......@@ -61,8 +111,17 @@ public function settingsForm() {
/**
* {@inheritdoc}
*/
public function settingsFormSubmit($form, FormStateInterface $form_state) {
\Drupal::config('system.image.test_toolkit')
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if ($form_state['values']['test']['test_parameter'] == 0) {
$form_state->setErrorByName('test][test_parameter', $this->t('Test parameter should be different from 0.'));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->get('system.image.test_toolkit')
->set('test_parameter', $form_state->getValue(array('test', 'test_parameter')))
->save();
}
......@@ -111,7 +170,7 @@ public function save($destination) {
* @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestGetAllCalls()
*/
protected function logCall($op, $args) {
$results = \Drupal::state()->get('image_test.results') ?: array();
$results = $this->state->get('image_test.results') ?: array();
$results[$op][] = $args;
// A call to apply is also logged under its operation name whereby the
// array of arguments are logged as separate arguments, this because at the
......@@ -120,7 +179,7 @@ protected function logCall($op, $args) {
$operation = array_shift($args);
$results[$operation][] = array_values(reset($args));
}
\Drupal::state()->set('image_test.results', $results);
$this->state->set('image_test.results', $results);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment