Commit 5f317ad6 authored by alexpott's avatar alexpott

Issue #1788542 by claudiu.cristea, andypost, tim-e, fubhy, speely, heyrocker:...

Issue #1788542 by claudiu.cristea, andypost, tim-e, fubhy, speely, heyrocker: Use EntityFormController and EntityListController for image styles.
parent 42ebadca
...@@ -5,310 +5,6 @@ ...@@ -5,310 +5,6 @@
* Administration pages for image settings. * Administration pages for image settings.
*/ */
use Drupal\Component\Utility\String;
use Drupal\image\ConfigurableImageEffectInterface;
use Drupal\image\ImageStyleInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Menu callback; Listing of all current image styles.
*/
function image_style_list() {
$page = array();
$styles = entity_load_multiple('image_style');
$page['image_style_list'] = array(
'#markup' => theme('image_style_list', array('styles' => $styles)),
'#attached' => array(
'css' => array(drupal_get_path('module', 'image') . '/css/image.admin.css' => array()),
),
);
return $page;
}
/**
* Form builder; Edit an image style name and effects order.
*
* @param $form_state
* An associative array containing the current state of the form.
* @param $style
* An image style array.
* @ingroup forms
* @see image_style_form_submit()
*/
function image_style_form($form, &$form_state, ImageStyleInterface $style) {
$title = t('Edit style %name', array('%name' => $style->label()));
drupal_set_title($title, PASS_THROUGH);
$form_state['image_style'] = $style;
$form['#tree'] = TRUE;
$form['#attached']['css'][drupal_get_path('module', 'image') . '/css/image.admin.css'] = array();
// Show the thumbnail preview.
$form['preview'] = array(
'#type' => 'item',
'#title' => t('Preview'),
'#markup' => theme('image_style_preview', array('style' => $style)),
);
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Administrative label'),
'#default_value' => $style->label(),
'#required' => TRUE,
);
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => $style->id(),
'#machine_name' => array(
'exists' => 'image_style_load',
),
'#required' => TRUE,
);
// Build the list of existing image effects for this image style.
$form['effects'] = array(
'#theme' => 'image_style_effects',
);
foreach ($style->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(
'#markup' => String::checkPlain($effect->label()),
);
$form['effects'][$key]['summary'] = $effect->getSummary();
$form['effects'][$key]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array('@title' => $effect->label())),
'#title_display' => 'invisible',
'#default_value' => $effect->getWeight(),
);
$links = array();
$is_configurable = $effect instanceof ConfigurableImageEffectInterface;
if ($is_configurable) {
$links['edit'] = array(
'title' => t('edit'),
'href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key,
);
}
$links['delete'] = array(
'title' => t('delete'),
'href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key . '/delete',
);
$form['effects'][$key]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
$form['effects'][$key]['configure'] = array(
'#type' => 'link',
'#title' => t('edit'),
'#href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key,
'#access' => $is_configurable,
);
$form['effects'][$key]['remove'] = array(
'#type' => 'link',
'#title' => t('delete'),
'#href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key . '/delete',
);
}
// Build the new image effect addition form and add it to the effect list.
$new_effect_options = array();
$effects = Drupal::service('plugin.manager.image.effect')->getDefinitions();
uasort($effects, function ($a, $b) {
return strcasecmp($a['id'], $b['id']);
});
foreach ($effects as $effect => $definition) {
$new_effect_options[$effect] = $definition['label'];
}
$form['effects']['new'] = array(
'#tree' => FALSE,
'#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
);
$form['effects']['new']['new'] = array(
'#type' => 'select',
'#title' => t('Effect'),
'#title_display' => 'invisible',
'#options' => $new_effect_options,
'#empty_option' => t('Select a new effect'),
);
$form['effects']['new']['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for new effect'),
'#title_display' => 'invisible',
'#default_value' => count($form['effects']) - 1,
);
$form['effects']['new']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#validate' => array('image_style_form_add_validate'),
'#submit' => array('image_style_form_submit', 'image_style_form_add_submit'),
);
// Show the Override or Submit button for this style.
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update style'),
);
return $form;
}
/**
* Validate handler for adding a new image effect to an image style.
*/
function image_style_form_add_validate($form, &$form_state) {
if (!$form_state['values']['new']) {
form_error($form['effects']['new']['new'], t('Select an effect to add.'));
}
}
/**
* Submit handler for adding a new image effect to an image style.
*/
function image_style_form_add_submit($form, &$form_state) {
$style = $form_state['image_style'];
// Check if this field has any configuration options.
$effect = Drupal::service('plugin.manager.image.effect')->getDefinition($form_state['values']['new']);
// 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'];
$form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['weight'])));
}
// If there's no form, immediately add the image effect.
else {
$effect = array(
'id' => $effect['id'],
'data' => array(),
'weight' => $form_state['values']['weight'],
);
$style->saveImageEffect($effect);
drupal_set_message(t('The image effect was successfully applied.'));
}
}
/**
* Submit handler for saving an image style.
*/
function image_style_form_submit($form, &$form_state) {
$style = $form_state['image_style'];
// Update image effect weights.
if (!empty($form_state['values']['effects'])) {
foreach ($form_state['values']['effects'] as $uuid => $effect_data) {
if ($style->getEffects()->has($uuid)) {
$style->getEffect($uuid)->setWeight($effect_data['weight']);
}
}
}
$style->set('name', $form_state['values']['name']);
$style->set('label', $form_state['values']['label']);
$status = $style->save();
if ($status == SAVED_UPDATED) {
drupal_set_message(t('Changes to the style have been saved.'));
}
$form_state['redirect'] = 'admin/config/media/image-styles/manage/' . $style->id();
}
/**
* Form builder; Form for adding a new image style.
*
* @ingroup forms
* @see image_style_add_form_submit()
*/
function image_style_add_form($form, &$form_state) {
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Administrative label'),
'#default_value' => '',
'#required' => TRUE,
);
$form['name'] = array(
'#type' => 'machine_name',
'#machine_name' => array(
'exists' => 'image_style_load',
),
'#default_value' => '',
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create new style'),
);
return $form;
}
/**
* Submit handler for adding a new image style.
*/
function image_style_add_form_submit($form, &$form_state) {
$style = entity_create('image_style', array(
'name' => $form_state['values']['name'],
'label' => $form_state['values']['label'],
));
$style->save();
drupal_set_message(t('Style %name was created.', array('%name' => $style->label())));
$form_state['redirect'] = 'admin/config/media/image-styles/manage/' . $style->id();
}
/**
* Returns HTML for the page containing the list of image styles.
*
* @param $variables
* An associative array containing:
* - styles: An array of all the image styles returned by image_get_styles().
*
* @see image_get_styles()
* @ingroup themeable
*/
function theme_image_style_list($variables) {
$styles = $variables['styles'];
$header = array(t('Style name'), t('Operations'));
$rows = array();
foreach ($styles as $style) {
$row = array();
$row[] = l($style->label(), 'admin/config/media/image-styles/manage/' . $style->id());
$links = array();
$links['edit'] = array(
'title' => t('edit'),
'href' => 'admin/config/media/image-styles/manage/' . $style->id(),
'class' => array('image-style-link'),
);
$links['delete'] = array(
'title' => t('delete'),
'href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/delete',
'class' => array('image-style-link'),
);
$row[] = array(
'data' => array(
'#type' => 'operations',
'#links' => $links,
),
);
$rows[] = $row;
}
if (empty($rows)) {
$rows[] = array(array(
'colspan' => 4,
'data' => t('There are currently no styles. <a href="!url">Add a new one</a>.', array('!url' => url('admin/config/media/image-styles/add'))),
));
}
return theme('table', array('header' => $header, 'rows' => $rows));
}
/** /**
* Returns HTML for a listing of the effects within a specific image style. * Returns HTML for a listing of the effects within a specific image style.
* *
......
...@@ -96,42 +96,26 @@ function image_menu() { ...@@ -96,42 +96,26 @@ function image_menu() {
$items['admin/config/media/image-styles'] = array( $items['admin/config/media/image-styles'] = array(
'title' => 'Image styles', 'title' => 'Image styles',
'description' => 'Configure styles that can be used for resizing or adjusting images on display.', 'description' => 'Configure styles that can be used for resizing or adjusting images on display.',
'page callback' => 'image_style_list', 'route_name' => 'image_style_list',
'access arguments' => array('administer image styles'),
'file' => 'image.admin.inc',
); );
$items['admin/config/media/image-styles/list'] = array( $items['admin/config/media/image-styles/list'] = array(
'title' => 'List', 'title' => 'List',
'description' => 'List the current image styles on the site.', 'description' => 'List the current image styles on the site.',
'page callback' => 'image_style_list',
'access arguments' => array('administer image styles'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'image.admin.inc',
); );
$items['admin/config/media/image-styles/add'] = array( $items['admin/config/media/image-styles/add'] = array(
'title' => 'Add style', 'route_name' => 'image_style_add',
'description' => 'Add a new image style.', 'type' => MENU_SIBLING_LOCAL_TASK,
'page callback' => 'drupal_get_form', 'weight' => 1,
'page arguments' => array('image_style_add_form'),
'access arguments' => array('administer image styles'),
'type' => MENU_LOCAL_ACTION,
'weight' => 2,
'file' => 'image.admin.inc',
); );
$items['admin/config/media/image-styles/manage/%image_style'] = array( $items['admin/config/media/image-styles/manage/%image_style'] = array(
'title' => 'Edit style', 'title' => 'Edit style',
'title callback' => 'entity_page_label',
'title arguments' => array(5),
'description' => 'Configure an image style.', 'description' => 'Configure an image style.',
'page callback' => 'drupal_get_form', 'route_name' => 'image_style_edit',
'page arguments' => array('image_style_form', 5),
'access arguments' => array('administer image styles'),
'file' => 'image.admin.inc',
); );
$items['admin/config/media/image-styles/manage/%image_style/edit'] = array( $items['admin/config/media/image-styles/manage/%image_style/edit'] = array(
'title' => 'Edit', 'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK, 'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
); );
$items['admin/config/media/image-styles/manage/%image_style/delete'] = array( $items['admin/config/media/image-styles/manage/%image_style/delete'] = array(
'title' => 'Delete', 'title' => 'Delete',
...@@ -178,10 +162,6 @@ function image_theme() { ...@@ -178,10 +162,6 @@ function image_theme() {
), ),
// Theme functions in image.admin.inc. // Theme functions in image.admin.inc.
'image_style_list' => array(
'variables' => array('styles' => NULL),
'file' => 'image.admin.inc',
),
'image_style_effects' => array( 'image_style_effects' => array(
'render element' => 'form', 'render element' => 'form',
'file' => 'image.admin.inc', 'file' => 'image.admin.inc',
......
image_style_add:
pattern: '/admin/config/media/image-styles/add'
defaults:
_entity_form: image_style.add
requirements:
_permission: 'administer image styles'
image_style_edit:
pattern: '/admin/config/media/image-styles/manage/{image_style}'
defaults:
_entity_form: image_style.edit
requirements:
_permission: 'administer image styles'
image_style_delete: image_style_delete:
pattern: 'admin/config/media/image-styles/manage/{image_style}/delete' pattern: 'admin/config/media/image-styles/manage/{image_style}/delete'
defaults: defaults:
...@@ -12,6 +26,13 @@ image_effect_delete: ...@@ -12,6 +26,13 @@ image_effect_delete:
requirements: requirements:
_permission: 'administer image styles' _permission: 'administer image styles'
image_style_list:
pattern: '/admin/config/media/image-styles'
defaults:
_entity_list: 'image_style'
requirements:
_permission: 'administer image styles'
image_style_private: image_style_private:
pattern: '/system/files/styles/{image_style}/{scheme}' pattern: '/system/files/styles/{image_style}/{scheme}'
defaults: defaults:
......
<?php
/**
* @file
* Contains \Drupal\image\Form\ImageStyleAddForm.
*/
namespace Drupal\image\Form;
use Drupal\image\Form\ImageStyleFormBase;
/**
* Controller for image style addition forms.
*/
class ImageStyleAddForm extends ImageStyleFormBase {
/**
* {@inheritdoc}
*/
public function save(array $form, array &$form_state) {
parent::save($form, $form_state);
drupal_set_message($this->translator->translate('Style %name was created.', array('%name' => $this->entity->label())));
}
/**
* {@inheritdoc}
*/
public function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->translator->translate('Create new style');
return $actions;
}
}
<?php
/**
* @file
* Contains \Drupal\image\Form\ImageStyleEditForm.
*/
namespace Drupal\image\Form;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
use Drupal\image\ConfigurableImageEffectInterface;
use Drupal\image\Form\ImageStyleFormBase;
use Drupal\image\ImageEffectManager;
use Drupal\Component\Utility\String;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller for image style edit form.
*/
class ImageStyleEditForm extends ImageStyleFormBase {
/**
* The image effect manager service.
*
* @var \Drupal\image\ImageEffectManager
*/
protected $imageEffectManager;
/**
* Constructs an ImageStyleEditForm object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @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 $image_style_storage, TranslatorInterface $translator, ImageEffectManager $image_effect_manager) {
parent::__construct($module_handler, $image_style_storage, $translator);
$this->imageEffectManager = $image_effect_manager;
}
/**
* {@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'),
$container->get('plugin.manager.image.effect')
);
}
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
// @todo Remove drupal_set_title() in http://drupal.org/node/1981644
$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' => $this->entity);
$form['preview'] = array(
'#type' => 'item',
'#title' => $this->translator->translate('Preview'),
'#markup' => drupal_render($preview_arguments),
// 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 ($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(
'#markup' => String::checkPlain($effect->label()),
);
$form['effects'][$key]['summary'] = $effect->getSummary();
$form['effects'][$key]['weight'] = array(
'#type' => 'weight',
'#title' => $this->translator->translate('Weight for @title', array('@title' => $effect->label())),
'#title_display' => 'invisible',
'#default_value' => $effect->getWeight(),
);
$links = array();
$is_configurable = $effect instanceof ConfigurableImageEffectInterface;
if ($is_configurable) {
$links['edit'] = array(
'title' => $this->translator->translate('edit'),
'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/' . $this->entity->id() . '/effects/' . $key . '/delete',
);
$form['effects'][$key]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
$form['effects'][$key]['configure'] = array(
'#type' => 'link',
'#title' => $this->translator->translate('edit'),
'#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/' . $this->entity->id() . '/effects/' . $key . '/delete',
);
}
// Build the new image effect addition form and add it to the effect list.
$new_effect_options = array();
$effects = $this->imageEffectManager->getDefinitions();
uasort($effects, function ($a, $b) {
return strcasecmp($a['id'], $b['id']);
});
foreach ($effects as $effect => $definition) {
$new_effect_options[$effect] = $definition['label'];
}
$form['effects']['new'] = array(
'#tree' => FALSE,
'#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
);
$form['effects']['new']['new'] = array(
'#type' => 'select',
'#title' => $this->translator->translate('Effect'),
'#title_display' => 'invisible',
'#options' => $new_effect_options,
'#empty_option' => $this->translator->translate('Select a new effect'),
);
$form['effects']['new']['weight'] = array(
'#type' => 'weight',
'#title' => $this->translator->translate('Weight for new effect'),
'#title_display' => 'invisible',
'#default_value' => count($form['effects']) - 1,
);
$form['effects']['new']['add'] = array(
'#type' => 'submit',
'#value' => $this->translator->translate('Add'),
'#validate' => array(array($this, 'effectValidate')),
'#submit' => array(array($this, 'effectSave')),
);
return parent::form($form, $form_state);
}
/**
* Validate handler for image effect.
*/
public function effectValidate($form, &$form_state) {
if (!$form_state['values']['new']) {
form_error($form['effects']['new']['new'], $this->translator->translate('Select an effect to add.'));
}
}
/**
* Submit handler for image effect.
*/
public function effectSave($form, &$form_state) {
// Update image effect weights.
if (!empty($form_state['values']['effects'])) {
$this->updateEffectWeights($form_state['values']['effects']);
}
$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.'));
}
// Check if this field has any configuration options.
$effect = $this->imageEffectManager->getDefinition($form_state['values']['new']);
// Load the configuration form for this option.
if (is_subclass_of($effect['class'], '\Drupal\image\ConfigurableImageEffectInterface')) {
$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.
else {
$effect = array(
'id' => $effect['id'],
'data' => array(),
'weight' => $form_state['values']['weight'],
);
$effect_id = $this->entity->saveImageEffect($effect);
if (!empty($effect_id)) {
drupal_set_message($this->translator->translate('The image effect was successfully applied.'));
}
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, array &$form_state) {
// Update image effect weights.
if (!empty($form_state['values']['effects'])) {
$this->updateEffectWeights($form_state['values']['effects']);
}
parent::save($form, $form_state);
drupal_set_message($this->translator->translate('Changes to the style have been saved.'));
}
/**
* {@inheritdoc}
*/
public function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->translator->translate('Update style');
return $actions;
}
/**
* Updates image effect weights.
*
* @param array $effects
* Associative array with effects having effect uuid as keys and and array
* with effect data as values.
*/