You are here

protected function BatchWatermarkForm::addWatermarks in Media watermark 8

Batch helper function.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.

Return value

array An associative array defining the batch.

1 call to BatchWatermarkForm::addWatermarks()
BatchWatermarkForm::submitForm in src/Form/BatchWatermarkForm.php
Form submission handler.

File

src/Form/BatchWatermarkForm.php, line 318

Class

BatchWatermarkForm
Class BatchWatermarkForm.

Namespace

Drupal\media_watermark\Form

Code

protected function addWatermarks(FormStateInterface $form_state) {

  // We should not check if selected files empty because validation handle it.
  $file_fids = $this
    ->checkFiles($form_state
    ->getValue('files'));

  // Load files to add watermark.
  $files = File::loadMultiple($file_fids);

  // Get chosen watermark file id.
  $watermark_fid = $form_state
    ->getValue('watermarks_names');

  // Load watermarks object.
  $watermark = MediaWatermark::load($this
    ->getWatermarksOptions($watermark_fid));

  // Prepare batch operations array.
  foreach ($files as $file) {
    if (!empty($file)) {
      $operations[] = [
        [
          '\\Drupal\\media_watermark\\Watermark\\Watermark',
          'batchCreateImage',
        ],
        [
          $file,
          $watermark,
        ],
      ];
    }
  }
  if (!empty($operations)) {
    $batch = [
      'operations' => $operations,
      'title' => t('Adding multiple watermarks'),
      'init_message' => t('Adding multiple watermarks is starting.'),
      'progress_message' => t('Processed @current out of @total.'),
      'error_message' => t('Adding multiple watermarks has encountered an error.'),
    ];
    return $batch;
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Please select images to add watermarks'));
    return [];
  }
}