You are here

public function ImageOptimizerForm::buildForm in Fastly 8.3

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/ImageOptimizerForm.php, line 93

Class

ImageOptimizerForm
Class ImageOptimizerForm.

Namespace

Drupal\fastly\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('fastly.settings');
  if (!$this->vclHandler
    ->checkImageOptimizerStatus()) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Please contact your sales rep or send an email to support@fastly.com to request image optimization activation for your fastly service!'));
    return $form;
  }
  if ($config
    ->get('image_optimization') == 1) {
    if (!$this->api
      ->ioEnabled($config
      ->get('service_id'))) {
      $this
        ->messenger()
        ->addError($this
        ->t('You have Fastly image optimization enabled in configuration but you don\'t have it available on service!'));
    }
  }
  $form['io'] = [
    '#type' => 'markup',
    '#markup' => $this
      ->t('Turn on image optimizations and configure basic image settings. More details can be found <a target="_blank" href=":image_optimizer">here</a> ', [
      ':image_optimizer' => 'https://docs.fastly.com/en/guides/about-fastly-image-optimizer#setting-up-image-optimization',
    ]),
  ];
  $form['image_optimization'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable image optimization'),
    '#description' => $this
      ->t('Enabling image optimization will upload VCL file which will add X-Fastly-Imageopto-Api header to all images and thus enable Fastly Image optimization API.'),
    '#default_value' => $config
      ->get('image_optimization'),
    '#ajax' => array(
      'callback' => array(
        $this,
        'updateIOCallback',
      ),
      'event' => 'change',
    ),
    '#attached' => [
      'library' => [
        'core/jquery',
        'core/drupal.dialog.ajax',
      ],
    ],
  ];
  $form['optimize'] = [
    '#title' => t('Optimize'),
    '#type' => 'select',
    '#description' => $this
      ->t('Automatically applies optimal quality compression to produce an output image with as much visual fidelity as possible, while minimizing the file size. More details <a href=":url" target="_blank">here</a>.', [
      ':url' => 'https://docs.fastly.com/en/image-optimization-api/enable',
    ]),
    '#default_value' => $config
      ->get('optimize'),
    '#empty_option' => t('None'),
    '#options' => [
      'low' => 'low',
      'medium' => 'medium',
      'high' => 'high',
    ],
    '#states' => [
      'visible' => array(
        ':input[name="image_optimization"]' => array(
          'checked' => TRUE,
        ),
      ),
    ],
  ];
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced settings'),
    '#open' => FALSE,
  ];
  $form['advanced']['webp'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Auto WebP?'),
    '#default_value' => $config
      ->get('webp') ?: TRUE,
    '#states' => [
      'visible' => array(
        ':input[name="image_optimization"]' => array(
          'checked' => TRUE,
        ),
      ),
    ],
  ];
  $form['advanced']['webp_quality'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Default WebP (lossy) quality.'),
    '#min' => 1,
    '#max' => 100,
    '#step' => 1,
    '#default_value' => $config
      ->get('webp_quality') ?: 85,
    '#states' => [
      'visible' => array(
        ':input[name="image_optimization"]' => array(
          'checked' => TRUE,
        ),
      ),
    ],
  ];
  $form['advanced']['jpeg_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default JPEG format.'),
    '#options' => [
      'auto' => 'auto',
      'baseline' => 'baseline',
      'progressive' => 'progressive',
    ],
    '#default_value' => $config
      ->get('jpeg_type') ?: 'auto',
    '#states' => [
      'visible' => array(
        ':input[name="image_optimization"]' => array(
          'checked' => TRUE,
        ),
      ),
    ],
  ];
  $form['advanced']['jpeg_quality'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Default JPEG quality.'),
    '#min' => 1,
    '#max' => 100,
    '#step' => 1,
    '#default_value' => $config
      ->get('jpeg_quality') ?: 85,
    '#states' => [
      'visible' => array(
        ':input[name="image_optimization"]' => array(
          'checked' => TRUE,
        ),
      ),
    ],
  ];
  $form['advanced']['upscale'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow upscaling?'),
    '#default_value' => $config
      ->get('upscale') ?: FALSE,
    '#states' => [
      'visible' => array(
        ':input[name="image_optimization"]' => array(
          'checked' => TRUE,
        ),
      ),
    ],
  ];
  $form['advanced']['resize_filter'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Resize filter?'),
    '#options' => [
      'lanczos3' => 'lanczos3',
      'lanczos2' => 'lanczos2',
      'bicubic' => 'bicubic',
      'bilinear' => 'bilinear',
      'nearest' => 'nearest',
    ],
    '#default_value' => $config
      ->get('resize_filter') ?: 'lanczos3',
    '#states' => [
      'visible' => array(
        ':input[name="image_optimization"]' => array(
          'checked' => TRUE,
        ),
      ),
    ],
  ];
  return parent::buildForm($form, $form_state);
}