You are here

ImageZoomOptionsForm.php in Image Zoom 8.3

File

src/Form/ImageZoomOptionsForm.php
View source
<?php

namespace Drupal\imagezoom\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * Class ImageZoomOptionsForm.
 */
class ImageZoomOptionsForm extends EntityForm {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $imagezoom = $this->entity;
    $image_styles = image_style_options(FALSE);
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $imagezoom
        ->label(),
      '#description' => $this
        ->t("Label for the Image Zoom options profile."),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $imagezoom
        ->id(),
      '#machine_name' => [
        'exists' => '\\Drupal\\imagezoom\\Entity\\ImageZoomOptions::load',
      ],
      '#disabled' => !$imagezoom
        ->isNew(),
    ];
    $form['zoom_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Zoom type'),
      '#options' => [
        'window' => $this
          ->t('Window'),
        'inner' => $this
          ->t('Inner'),
        'lens' => $this
          ->t('Lens'),
      ],
      '#default_value' => $imagezoom
        ->getOption('zoom_type'),
    ];
    $form['display_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Image style'),
      '#options' => $image_styles,
      '#empty_option' => $this
        ->t('None (original image)'),
      '#default_value' => $imagezoom
        ->getOption('display_style'),
    ];
    $form['zoom_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Zoomed Image style'),
      '#options' => $image_styles,
      '#empty_option' => $this
        ->t('None (original image)'),
      '#default_value' => $imagezoom
        ->getOption('zoom_style'),
    ];
    $form['thumb_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Thumbnail image style'),
      '#options' => $image_styles,
      '#empty_option' => $this
        ->t('None (original image)'),
      '#default_value' => $imagezoom
        ->getOption('thumb_style'),
    ];
    $form['disable'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Disable zoom on small screens'),
      '#return_value' => 1,
      '#default_value' => $imagezoom
        ->getOption('disable'),
      '#weight' => 10,
    ];
    $form['disable_width'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Minimum width for zoom to display'),
      '#min' => 0,
      '#states' => [
        'invisible' => [
          ':input[name="disable"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
      '#default_value' => $imagezoom
        ->getOption('disable_width'),
      '#weight' => 10,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $imagezoom = $this->entity;
    $status = $imagezoom
      ->save();
    switch ($status) {
      case SAVED_NEW:
        drupal_set_message($this
          ->t('Created the %label Image Zoom options profile.', [
          '%label' => $imagezoom
            ->label(),
        ]));
        break;
      default:
        drupal_set_message($this
          ->t('Saved the %label Image Zoom options profile.', [
          '%label' => $imagezoom
            ->label(),
        ]));
    }
    $form_state
      ->setRedirectUrl($imagezoom
      ->toUrl('collection'));
  }

  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    $options = [];
    $values = $form_state
      ->getValues();
    foreach ($values as $key => $value) {
      if (in_array($key, [
        'label',
        'id',
      ])) {
        $entity
          ->set($key, $value);
      }
      else {
        $options[$key] = $value;
      }
    }
    $entity
      ->set('options', $options);
  }

}

Classes

Namesort descending Description
ImageZoomOptionsForm Class ImageZoomOptionsForm.