You are here

class ImageDTO in PhotoSwipe 3.x

Contains image item.

Hierarchy

Expanded class hierarchy of ImageDTO

File

src/ImageDTO.php, line 12

Namespace

Drupal\photoswipe
View source
class ImageDTO {

  /**
   * Image dimensions.
   */
  const HEIGHT = 'height';
  const WIDTH = 'width';

  /**
   * Preprocesed image settings.
   *
   * @var array
   */
  protected $settings;

  /**
   * Item.
   *
   * @var mixed
   */
  protected $item;

  /**
   * Variables.
   *
   * @var array
   */
  protected $variables;

  /**
   * Image url.
   *
   * @var string|null
   */
  protected $uri;

  /**
   * Image title.
   *
   * @var null|string
   */
  protected $title;

  /**
   * Image alt text.
   *
   * @var null|string
   */
  protected $alt;

  /**
   * Entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * Caption.
   *
   * @var string|null
   */
  protected $caption;

  /**
   * Image dimensions.
   *
   * @var array
   */
  protected $dimensions = [
    self::HEIGHT => 150,
    self::WIDTH => 150,
  ];

  /**
   * The path to the image that will show in Photoswipe.
   *
   * @var string
   */
  protected $path;

  /**
   * Construct new ImageDTO object.
   *
   * @param array $variables
   *   Variables from which fetch the image information.
   */
  public function __construct(array $variables) {
    $this->settings = $variables['display_settings'];

    // In case if entity is instance of Media use referenced field provided
    // specified by user.
    $this->item = ($item = $variables['item']) && ($item->entity instanceof MediaInterface || $item->entity instanceof MediaEntityInterface) && $item->entity
      ->hasField($this->settings['photoswipe_reference_image_field']) ? $item->entity
      ->get($this->settings['photoswipe_reference_image_field']) : $item;
    !empty($item->alt) ? $this->alt = $item->alt : !empty($item->field_file_image_alt_text[Language::LANGCODE_NOT_SPECIFIED]) && ($this->alt = $item->field_file_image_alt_text[Language::LANGCODE_NOT_SPECIFIED][0]['value']);
    !empty($item->title) ? $this->item = $item->title : !empty($item->field_file_image_title_text[Language::LANGCODE_NOT_SPECIFIED]) && ($this->item = $item->field_file_image_title_text[Language::LANGCODE_NOT_SPECIFIED][0]['value']);
    $this->uri = $this->item->entity
      ->getFileUri();
    $this->entity = $variables['entity'];
  }

  /**
   * Create from variables.
   *
   * @param array $variables
   *   Variables.
   *
   * @return self
   *   New self.
   */
  public static function createFromVariables(array $variables) {
    return new static($variables);
  }

  /**
   * Get item settings.
   *
   * @return array
   *   Settings.
   */
  public function getSettings() {
    return $this->settings;
  }

  /**
   * Get item.
   *
   * @return mixed
   *   Item.
   */
  public function getItem() {
    return $this->item;
  }

  /**
   * Get entity.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   Entity.
   */
  public function getEntity() {
    return $this->entity;
  }

  /**
   * Get image alt.
   *
   * @return string|null
   *   Image alternative text.
   */
  public function getAlt() {
    return $this->alt;
  }

  /**
   * Get image uri.
   *
   * @return string|null
   *   Image uri.
   */
  public function getUri() {
    return $this->uri;
  }

  /**
   * Get Caption.
   *
   * @return string|null
   *   Image caption.
   */
  public function getCaption() {
    return $this->caption;
  }

  /**
   * Get image title.
   *
   * @return string|null
   *   Image title.
   */
  public function getTitle() {
    return $this->title;
  }

  /**
   * Get image dimensions.
   *
   * @return array
   *   Set image dimensions.
   */
  public function getDimensions() {
    return $this->dimensions;
  }

  /**
   * Image dimensions.
   *
   * @param array $dimensions
   *   Dimentions.
   */
  public function setDimensions(array $dimensions) {
    $this->dimensions = $dimensions;
  }

  /**
   * Image width.
   *
   * @return int
   *   Image width.
   */
  public function getWidth() {
    return $this->dimensions[self::WIDTH];
  }

  /**
   * Image height.
   *
   * @return int
   *   Image height.
   */
  public function getHeight() {
    return $this->dimensions[self::HEIGHT];
  }

  /**
   * Get the path.
   *
   * @return string
   *   Path.
   */
  public function getPath() {
    return $this->path;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImageDTO::$alt protected property Image alt text.
ImageDTO::$caption protected property Caption.
ImageDTO::$dimensions protected property Image dimensions.
ImageDTO::$entity protected property Entity.
ImageDTO::$item protected property Item.
ImageDTO::$path protected property The path to the image that will show in Photoswipe.
ImageDTO::$settings protected property Preprocesed image settings.
ImageDTO::$title protected property Image title.
ImageDTO::$uri protected property Image url.
ImageDTO::$variables protected property Variables.
ImageDTO::createFromVariables public static function Create from variables.
ImageDTO::getAlt public function Get image alt.
ImageDTO::getCaption public function Get Caption.
ImageDTO::getDimensions public function Get image dimensions.
ImageDTO::getEntity public function Get entity.
ImageDTO::getHeight public function Image height.
ImageDTO::getItem public function Get item.
ImageDTO::getPath public function Get the path.
ImageDTO::getSettings public function Get item settings.
ImageDTO::getTitle public function Get image title.
ImageDTO::getUri public function Get image uri.
ImageDTO::getWidth public function Image width.
ImageDTO::HEIGHT constant Image dimensions.
ImageDTO::setDimensions public function Image dimensions.
ImageDTO::WIDTH constant
ImageDTO::__construct public function Construct new ImageDTO object.