class BatchWatermarkForm in Media watermark 8
Class BatchWatermarkForm.
@package Drupal\media_watermark\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\media_watermark\Form\BatchWatermarkForm
Expanded class hierarchy of BatchWatermarkForm
1 string reference to 'BatchWatermarkForm'
File
- src/
Form/ BatchWatermarkForm.php, line 19
Namespace
Drupal\media_watermark\FormView source
class BatchWatermarkForm extends FormBase {
/**
* Property to store watermarks options info.
*
* @var array
*/
protected $watermarksOptions = [];
/**
* Property to store watermarks file Ids.
*
* @var array
*/
protected $watermarksFids = [];
/**
* Helper to get Watermarks options array or watermark id.
*
* @param null|string|int $fid
* File id.
*
* @return array
* Watermark options.
*/
protected function getWatermarksOptions($fid = NULL) {
$this->watermarksOptions =& drupal_static(__CLASS__ . __FUNCTION__);
if (!isset($this->watermarksOptions)) {
$watermarks = MediaWatermark::loadMultiple();
$this->watermarksOptions = MediaWatermark::prepareNames($watermarks);
}
return !empty($this->watermarksOptions[$fid]) ? $this->watermarksOptions[$fid] : $this->watermarksOptions;
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'media_watermark_batch';
}
/**
* Define the form used for ContentEntityExample settings.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* An associative array containing the current state of the form.
*
* @return array
* Form definition array.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Set actions field set.
$form['actions'] = [
'#type' => 'fieldset',
'#collapsed' => FALSE,
'#collapsible' => FALSE,
];
$form['actions']['search_name'] = [
'#type' => 'textfield',
'#title' => t('Enter file name'),
'#default_value' => !empty($_GET['search']) ? $_GET['search'] : '',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Search'),
'#submit' => [
[
$this,
'searchRedirect',
],
],
];
$form['actions']['back'] = [
'#type' => 'submit',
'#value' => t('Reset'),
'#submit' => [
[
$this,
'resetRedirect',
],
],
];
// Get watermarks.
$watermarks = MediaWatermark::loadMultiple();
if (!empty($watermarks)) {
$names = $this
->getWatermarksOptions();
$form['actions']['watermarks_names'] = [
'#type' => 'select',
'#options' => $names,
'#description' => t('Choose watermark you need in dropdown'),
'#weight' => 19,
];
// Hide select list if one watermark.
if (count($watermarks) == 1) {
$form['actions']['watermarks_names']['#prefix'] = '<div class="hide-select-list">';
$form['actions']['watermarks_names']['#suffix'] = '</div>';
}
$form['actions']['watermarks_images'] = $this
->prepareImages($watermarks);
$form['#attached']['library'][] = 'media_watermark/media_watermark.scripts';
$this->watermarksFids = array_keys($names);
}
$form += $this
->getImageFiles();
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Add watermark'),
];
return $form;
}
/**
* Form validation handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* An associative array containing the current state of the form.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$fids = $this
->checkFiles($form_state
->getValue('files'));
if (empty($fids)) {
$form_state
->setErrorByName('files', $this
->t('Please select files to add watermarks'));
}
}
/**
* Helper to check file fids.
*
* @param array $fids
* File ids array.
*
* @return mixed
* File ids array.
*/
protected function checkFiles(array $fids) {
$file_fids = [];
foreach ($fids as $key => $fid) {
if (!empty($fid)) {
$file_fids[] = $fid;
}
}
return $file_fids;
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* An associative array containing the current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$batch = $this
->addWatermarks($form_state);
batch_set($batch);
}
/**
* Submit callback for search button.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* An associative array containing the current state of the form.
*/
protected function searchRedirect(array &$form, FormStateInterface $form_state) {
$search_name = $form_state
->getValue('search_name');
if (!empty($search_name) && is_string($search_name)) {
$form_state
->setRedirect('media_watermark.batch', [], [
'query' => [
'search' => $search_name,
],
]);
}
}
/**
* Submit callback for reset button.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* An associative array containing the current state of the form.
*/
protected function resetRedirect(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirect('media_watermark.batch');
}
/**
* Get files sortable table.
*
* @return array
* Renderable array.
*/
protected function getImageFiles() {
$admin_access = \Drupal::currentUser()
->hasPermission('administer files');
$header = $this
->buildHeader();
$result = $this
->getFiles($header);
$files = File::loadMultiple(array_keys($result));
$uids = [];
foreach ($files as $file) {
$uids[] = $file
->getOwnerId();
}
$accounts = !empty($uids) ? User::loadMultiple(array_unique($uids)) : [];
// Prepare the list of files.
$options = [];
foreach ($files as $file) {
$file_url = parse_url($file
->toUrl());
$options[$file
->id()] = [
'filename' => [
'data' => [
'#type' => 'link',
'#title' => $file
->getFilename(),
'#url' => Url::fromUserInput($file_url['path']),
'#attributes' => [
'target' => '_blank',
],
],
],
'image_preview' => [
'data' => [
'#theme' => 'image_style',
'#width' => 100,
'#height' => NULL,
'#style_name' => 'media_watermark',
'#uri' => $file
->getFileUri(),
],
],
'type' => 'image',
'filesize' => format_size($file
->getSize()),
'author' => [
'data' => [
'#type' => 'link',
'#title' => is_object($accounts[$file
->getOwnerId()]) ? $accounts[$file
->getOwnerId()]
->getUsername() : 'Anonymous',
'#url' => is_object($accounts[$file
->getOwnerId()]) ? Url::fromRoute('entity.user.canonical', [
'user' => $file
->getOwnerId(),
]) : Url::fromUserInput('/user/0'),
'#attributes' => [
'target' => '_blank',
],
],
],
'timestamp' => \Drupal::service('date.formatter')
->format($file
->getCreatedTime(), 'short'),
];
// Show a warning for files that do not exist.
if (@(!is_file($file
->getFileUri()))) {
$options[$file
->id()]['#attributes']['class'][] = 'error';
if ($file
->getFileUri()) {
$options[$file
->id()]['#attributes']['title'] = t('The stream wrapper for @scheme files is missing.', [
'@scheme' => FileSystem::uriScheme($file->uri),
]);
}
else {
$options[$file
->id()]['#attributes']['title'] = t('The file does not exist.');
}
}
}
// Only use a tableselect when the current user is able to perform any
// operations.
if ($admin_access) {
$form['files'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No files available.'),
'#multiple' => TRUE,
];
}
else {
$form['files'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $options,
'#empty' => t('No files available.'),
];
}
$form['pager'] = [
'#type' => 'pager',
];
return $form;
}
/**
* Batch helper function.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* An associative array containing the current state of the form.
*
* @return array
* An associative array defining the batch.
*/
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 [];
}
}
/**
* Helper to get image files.
*
* @param array $headers
* Table sort headers array.
*
* @return array
* List of file identifiers.
*/
protected function getFiles(array $headers) {
$query = \Drupal::entityQuery('file');
$query
->tableSort($headers);
// Load only image files.
$query
->condition('filemime', 'image/%', 'LIKE');
$query
->pager(50);
// Add condition to hide watermarks on batch add page.
if (!empty($this->watermarksFids)) {
$query
->condition('fid', $this->watermarksFids, 'NOT IN');
}
// TODO: prevent direct usage of $_GET.
if (!empty($_GET['search'])) {
$query
->condition('filename', $_GET['search'] . '%', 'LIKE');
}
$fids = $query
->execute();
return $fids;
}
/**
* Helper to get table sort header.
*
* @return array
* The sortable table header definition.
*/
protected function buildHeader() {
return [
'filename' => [
'data' => t('File name'),
'field' => 'filename',
'specifier' => 'filename',
],
'image_preview' => [
'data' => t('Image Preview'),
'field' => 'image_preview',
'specifier' => 'image_preview',
],
'type' => [
'data' => t('Type'),
],
'filesize' => [
'data' => t('Size'),
'field' => 'filesize',
'specifier' => 'filesize',
],
'author' => [
'data' => t('Author'),
'field' => 'uid',
'specifier' => 'uid',
],
'timestamp' => [
'data' => t('Updated'),
'field' => 'created',
'specifier' => 'created',
],
];
}
/**
* Helper to prepare images.
*
* @param \Drupal\media_watermark\Entity\MediaWatermarkInterface[] $watermarks
* List of Media Watermark entities.
*
* @return array
* Renderable array.
*/
protected function prepareImages(array $watermarks) {
$output = [];
// Build render array.
$output['images'] = [
'#type' => 'container',
'#prefix' => '<div id="edit-watermarks-images">',
'#suffix' => '</div>',
'#weight' => 18,
];
foreach ($watermarks as $watermark) {
$fids = $watermark
->getFid();
if (!empty($fids)) {
$fid = reset($fids);
$file = File::load($fid);
if (is_object($file)) {
$output['images']['image-' . $fid] = [
'#theme' => 'image_style',
'#width' => 200,
'#height' => NULL,
'#style_name' => 'media_watermark',
'#uri' => $file
->getFileUri(),
'#prefix' => '<div class="image-hidden" id="image-' . $fid . '">',
'#suffix' => '</div>',
];
}
}
}
return $output;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BatchWatermarkForm:: |
protected | property | Property to store watermarks file Ids. | |
BatchWatermarkForm:: |
protected | property | Property to store watermarks options info. | |
BatchWatermarkForm:: |
protected | function | Batch helper function. | |
BatchWatermarkForm:: |
public | function |
Define the form used for ContentEntityExample settings. Overrides FormInterface:: |
|
BatchWatermarkForm:: |
protected | function | Helper to get table sort header. | |
BatchWatermarkForm:: |
protected | function | Helper to check file fids. | |
BatchWatermarkForm:: |
protected | function | Helper to get image files. | |
BatchWatermarkForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
BatchWatermarkForm:: |
protected | function | Get files sortable table. | |
BatchWatermarkForm:: |
protected | function | Helper to get Watermarks options array or watermark id. | |
BatchWatermarkForm:: |
protected | function | Helper to prepare images. | |
BatchWatermarkForm:: |
protected | function | Submit callback for reset button. | |
BatchWatermarkForm:: |
protected | function | Submit callback for search button. | |
BatchWatermarkForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
BatchWatermarkForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |