class ImageStyleEditForm
Same name in other branches
- 9 core/modules/image/src/Form/ImageStyleEditForm.php \Drupal\image\Form\ImageStyleEditForm
- 8.9.x core/modules/image/src/Form/ImageStyleEditForm.php \Drupal\image\Form\ImageStyleEditForm
- 10 core/modules/image/src/Form/ImageStyleEditForm.php \Drupal\image\Form\ImageStyleEditForm
Controller for image style edit form.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm extends \Drupal\Core\Form\FormBase implements \Drupal\Core\Entity\EntityFormInterface
- class \Drupal\image\Form\ImageStyleFormBase extends \Drupal\Core\Entity\EntityForm
- class \Drupal\image\Form\ImageStyleEditForm extends \Drupal\image\Form\ImageStyleFormBase
- class \Drupal\image\Form\ImageStyleFormBase extends \Drupal\Core\Entity\EntityForm
- class \Drupal\Core\Entity\EntityForm extends \Drupal\Core\Form\FormBase implements \Drupal\Core\Entity\EntityFormInterface
Expanded class hierarchy of ImageStyleEditForm
1 file declares its use of ImageStyleEditForm
- ImageStyle.php in core/
modules/ image/ src/ Entity/ ImageStyle.php
File
-
core/
modules/ image/ src/ Form/ ImageStyleEditForm.php, line 18
Namespace
Drupal\image\FormView source
class ImageStyleEditForm extends ImageStyleFormBase {
/**
* The image effect manager service.
*
* @var \Drupal\image\ImageEffectManager
*/
protected $imageEffectManager;
/**
* Constructs an ImageStyleEditForm object.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
* The storage.
* @param \Drupal\image\ImageEffectManager $image_effect_manager
* The image effect manager service.
*/
public function __construct(EntityStorageInterface $image_style_storage, ImageEffectManager $image_effect_manager) {
parent::__construct($image_style_storage);
$this->imageEffectManager = $image_effect_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager')
->getStorage('image_style'), $container->get('plugin.manager.image.effect'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$user_input = $form_state->getUserInput();
$form['#title'] = $this->t('Edit style %name', [
'%name' => $this->entity
->label(),
]);
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'image/admin';
// Show the thumbnail preview.
$preview_arguments = [
'#theme' => 'image_style_preview',
'#style' => $this->entity,
];
$form['preview'] = [
'#type' => 'item',
'#title' => $this->t('Preview (Click for actual images)'),
'#markup' => \Drupal::service('renderer')->render($preview_arguments),
// Render preview above parent elements.
'#weight' => -5,
];
// Build the list of existing image effects for this image style.
$form['effects'] = [
'#type' => 'table',
'#header' => [
$this->t('Effect'),
$this->t('Weight'),
$this->t('Operations'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'image-effect-order-weight',
],
],
'#attributes' => [
'id' => 'image-style-effects',
],
'#empty' => $this->t('There are currently no effects in this style. Add one by selecting an option below.'),
// Render effects below parent elements.
'#weight' => 5,
];
foreach ($this->entity
->getEffects() as $effect) {
$key = $effect->getUuid();
$form['effects'][$key]['#attributes']['class'][] = 'draggable';
$form['effects'][$key]['#weight'] = isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL;
$form['effects'][$key]['effect'] = [
'#tree' => FALSE,
'data' => [
'label' => [
'#plain_text' => $effect->label(),
],
],
];
$summary = $effect->getSummary();
if (!empty($summary)) {
$summary['#prefix'] = ' ';
$form['effects'][$key]['effect']['data']['summary'] = $summary;
}
$form['effects'][$key]['weight'] = [
'#type' => 'weight',
'#title' => $this->t('Weight for @title', [
'@title' => $effect->label(),
]),
'#title_display' => 'invisible',
'#default_value' => $effect->getWeight(),
'#attributes' => [
'class' => [
'image-effect-order-weight',
],
],
];
$links = [];
$is_configurable = $effect instanceof ConfigurableImageEffectInterface;
if ($is_configurable) {
$links['edit'] = [
'title' => $this->t('Edit'),
'url' => Url::fromRoute('image.effect_edit_form', [
'image_style' => $this->entity
->id(),
'image_effect' => $key,
]),
];
}
$links['delete'] = [
'title' => $this->t('Delete'),
'url' => Url::fromRoute('image.effect_delete', [
'image_style' => $this->entity
->id(),
'image_effect' => $key,
]),
];
$form['effects'][$key]['operations'] = [
'#type' => 'operations',
'#links' => $links,
];
}
// Build the new image effect addition form and add it to the effect list.
$new_effect_options = [];
$effects = $this->imageEffectManager
->getDefinitions();
uasort($effects, function ($a, $b) {
return Unicode::strcasecmp($a['label'], $b['label']);
});
foreach ($effects as $effect => $definition) {
$new_effect_options[$effect] = $definition['label'];
}
$form['effects']['new'] = [
'#tree' => FALSE,
'#weight' => $user_input['weight'] ?? NULL,
'#attributes' => [
'class' => [
'draggable',
],
],
];
$form['effects']['new']['effect'] = [
'data' => [
'new' => [
'#type' => 'select',
'#title' => $this->t('Effect'),
'#title_display' => 'invisible',
'#options' => $new_effect_options,
'#empty_option' => $this->t('Select a new effect'),
],
[
'add' => [
'#type' => 'submit',
'#value' => $this->t('Add'),
'#validate' => [
'::effectValidate',
],
'#submit' => [
'::submitForm',
'::effectSave',
],
],
],
],
'#prefix' => '<div class="image-style-new">',
'#suffix' => '</div>',
];
$form['effects']['new']['weight'] = [
'#type' => 'weight',
'#title' => $this->t('Weight for new effect'),
'#title_display' => 'invisible',
'#default_value' => count($this->entity
->getEffects()) + 1,
'#attributes' => [
'class' => [
'image-effect-order-weight',
],
],
];
$form['effects']['new']['operations'] = [
'data' => [],
];
return parent::form($form, $form_state);
}
/**
* Validate handler for image effect.
*/
public function effectValidate($form, FormStateInterface $form_state) {
if (!$form_state->getValue('new')) {
$form_state->setErrorByName('new', $this->t('Select an effect to add.'));
}
}
/**
* Submit handler for image effect.
*/
public function effectSave($form, FormStateInterface $form_state) {
$this->save($form, $form_state);
// Check if this field has any configuration options.
$effect = $this->imageEffectManager
->getDefinition($form_state->getValue('new'));
// Load the configuration form for this option.
if (is_subclass_of($effect['class'], '\\Drupal\\image\\ConfigurableImageEffectInterface')) {
$form_state->setRedirect('image.effect_add_form', [
'image_style' => $this->entity
->id(),
'image_effect' => $form_state->getValue('new'),
], [
'query' => [
'weight' => $form_state->getValue('weight'),
],
]);
$form_state->setIgnoreDestination();
}
else {
$effect = [
'id' => $effect['id'],
'data' => [],
'weight' => $form_state->getValue('weight'),
];
$effect_id = $this->entity
->addImageEffect($effect);
$this->entity
->save();
if (!empty($effect_id)) {
$this->messenger()
->addStatus($this->t('The image effect was successfully applied.'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Update image effect weights.
if (!$form_state->isValueEmpty('effects')) {
$this->updateEffectWeights($form_state->getValue('effects'));
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$this->messenger()
->addStatus($this->t('Changes to the style have been saved.'));
}
/**
* Updates image effect weights.
*
* @param array $effects
* Associative array with effects having effect uuid as keys and array
* with effect data as values.
*/
protected function updateEffectWeights(array $effects) {
foreach ($effects as $uuid => $effect_data) {
if ($this->entity
->getEffects()
->has($uuid)) {
$this->entity
->getEffect($uuid)
->setWeight($effect_data['weight']);
}
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
DependencySerializationTrait::$_entityStorages | protected | property | |||
DependencySerializationTrait::$_serviceIds | protected | property | |||
DependencySerializationTrait::__sleep | public | function | 1 | ||
DependencySerializationTrait::__wakeup | public | function | 2 | ||
EntityForm::$entityTypeManager | protected | property | The entity type manager. | 3 | |
EntityForm::$moduleHandler | protected | property | The module handler service. | 2 | |
EntityForm::$operation | protected | property | The name of the current operation. | ||
EntityForm::actions | protected | function | Returns an array of supported actions for the current entity form. | 36 | |
EntityForm::actionsElement | protected | function | Returns the action form element for the current entity form. | ||
EntityForm::afterBuild | public | function | Form element #after_build callback: Updates the entity with submitted data. | 1 | |
EntityForm::buildEntity | public | function | Overrides EntityFormInterface::buildEntity | 5 | |
EntityForm::buildForm | public | function | Overrides FormInterface::buildForm | 13 | |
EntityForm::copyFormValuesToEntity | protected | function | Copies top-level form values to entity properties. | 12 | |
EntityForm::getBaseFormId | public | function | Overrides BaseFormIdInterface::getBaseFormId | 4 | |
EntityForm::getEntity | public | function | Overrides EntityFormInterface::getEntity | ||
EntityForm::getEntityFromRouteMatch | public | function | Overrides EntityFormInterface::getEntityFromRouteMatch | 3 | |
EntityForm::getFormId | public | function | Overrides FormInterface::getFormId | 13 | |
EntityForm::getOperation | public | function | Overrides EntityFormInterface::getOperation | ||
EntityForm::init | protected | function | Initialize the form state and the entity before the first form build. | 3 | |
EntityForm::prepareEntity | protected | function | Prepares the entity object before the form is built first. | 3 | |
EntityForm::prepareInvokeAll | protected | function | Invokes the specified prepare hook variant. | ||
EntityForm::processForm | public | function | Process callback: assigns weights and hides extra fields. | ||
EntityForm::setEntity | public | function | Overrides EntityFormInterface::setEntity | ||
EntityForm::setEntityTypeManager | public | function | Overrides EntityFormInterface::setEntityTypeManager | ||
EntityForm::setModuleHandler | public | function | Overrides EntityFormInterface::setModuleHandler | ||
EntityForm::setOperation | public | function | Overrides EntityFormInterface::setOperation | ||
FormBase::$configFactory | protected | property | The config factory. | 2 | |
FormBase::$requestStack | protected | property | The request stack. | 1 | |
FormBase::$routeMatch | protected | property | The route match. | ||
FormBase::config | protected | function | Retrieves a configuration object. | ||
FormBase::configFactory | protected | function | Gets the config factory for this form. | 2 | |
FormBase::container | private | function | Returns the service container. | ||
FormBase::currentUser | protected | function | Gets the current user. | 2 | |
FormBase::getRequest | protected | function | Gets the request object. | ||
FormBase::getRouteMatch | protected | function | Gets the route match. | ||
FormBase::logger | protected | function | Gets the logger for a specific channel. | ||
FormBase::redirect | protected | function | Returns a redirect response object for the specified route. | ||
FormBase::resetConfigFactory | public | function | Resets the configuration factory. | ||
FormBase::setConfigFactory | public | function | Sets the config factory for this form. | ||
FormBase::setRequestStack | public | function | Sets the request stack object to use. | ||
FormBase::validateForm | public | function | Overrides FormInterface::validateForm | 59 | |
ImageStyleEditForm::$imageEffectManager | protected | property | The image effect manager service. | ||
ImageStyleEditForm::create | public static | function | Overrides ImageStyleFormBase::create | ||
ImageStyleEditForm::effectSave | public | function | Submit handler for image effect. | ||
ImageStyleEditForm::effectValidate | public | function | Validate handler for image effect. | ||
ImageStyleEditForm::form | public | function | Overrides ImageStyleFormBase::form | ||
ImageStyleEditForm::save | public | function | Overrides ImageStyleFormBase::save | ||
ImageStyleEditForm::submitForm | public | function | Overrides EntityForm::submitForm | ||
ImageStyleEditForm::updateEffectWeights | protected | function | Updates image effect weights. | ||
ImageStyleEditForm::__construct | public | function | Constructs an ImageStyleEditForm object. | Overrides ImageStyleFormBase::__construct | |
ImageStyleFormBase::$entity | protected | property | The entity being used by this form. | Overrides EntityForm::$entity | |
ImageStyleFormBase::$imageStyleStorage | protected | property | The image style entity storage. | ||
LoggerChannelTrait::$loggerFactory | protected | property | The logger channel factory service. | ||
LoggerChannelTrait::getLogger | protected | function | Gets the logger for a specific channel. | ||
LoggerChannelTrait::setLoggerFactory | public | function | Injects the logger channel factory. | ||
MessengerTrait::$messenger | protected | property | The messenger. | 16 | |
MessengerTrait::messenger | public | function | Gets the messenger. | 16 | |
MessengerTrait::setMessenger | public | function | Sets the messenger. | ||
RedirectDestinationTrait::$redirectDestination | protected | property | The redirect destination service. | 2 | |
RedirectDestinationTrait::getDestinationArray | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | ||
RedirectDestinationTrait::getRedirectDestination | protected | function | Returns the redirect destination service. | ||
RedirectDestinationTrait::setRedirectDestination | public | function | Sets the redirect destination service. | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.