class ImageAPIOptimizePipelineEditForm in Image Optimize (or ImageAPI Optimize) 4.x
Same name and namespace in other branches
- 8.3 src/Form/ImageAPIOptimizePipelineEditForm.php \Drupal\imageapi_optimize\Form\ImageAPIOptimizePipelineEditForm
- 8.2 src/Form/ImageAPIOptimizePipelineEditForm.php \Drupal\imageapi_optimize\Form\ImageAPIOptimizePipelineEditForm
Controller for image optimize pipeline edit form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\imageapi_optimize\Form\ImageAPIOptimizePipelineFormBase
- class \Drupal\imageapi_optimize\Form\ImageAPIOptimizePipelineEditForm
- class \Drupal\imageapi_optimize\Form\ImageAPIOptimizePipelineFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of ImageAPIOptimizePipelineEditForm
File
- src/
Form/ ImageAPIOptimizePipelineEditForm.php, line 15
Namespace
Drupal\imageapi_optimize\FormView source
class ImageAPIOptimizePipelineEditForm extends ImageAPIOptimizePipelineFormBase {
/**
* The image optimize processor manager service.
*
* @var \Drupal\imageapi_optimize\ImageAPIOptimizeProcessorManager
*/
protected $imageAPIOptimizeProcessorManager;
/**
* Constructs an ImageAPIOptimizePipelineEditForm object.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $imageapi_optimize_pipeline_storage
* The storage.
* @param \Drupal\imageapi_optimize\ImageAPIOptimizeProcessorManager $imageapi_optimize_processor_manager
* The image optimize processor manager service.
*/
public function __construct(EntityStorageInterface $imageapi_optimize_pipeline_storage, ImageAPIOptimizeProcessorManager $imageapi_optimize_processor_manager) {
parent::__construct($imageapi_optimize_pipeline_storage);
$this->imageAPIOptimizeProcessorManager = $imageapi_optimize_processor_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('imageapi_optimize_pipeline'), $container
->get('plugin.manager.imageapi_optimize.processor'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
$form['#title'] = $this
->t('Edit pipeline %name', [
'%name' => $this->entity
->label(),
]);
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'imageapi_optimize/admin';
// Build the list of existing image processors for this image optimize pipeline.
$form['processors'] = [
'#type' => 'table',
'#header' => [
$this
->t('Processor'),
$this
->t('Weight'),
$this
->t('Operations'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'image-processor-order-weight',
],
],
'#attributes' => [
'id' => 'image-pipeline-processors',
],
'#empty' => t('There are currently no processors in this pipeline. Add one by selecting an option below.'),
// Render processors below parent elements.
'#weight' => 5,
];
foreach ($this->entity
->getProcessors() as $processor) {
$key = $processor
->getUuid();
$form['processors'][$key]['#attributes']['class'][] = 'draggable';
$form['processors'][$key]['#weight'] = isset($user_input['processors']) ? $user_input['processors'][$key]['weight'] : NULL;
$form['processors'][$key]['processor'] = [
'#tree' => FALSE,
'data' => [
'label' => [
'#plain_text' => $processor
->label(),
],
],
];
$summary = $processor
->getSummary();
if (!empty($summary)) {
$summary['#prefix'] = ' ';
$form['processors'][$key]['processor']['data']['summary'] = $summary;
}
$form['processors'][$key]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $processor
->label(),
]),
'#title_display' => 'invisible',
'#default_value' => $processor
->getWeight(),
'#attributes' => [
'class' => [
'image-processor-order-weight',
],
],
];
$links = [];
$is_configurable = $processor instanceof ConfigurableImageAPIOptimizeProcessorInterface;
if ($is_configurable) {
$links['edit'] = [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('imageapi_optimize.processor_edit_form', [
'imageapi_optimize_pipeline' => $this->entity
->id(),
'imageapi_optimize_processor' => $key,
]),
];
}
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('imageapi_optimize.processor_delete', [
'imageapi_optimize_pipeline' => $this->entity
->id(),
'imageapi_optimize_processor' => $key,
]),
];
$form['processors'][$key]['operations'] = [
'#type' => 'operations',
'#links' => $links,
];
}
// Build the new image processor addition form and add it to the processor list.
$new_processor_options = [];
$processors = $this->imageAPIOptimizeProcessorManager
->getDefinitions();
uasort($processors, function ($a, $b) {
return strcasecmp($a['id'], $b['id']);
});
foreach ($processors as $processor => $definition) {
$new_processor_options[$processor] = $definition['label'];
}
$form['processors']['new'] = [
'#tree' => FALSE,
'#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
'#attributes' => [
'class' => [
'draggable',
],
],
];
$form['processors']['new']['processor'] = [
'data' => [
'new' => [
'#type' => 'select',
'#title' => $this
->t('Processor'),
'#title_display' => 'invisible',
'#options' => $new_processor_options,
'#empty_option' => $this
->t('Select a new processor'),
],
[
'add' => [
'#type' => 'submit',
'#value' => $this
->t('Add'),
'#validate' => [
'::processorValidate',
],
'#submit' => [
'::submitForm',
'::processorSave',
],
],
],
],
'#prefix' => '<div class="image-pipeline-new">',
'#suffix' => '</div>',
];
$form['processors']['new']['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for new processor'),
'#title_display' => 'invisible',
'#default_value' => count($this->entity
->getProcessors()) + 1,
'#attributes' => [
'class' => [
'image-processor-order-weight',
],
],
];
$form['processors']['new']['operations'] = [
'data' => [],
];
return parent::form($form, $form_state);
}
/**
* Validate handler for image optimize processor.
*/
public function processorValidate($form, FormStateInterface $form_state) {
if (!$form_state
->getValue('new')) {
$form_state
->setErrorByName('new', $this
->t('Select a processor to add.'));
}
}
/**
* Submit handler for image optimize processor.
*/
public function processorSave($form, FormStateInterface $form_state) {
$this
->save($form, $form_state);
// Check if this field has any configuration options.
$processor = $this->imageAPIOptimizeProcessorManager
->getDefinition($form_state
->getValue('new'));
// Load the configuration form for this option.
if (is_subclass_of($processor['class'], '\\Drupal\\imageapi_optimize\\ConfigurableImageAPIOptimizeProcessorInterface')) {
$form_state
->setRedirect('imageapi_optimize.processor_add_form', [
'imageapi_optimize_pipeline' => $this->entity
->id(),
'imageapi_optimize_processor' => $form_state
->getValue('new'),
], [
'query' => [
'weight' => $form_state
->getValue('weight'),
],
]);
}
else {
$processor = [
'id' => $processor['id'],
'data' => [],
'weight' => $form_state
->getValue('weight'),
];
$processor_id = $this->entity
->addProcessor($processor);
$this->entity
->save();
if (!empty($processor_id)) {
$this
->messenger()
->addMessage($this
->t('The Image Optimize processor was successfully applied.'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Update image optimize processor weights.
if (!$form_state
->isValueEmpty('processors')) {
$this
->updateProcessorWeights($form_state
->getValue('processors'));
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$this
->messenger()
->addMessage($this
->t('Changes to the pipeline have been saved.'));
}
/**
* {@inheritdoc}
*/
public function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Update pipeline');
return $actions;
}
/**
* Updates image optimize processor weights.
*
* @param array $processors
* Associative array with processors having processor uuid as keys and array
* with processor data as values.
*/
protected function updateProcessorWeights(array $processors) {
foreach ($processors as $uuid => $processor_data) {
if ($this->entity
->getProcessors()
->has($uuid)) {
$this->entity
->getProcessor($uuid)
->setWeight($processor_data['weight']);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
13 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties. | 9 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
6 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
ImageAPIOptimizePipelineEditForm:: |
protected | property | The image optimize processor manager service. | |
ImageAPIOptimizePipelineEditForm:: |
public | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
ImageAPIOptimizePipelineEditForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ImageAPIOptimizePipelineFormBase:: |
|
ImageAPIOptimizePipelineEditForm:: |
public | function |
Gets the actual form array to be built. Overrides ImageAPIOptimizePipelineFormBase:: |
|
ImageAPIOptimizePipelineEditForm:: |
public | function | Submit handler for image optimize processor. | |
ImageAPIOptimizePipelineEditForm:: |
public | function | Validate handler for image optimize processor. | |
ImageAPIOptimizePipelineEditForm:: |
public | function |
Form submission handler for the 'save' action. Overrides ImageAPIOptimizePipelineFormBase:: |
|
ImageAPIOptimizePipelineEditForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
ImageAPIOptimizePipelineEditForm:: |
protected | function | Updates image optimize processor weights. | |
ImageAPIOptimizePipelineEditForm:: |
public | function |
Constructs an ImageAPIOptimizePipelineEditForm object. Overrides ImageAPIOptimizePipelineFormBase:: |
|
ImageAPIOptimizePipelineFormBase:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
|
ImageAPIOptimizePipelineFormBase:: |
protected | property | The image optimize pipeline entity storage. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |