diff --git a/core/modules/image/lib/Drupal/image/ImageStyleAddFormController.php b/core/modules/image/lib/Drupal/image/ImageStyleAddFormController.php index 289add9..e399a1c 100644 --- a/core/modules/image/lib/Drupal/image/ImageStyleAddFormController.php +++ b/core/modules/image/lib/Drupal/image/ImageStyleAddFormController.php @@ -7,95 +7,19 @@ namespace Drupal\image; -use Drupal\Core\Entity\EntityControllerInterface; -use Drupal\Core\Entity\EntityFormController; -use Drupal\Core\Entity\EntityStorageControllerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\StringTranslation\Translator\TranslatorInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\image\ImageStyleFormBase; /** - * Base form controller for image style addition forms. + * Controller for image style addition forms. */ -class ImageStyleAddFormController extends EntityFormController implements EntityControllerInterface { - - /** - * The storage controller. - * - * @var \Drupal\Core\Entity\EntityStorageControllerInterface - */ - protected $storageController; - - /** - * The translator service. - * - * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface - */ - protected $translator; - - /** - * Constructs an ImageStyleAddFormController object. - * - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. - * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage_controller - * The storage controller. - * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator - * The translator service. - */ - public function __construct(ModuleHandlerInterface $module_handler, EntityStorageControllerInterface $storage_controller, TranslatorInterface $translator) { - $this->moduleHandler = $module_handler; - $this->storageController = $storage_controller; - $this->translator = $translator; - parent::__construct($this->moduleHandler); - } - - /** - * {@inheritdoc} - */ - public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { - return new static( - $container->get('module_handler'), - $container->get('plugin.manager.entity')->getStorageController($entity_type), - $container->get('string_translation') - ); - } - - /** - * {@inheritdoc} - */ - public function form(array $form, array &$form_state) { - $form = parent::form($form, $form_state); - - $form['label'] = array( - '#type' => 'textfield', - '#title' => $this->translator->translate('Administrative label'), - '#default_value' => $this->entity->label(), - '#required' => TRUE, - '#weight' => 20, - ); - $form['name'] = array( - '#type' => 'machine_name', - '#machine_name' => array( - 'exists' => array($this->storageController, 'load'), - ), - '#default_value' => $this->entity->id(), - '#required' => TRUE, - '#weight' => 30, - ); - - return $form; - } +class ImageStyleAddFormController extends ImageStyleFormBase { /** * {@inheritdoc} */ public function save(array $form, array &$form_state) { - $this->entity->save(); + parent::save($form, $form_state); drupal_set_message($this->translator->translate('Style %name was created.', array('%name' => $this->entity->label()))); - $uri = $this->entity->uri(); - watchdog('image', 'Style %name was created.', array('%name' => $this->entity->label()), WATCHDOG_NOTICE, l($this->translator->translate('Edit'), $uri['path'])); - $form_state['redirect'] = 'admin/config/media/image-styles/manage/' . $this->entity->id(); } /** @@ -103,9 +27,7 @@ public function save(array $form, array &$form_state) { */ public function actions(array $form, array &$form_state) { $actions = parent::actions($form, $form_state); - $actions['submit']['#value'] = $this->translator->translate('Create new style'); - unset($actions['delete']); return $actions; } diff --git a/core/modules/image/lib/Drupal/image/ImageStyleFormBase.php b/core/modules/image/lib/Drupal/image/ImageStyleFormBase.php new file mode 100644 index 0000000..93a204e --- /dev/null +++ b/core/modules/image/lib/Drupal/image/ImageStyleFormBase.php @@ -0,0 +1,104 @@ +imageStyleStorage = $image_style_storage; + $this->translator = $translator; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { + return new static( + $container->get('module_handler'), + $container->get('plugin.manager.entity')->getStorageController($entity_type), + $container->get('string_translation') + ); + } + + /** + * {@inheritdoc} + */ + public function form(array $form, array &$form_state) { + + $form['label'] = array( + '#type' => 'textfield', + '#title' => $this->translator->translate('Image style name'), + '#default_value' => $this->entity->label(), + '#required' => TRUE, + ); + $form['name'] = array( + '#type' => 'machine_name', + '#machine_name' => array( + 'exists' => array($this->imageStyleStorage, 'load'), + ), + '#default_value' => $this->entity->id(), + '#required' => TRUE, + ); + + return parent::form($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function save(array $form, array &$form_state) { + $form_state['redirect'] = 'admin/config/media/image-styles/manage/' . $this->entity->id(); + + return $this->entity->save(); + } + + /** + * {@inheritdoc} + */ + public function actions(array $form, array &$form_state) { + $actions = parent::actions($form, $form_state); + unset($actions['delete']); + return $actions; + } + +} diff --git a/core/modules/image/lib/Drupal/image/ImageStyleFormController.php b/core/modules/image/lib/Drupal/image/ImageStyleFormController.php index 8a6fd3c..01d03ea 100644 --- a/core/modules/image/lib/Drupal/image/ImageStyleFormController.php +++ b/core/modules/image/lib/Drupal/image/ImageStyleFormController.php @@ -7,27 +7,18 @@ namespace Drupal\image; -use Drupal\Core\Entity\EntityControllerInterface; -use Drupal\Core\Entity\EntityFormController; use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; use Drupal\image\ImageEffectManager; -use Drupal\image\ImageStyleInterface; +use Drupal\image\ImageStyleFormBase; use Drupal\Component\Utility\String; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Base form controller for image style edit forms. + * Controller for image style edit form. */ -class ImageStyleFormController extends EntityFormController implements EntityControllerInterface { - - /** - * The storage controller. - * - * @var \Drupal\Core\Entity\EntityStorageControllerInterface - */ - protected $storageController; +class ImageStyleFormController extends ImageStyleFormBase { /** * The image effect manager service. @@ -37,30 +28,20 @@ class ImageStyleFormController extends EntityFormController implements EntityCon protected $imageEffectManager; /** - * The translator service. - * - * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface - */ - protected $translator; - - /** * Constructs an ImageStyleFormController object. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. - * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage_controller + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $image_style_storage * The storage controller. * @param \Drupal\image\ImageEffectManager $image_effect_manager * The image effect manager service. * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator * The translator service. */ - public function __construct(ModuleHandlerInterface $module_handler, EntityStorageControllerInterface $storage_controller, ImageEffectManager $image_effect_manager, TranslatorInterface $translator) { - $this->moduleHandler = $module_handler; - $this->storageController = $storage_controller; + public function __construct(ModuleHandlerInterface $module_handler, EntityStorageControllerInterface $image_style_storage, TranslatorInterface $translator, ImageEffectManager $image_effect_manager) { + parent::__construct($module_handler, $image_style_storage, $translator); $this->imageEffectManager = $image_effect_manager; - $this->translator = $translator; - parent::__construct($this->moduleHandler); } /** @@ -70,8 +51,8 @@ public static function createInstance(ContainerInterface $container, $entity_typ return new static( $container->get('module_handler'), $container->get('plugin.manager.entity')->getStorageController($entity_type), - $container->get('plugin.manager.image.effect'), - $container->get('string_translation') + $container->get('string_translation'), + $container->get('plugin.manager.image.effect') ); } @@ -79,44 +60,31 @@ public static function createInstance(ContainerInterface $container, $entity_typ * {@inheritdoc} */ public function form(array $form, array &$form_state) { - $form = parent::form($form, $form_state); - $style = $this->entity; // @todo Remove drupal_set_title() in http://drupal.org/node/1981644 - $title = $this->translator->translate('Edit style %name', array('%name' => $style->label())); + $title = $this->translator->translate('Edit style %name', array('%name' => $this->entity->label())); drupal_set_title($title, PASS_THROUGH); $form['#tree'] = TRUE; $form['#attached']['css'][drupal_get_path('module', 'image') . '/css/image.admin.css'] = array(); // Show the thumbnail preview. - $preview_arguments = array('#theme' => 'image_style_preview', '#style' => $style); + $preview_arguments = array('#theme' => 'image_style_preview', '#style' => $this->entity); $form['preview'] = array( '#type' => 'item', '#title' => $this->translator->translate('Preview'), '#markup' => drupal_render($preview_arguments), - ); - - $form['label'] = array( - '#type' => 'textfield', - '#title' => $this->translator->translate('Image style name'), - '#default_value' => $style->label(), - '#required' => TRUE, - ); - $form['name'] = array( - '#type' => 'machine_name', - '#machine_name' => array( - 'exists' => array($this->storageController, 'load'), - ), - '#default_value' => $style->id(), - '#required' => TRUE, + // Render preview above parent elements. + '#weight' => -5, ); // Build the list of existing image effects for this image style. $form['effects'] = array( '#theme' => 'image_style_effects', + // Render effects below parent elements. + '#weight' => 5, ); - foreach ($style->getEffects()->sort() as $effect) { + foreach ($this->entity->getEffects()->sort() as $effect) { $key = $effect->getUuid(); $form['effects'][$key]['#weight'] = isset($form_state['input']['effects']) ? $form_state['input']['effects'][$key]['weight'] : NULL; $form['effects'][$key]['label'] = array( @@ -135,12 +103,12 @@ public function form(array $form, array &$form_state) { if ($is_configurable) { $links['edit'] = array( 'title' => $this->translator->translate('edit'), - 'href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key, + 'href' => 'admin/config/media/image-styles/manage/' . $this->entity->id() . '/effects/' . $key, ); } $links['delete'] = array( 'title' => $this->translator->translate('delete'), - 'href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key . '/delete', + 'href' => 'admin/config/media/image-styles/manage/' . $this->entity->id() . '/effects/' . $key . '/delete', ); $form['effects'][$key]['operations'] = array( '#type' => 'operations', @@ -149,13 +117,13 @@ public function form(array $form, array &$form_state) { $form['effects'][$key]['configure'] = array( '#type' => 'link', '#title' => $this->translator->translate('edit'), - '#href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key, + '#href' => 'admin/config/media/image-styles/manage/' . $this->entity->id() . '/effects/' . $key, '#access' => $is_configurable, ); $form['effects'][$key]['remove'] = array( '#type' => 'link', '#title' => $this->translator->translate('delete'), - '#href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key . '/delete', + '#href' => 'admin/config/media/image-styles/manage/' . $this->entity->id() . '/effects/' . $key . '/delete', ); } @@ -192,7 +160,7 @@ public function form(array $form, array &$form_state) { '#submit' => array(array($this, 'effectSave')), ); - return $form; + return parent::form($form, $form_state); } /** @@ -208,16 +176,16 @@ public function effectValidate($form, &$form_state) { * Submit handler for image effect. */ public function effectSave($form, &$form_state) { - $style = $this->entity; // Update image effect weights. if (!empty($form_state['values']['effects'])) { - $this->updateEffectWeights($style, $form_state['values']['effects']); + $this->updateEffectWeights($form_state['values']['effects']); } - $style->set('name', $form_state['values']['name']); - $style->set('label', $form_state['values']['label']); - $status = $style->save(); + $this->entity->set('name', $form_state['values']['name']); + $this->entity->set('label', $form_state['values']['label']); + + $status = parent::save($form, $form_state); if ($status == SAVED_UPDATED) { drupal_set_message($this->translator->translate('Changes to the style have been saved.')); @@ -228,7 +196,7 @@ public function effectSave($form, &$form_state) { // Load the configuration form for this option. if (is_subclass_of($effect['class'], '\Drupal\image\ConfigurableImageEffectInterface')) { - $path = 'admin/config/media/image-styles/manage/' . $style->id() . '/add/' . $form_state['values']['new']; + $path = 'admin/config/media/image-styles/manage/' . $this->entity->id() . '/add/' . $form_state['values']['new']; $form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['weight']))); } // If there's no form, immediately add the image effect. @@ -238,11 +206,10 @@ public function effectSave($form, &$form_state) { 'data' => array(), 'weight' => $form_state['values']['weight'], ); - $effect_id = $style->saveImageEffect($effect); + $effect_id = $this->entity->saveImageEffect($effect); if (!empty($effect_id)) { drupal_set_message($this->translator->translate('The image effect was successfully applied.')); } - $form_state['redirect'] = 'admin/config/media/image-styles/manage/' . $style->id(); } } @@ -250,18 +217,14 @@ public function effectSave($form, &$form_state) { * {@inheritdoc} */ public function save(array $form, array &$form_state) { - $style = $this->entity; // Update image effect weights. if (!empty($form_state['values']['effects'])) { - $this->updateEffectWeights($style, $form_state['values']['effects']); + $this->updateEffectWeights($form_state['values']['effects']); } - $style->save(); + parent::save($form, $form_state); drupal_set_message($this->translator->translate('Changes to the style have been saved.')); - $uri = $this->entity->uri(); - watchdog('image', 'Style %label has been updated.', array('%label' => $this->entity->label()), WATCHDOG_NOTICE, l($this->translator->translate('Edit'), $uri['path'])); - $form_state['redirect'] = 'admin/config/media/image-styles/manage/' . $this->entity->id(); } /** @@ -269,26 +232,22 @@ public function save(array $form, array &$form_state) { */ public function actions(array $form, array &$form_state) { $actions = parent::actions($form, $form_state); - $actions['submit']['#value'] = $this->translator->translate('Update style'); - unset($actions['delete']); return $actions; } /** - * Update image effect weights. + * Updates image effect weights. * - * @param \Drupal\image\ImageStyleInterface $style - * The image style object to be updated. * @param array $effects * Associative array with effects having effect uuid as keys and and array * with effect data as values. */ - protected function updateEffectWeights(ImageStyleInterface $style, array $effects) { + protected function updateEffectWeights(array $effects) { foreach ($effects as $uuid => $effect_data) { - if ($style->getEffects()->has($uuid)) { - $style->getEffect($uuid)->setWeight($effect_data['weight']); + if ($this->entity->getEffects()->has($uuid)) { + $this->entity->getEffect($uuid)->setWeight($effect_data['weight']); } } } diff --git a/core/modules/image/lib/Drupal/image/ImageStyleListController.php b/core/modules/image/lib/Drupal/image/ImageStyleListController.php index df68293..28e0e29 100644 --- a/core/modules/image/lib/Drupal/image/ImageStyleListController.php +++ b/core/modules/image/lib/Drupal/image/ImageStyleListController.php @@ -14,7 +14,6 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Routing\PathBasedGeneratorInterface; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; -use Drupal\Component\Utility\String; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -43,8 +42,8 @@ class ImageStyleListController extends ConfigEntityListController implements Ent * The type of entity to be listed. * @param array $entity_info * An array of entity info for the entity type. - * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage - * The entity storage controller class. + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $image_style_storage + * The image style entity storage controller class. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler to invoke hooks on. * @param \Drupal\Core\Routing\PathBasedGeneratorInterface $url_generator @@ -52,8 +51,8 @@ class ImageStyleListController extends ConfigEntityListController implements Ent * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator * The translation manager. */ - public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler, PathBasedGeneratorInterface $url_generator, TranslatorInterface $translator) { - parent::__construct($entity_type, $entity_info, $storage, $module_handler); + public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $image_style_storage, ModuleHandlerInterface $module_handler, PathBasedGeneratorInterface $url_generator, TranslatorInterface $translator) { + parent::__construct($entity_type, $entity_info, $image_style_storage, $module_handler); $this->urlGenerator = $url_generator; $this->translator = $translator; } @@ -88,7 +87,7 @@ public function buildHeader() { public function buildRow(EntityInterface $entity) { $row = parent::buildRow($entity); unset($row['id']); - $row['label'] = String::checkPlain($entity->label()); + $row['label'] = $entity->label(); return $row; } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php index f34b246..0f38309 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php @@ -390,4 +390,16 @@ function testConfigImport() { $this->assertEqual($this->getImageCount($style), 0, 'Image style was flushed after being deleted by config import.'); } + /** + * Tests access for the image style listing. + */ + function testImageStyleAccess() { + $style = entity_create('image_style', array('name' => 'style_foo', 'label' => $this->randomString())); + $style->save(); + + $this->drupalGet('admin/config/media/image-styles'); + $this->clickLink(t('Edit')); + $this->assertRaw(t('Select a new effect')); + } + } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageStyleListTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageStyleListTest.php deleted file mode 100644 index 20a4210..0000000 --- a/core/modules/image/lib/Drupal/image/Tests/ImageStyleListTest.php +++ /dev/null @@ -1,57 +0,0 @@ - 'Image styles list', - 'description' => 'Tests access to overview page and operation links.', - 'group' => 'Image', - ); - } - - function setUp() { - parent::setUp(); - - $style = entity_create('image_style', array('name' => 'style_foo', 'label' => $this->randomString())); - $style->save(); - - $this->adminUser = $this->drupalCreateUser(array('administer image styles')); - $this->drupalLogin($this->adminUser); - } - - /** - * Tests access for the image style listing. - */ - function testImageStyleAccess() { - $this->drupalGet('admin/config/media/image-styles'); - $this->clickLink(t('Edit')); - } - -}