You are here

class Image in SimpleAds 8

Image Ad type.

Plugin annotation


@SimpleAdsType(
  id = "image",
  name = @Translation("Image Ad")
)

Hierarchy

Expanded class hierarchy of Image

File

src/Plugin/SimpleAds/Type/Image.php, line 18

Namespace

Drupal\simpleads\Plugin\SimpleAds\Type
View 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

Namesort descending Modifiers Type Description Overrides
Image::buildForm public function Return ad form to create an ad. Overrides SimpleAdsTypeBase::buildForm
Image::createFormSubmit public function Create an ad. Overrides SimpleAdsTypeBase::createFormSubmit
Image::deleteFormSubmit public function Delete an ad. Overrides SimpleAdsTypeBase::deleteFormSubmit
Image::deleteImage protected function Delete file entity.
Image::render public function Render an ad. Overrides SimpleAdsTypeBase::render
Image::saveImage protected function Save image as a file entity.
Image::theme public function Theme function for the ad. Overrides SimpleAdsTypeBase::theme
Image::updateFormSubmit public function Update an ad. Overrides SimpleAdsTypeBase::updateFormSubmit
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
SimpleAdsTypeBase::getName public function Get plugin name. Overrides SimpleAdsTypeInterface::getName
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.