class Image in SimpleAds 8
Image Ad type.
Plugin annotation
@SimpleAdsType(
id = "image",
name = @Translation("Image Ad")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\simpleads\SimpleAdsTypeBase implements SimpleAdsTypeInterface uses StringTranslationTrait
- class \Drupal\simpleads\Plugin\SimpleAds\Type\Image
- class \Drupal\simpleads\SimpleAdsTypeBase implements SimpleAdsTypeInterface uses StringTranslationTrait
Expanded class hierarchy of Image
File
- src/
Plugin/ SimpleAds/ Type/ Image.php, line 18
Namespace
Drupal\simpleads\Plugin\SimpleAds\TypeView source
class Image extends SimpleAdsTypeBase {
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function createFormSubmit($options, FormStateInterface $form_state, $type = NULL) {
if ($fid = reset($form_state
->getValue('image'))) {
$options['fid'] = $this
->saveImage($fid);
}
return $options;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function deleteFormSubmit($options, FormStateInterface $form_state, $type = NULL, $id = NULL) {
if (!empty($options['fid'])) {
$this
->deleteImage($options['fid']);
}
return $options;
}
/**
* {@inheritdoc}
*/
public function theme() {
return [
'image.simpleads' => [
'variables' => [],
],
];
}
/**
* {@inheritdoc}
*/
public function render() {
}
/**
* Save image as a file entity.
*/
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;
}
/**
* Delete file entity.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Image:: |
public | function |
Return ad form to create an ad. Overrides SimpleAdsTypeBase:: |
|
Image:: |
public | function |
Create an ad. Overrides SimpleAdsTypeBase:: |
|
Image:: |
public | function |
Delete an ad. Overrides SimpleAdsTypeBase:: |
|
Image:: |
protected | function | Delete file entity. | |
Image:: |
public | function |
Render an ad. Overrides SimpleAdsTypeBase:: |
|
Image:: |
protected | function | Save image as a file entity. | |
Image:: |
public | function |
Theme function for the ad. Overrides SimpleAdsTypeBase:: |
|
Image:: |
public | function |
Update an ad. Overrides SimpleAdsTypeBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
SimpleAdsTypeBase:: |
public | function |
Get plugin name. Overrides SimpleAdsTypeInterface:: |
|
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. |