You are here

SettingsForm.php in Media PDF Thumbnail 8.2

Same filename and directory in other branches
  1. 8 src/Form/SettingsForm.php

File

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

namespace Drupal\media_pdf_thumbnail\Form;

use Drupal;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManager;

/**
 * Class SettingsForm.
 */
class SettingsForm extends FormBase {
  protected $entityTypeManager;
  protected $messenger;

  /**
   * SettingsForm constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
   * @param \Drupal\Core\Messenger\Messenger $messenger
   */
  public function __construct(EntityTypeManager $entityTypeManager, Messenger $messenger) {
    $this->entityTypeManager = $entityTypeManager;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Regenerate thumbnails'),
      '#description' => $this
        ->t('Regenrate thumbnail images for all medias.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $mids = $this->entityTypeManager
      ->getStorage('media')
      ->getQuery()
      ->condition('bundle', 'file')
      ->execute();
    $operations = [];
    $total = count($mids);
    foreach ($mids as $mid) {
      $operations[] = [
        '\\Drupal\\media_pdf_thumbnail\\Form\\settingsForm::regenerateThumbnails',
        [
          $mid,
        ],
      ];
    }
    $batch = [
      'title' => t('Regenerates media thumbnails'),
      'operations' => [
        $operations,
      ],
      'init_message' => t('Thumbnail creating process is starting.'),
      'progress_message' => t('Processed @current out of @total. Estimated time: @estimate.'),
    ];
    batch_set($batch);
    $this->messenger
      ->addMessage(t($total . ' image(s) have been generated'), 'success');
  }

  /**
   * RegenerateThumbnails.
   *
   * @param $mid
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function regenerateThumbnails($mid) {
    $media = $this->entityTypeManager
      ->getStorage('media')
      ->load($mid);
    $media
      ->save();
  }

}

Classes

Namesort descending Description
SettingsForm Class SettingsForm.