View source
<?php
namespace Drupal\simpleads\Plugin\SimpleAds\Type;
use Drupal\simpleads\SimpleAdsTypeBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\simpleads\Ads;
class Image extends SimpleAdsTypeBase {
public function buildForm(array $form, FormStateInterface $form_state, $type = NULL, $id = NULL) {
$ad = (new Ads())
->setId($id)
->load();
$options = $ad
->getOptions(TRUE);
$form['image'] = [
'#title' => $this
->t('Advertisement Creative'),
'#type' => 'managed_file',
'#description' => $this
->t('Please upload advertisement image. Allowed extensions: gif png jpg jpeg'),
'#upload_location' => 'public://simpleads/image/',
'#required' => TRUE,
'#multiple' => FALSE,
'#upload_validators' => [
'file_validate_extensions' => [
'gif png jpg jpeg',
],
],
'#default_value' => !empty($options['fid']) ? [
$options['fid'],
] : '',
];
return $form;
}
public function createFormSubmit($options, FormStateInterface $form_state, $type = NULL) {
if ($fid = reset($form_state
->getValue('image'))) {
$options['fid'] = $this
->saveImage($fid);
}
return $options;
}
public function updateFormSubmit($options, FormStateInterface $form_state, $type = NULL, $id = NULL) {
if ($fid = reset($form_state
->getValue('image'))) {
if ($options['fid'] != $fid) {
$options['fid'] = $this
->saveImage($fid);
}
}
return $options;
}
public function deleteFormSubmit($options, FormStateInterface $form_state, $type = NULL, $id = NULL) {
if (!empty($options['fid'])) {
$this
->deleteImage($options['fid']);
}
return $options;
}
public function theme() {
return [
'image.simpleads' => [
'variables' => [],
],
];
}
public function render() {
}
protected function saveImage($fid) {
$file = File::load($fid);
$file
->setPermanent();
$file
->save();
$file_usage = \Drupal::service('file.usage');
$file_usage
->add($file, 'simpleads', 'file', $fid);
return $fid;
}
protected function deleteImage($fid) {
$file = File::load($fid);
$file_usage = \Drupal::service('file.usage');
$file_usage
->add($file, 'simpleads', 'file', $fid);
$file
->delete();
return $fid;
}
}