ImageStyleFormBase.php in Drupal 9        
                          
                  
                        
  
  
  
  
File
  core/modules/image/src/Form/ImageStyleFormBase.php
  
    View source  
  <?php
namespace Drupal\image\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ImageStyleFormBase extends EntityForm {
  
  protected $entity;
  
  protected $imageStyleStorage;
  
  public function __construct(EntityStorageInterface $image_style_storage) {
    $this->imageStyleStorage = $image_style_storage;
  }
  
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('image_style'));
  }
  
  public function form(array $form, FormStateInterface $form_state) {
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Image style name'),
      '#default_value' => $this->entity
        ->label(),
      '#required' => TRUE,
    ];
    $form['name'] = [
      '#type' => 'machine_name',
      '#machine_name' => [
        'exists' => [
          $this->imageStyleStorage,
          'load',
        ],
      ],
      '#default_value' => $this->entity
        ->id(),
      '#required' => TRUE,
    ];
    return parent::form($form, $form_state);
  }
  
  public function save(array $form, FormStateInterface $form_state) {
    parent::save($form, $form_state);
    $form_state
      ->setRedirectUrl($this->entity
      ->toUrl('edit-form'));
  }
}