View source
<?php
namespace Drupal\media_watermark\Form;
use Drupal\Core\Entity\EntityForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
class MediaWatermarkForm extends EntityForm {
public function __construct(QueryFactory $entity_query) {
$this->entityQuery = $entity_query;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'));
}
public function form(array $form, FormStateInterface $form_state) {
$media_watermark = $this->entity;
$is_new = $media_watermark
->isNew();
$form = parent::form($form, $form_state);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $media_watermark
->label(),
'#description' => $this
->t("Media Watermark Label."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $media_watermark
->id(),
'#machine_name' => [
'exists' => [
$this,
'exist',
],
],
'#disabled' => !$is_new,
];
$form['fid'] = [
'#type' => 'managed_file',
'#title' => t('Watermark image'),
'#upload_location' => 'public://watermark',
'#upload_validators' => [
'file_validate_extensions' => [
'png gif',
],
],
'#description' => t('allowed files extensions are .png and .gif'),
'#required' => TRUE,
'#default_value' => $is_new ? '' : $media_watermark
->getFid(),
];
$form['horizontalPosition'] = [
'#type' => 'select',
'#title' => t('Horizontal position of watermark'),
'#options' => [
'left' => t('left'),
'middle' => t('middle'),
'right' => t('right'),
],
'#default_value' => $is_new ? 'left' : $media_watermark
->getHorizontalPosition(),
'#required' => TRUE,
];
$form['verticalPosition'] = [
'#type' => 'select',
'#title' => t('Vertical position of watermark'),
'#options' => [
'top' => t('top'),
'center' => t('center'),
'bottom' => t('bottom'),
],
'#default_value' => $is_new ? 'bottom' : $media_watermark
->getVerticalPosition(),
'#required' => TRUE,
];
$form['horizontalMargin'] = [
'#type' => 'textfield',
'#title' => t('Horizontal margin, px'),
'#default_value' => $is_new ? '0' : $media_watermark
->getHorizontalMargin(),
'#description' => t('Set minus or plus signed value for moving watermark to left or right from defined position.'),
'#required' => TRUE,
];
$form['verticalMargin'] = [
'#type' => 'textfield',
'#title' => t('Vertical margin, px'),
'#default_value' => $is_new ? '0' : $media_watermark
->getVerticalMargin(),
'#description' => t('Set minus or plus signed value for moving watermark to higher or lower from defined position.'),
'#required' => TRUE,
];
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$media_watermark = $this->entity;
$is_new = !$media_watermark
->getOriginalId();
$id = $media_watermark
->id();
if ($is_new) {
if (!$this
->exist($media_watermark
->id())) {
$machine_name = \Drupal::transliteration()
->transliterate($media_watermark
->label(), LanguageInterface::LANGCODE_DEFAULT, '_');
$media_watermark
->set('id', mb_strtolower($machine_name));
$status = $media_watermark
->save();
if ($status) {
$fids = $form_state
->getValue('fid');
$fid = reset($fids);
$this
->addFileUsage($form_state);
$this
->messenger()
->addStatus($this
->t('Saved the %label Media Watermark.', [
'%label' => $media_watermark
->label(),
]));
}
else {
$this
->messenger()
->addStatus($this
->t('The %label Media Watermark was not saved.', [
'%label' => $media_watermark
->label(),
]));
}
}
else {
$this
->messenger()
->addStatus($this
->t('The Media Watermark with same machine name already exists %name.', [
'%name' => $id,
]));
}
}
else {
$status = $media_watermark
->save();
if ($status) {
$fids = $form_state
->getValue('fid');
$fid = reset($fids);
$watermark_fid = $media_watermark
->getFid();
$watermark_fid = reset($watermark_fid);
if ($fid != $watermark_fid) {
$this
->addFileUsage($form_state);
}
}
else {
$this
->messenger()
->addStatus($this
->t('The %label Media Watermark was not saved.', [
'%label' => $media_watermark
->label(),
]));
}
}
$form_state
->setRedirect('media_watermark.list');
}
protected function addFileUsage(FormStateInterface $form_state) {
$media_watermark = $this->entity;
$id = $media_watermark
->id();
$fids = $form_state
->getValue('fid');
if (!empty($fids) && is_array($fids)) {
$fid = reset($fids);
if (!empty($fid) && is_numeric($fid)) {
$file = File::load($fid);
\Drupal::service('file.usage')
->add($file, 'media_watermark', 'media_watermark', $id);
}
}
}
public function exist($media_watermark_id) {
$entity = $this->entityQuery
->get('media_watermark')
->condition('id', $media_watermark_id)
->execute();
return (bool) $entity;
}
}