Skip to content
Snippets Groups Projects
Commit 4da834d0 authored by Shibin Das's avatar Shibin Das
Browse files

Issue #3484013: Image tag is not outputting height and width

parent cb1e424b
No related branches found
No related tags found
No related merge requests found
......@@ -215,10 +215,10 @@ class ImgElement extends ElementBase {
/**
* Getter for Width.
*
* @return int
* @return int|null
* return Width.
*/
public function getWidth(): int {
public function getWidth(): ?int {
return $this->width;
}
......@@ -239,10 +239,10 @@ class ImgElement extends ElementBase {
/**
* Getter for Height.
*
* @return int
* @return int|null
* return Height.
*/
public function getHeight(): int {
public function getHeight(): ?int {
return $this->height;
}
......
......@@ -44,6 +44,20 @@ class SourceElement extends ElementBase {
*/
protected array $srcset = [];
/**
* The 'width' attribute.
*
* @var int|null
*/
protected ?int $width = NULL;
/**
* The 'height' attribute.
*
* @var int|null
*/
protected ?int $height = NULL;
/**
* Generates Drupal Render array for printing the Html Element.
*/
......@@ -63,6 +77,12 @@ class SourceElement extends ElementBase {
*/
public function getAttribute(): Attribute {
$attribute = parent::getAttribute();
if (!empty($this->width)) {
$attribute->setAttribute('width', strval($this->width));
}
if (!empty($this->height)) {
$attribute->setAttribute('height', strval($this->height));
}
if (!empty($this->src)) {
$attribute->setAttribute('src', $this->src);
}
......@@ -216,4 +236,53 @@ class SourceElement extends ElementBase {
return 'source';
}
/**
* Getter for Width.
*
* @return int|null
* return Width.
*/
public function getWidth(): ?int {
return $this->width;
}
/**
* Setter for Width.
*
* @param int $width
* Width value.
*
* @return SourceElement
* Self Reference.
*/
public function setWidth(int $width): SourceElement {
$this->width = $width;
return $this;
}
/**
* Getter for Height.
*
* @return int|null
* return Height.
*/
public function getHeight(): ?int {
return $this->height;
}
/**
* Setter for Height.
*
* @param int $height
* Height value.
*
* @return SourceElement
* Self Reference.
*/
public function setHeight(int $height): SourceElement {
$this->height = $height;
return $this;
}
}
......@@ -5,6 +5,7 @@ namespace Drupal\rift;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Template\Attribute;
use Drupal\media\MediaInterface;
use Drupal\rift\DTO\PictureConfig;
use Drupal\rift\DTO\SourceTransformConfig;
......@@ -255,7 +256,9 @@ class RiftPicture {
$img_element
->setSrc($url)
->setAlt($image_source->getAlt())
->setTitle($image_source->getTitle());
->setTitle($image_source->getTitle())
->setHeight($image_source->getHeight())
->setWidth($image_source->getWidth());
$picture_element->setImg($img_element);
$picture_element->setAttribute($this->pictureConfig->attribute);
......@@ -320,7 +323,7 @@ class RiftPicture {
$url = $this->generateImageUrlFromStyles($source['styles'], $uri);
$srcset_item = new SrcSetItem();
$srcset_item->setImg($url);
// Red Flag, why is multiplier being injected into width?
// Unlike traditional width, the width in srcset item is the multiplier.
$srcset_item->setWidth($source['multiplier']);
$srcset_items[] = $srcset_item;
// We set the type to the last one in the source.
......@@ -328,6 +331,19 @@ class RiftPicture {
if ($source['type']) {
$source_element->setType($source['type']);
}
if (!$source_element->getHeight()) {
if ($width = $source['source_transform_config']->width) {
[$arw, $arh] = explode('x', $source['source_transform_config']->aspectRatioStyle);
if ($arh > 0) {
$aspect_ratio = intval($arw) / intval($arh);
$height = intval($width / $aspect_ratio);
if ($height) {
$source_element->setWidth($width);
$source_element->setHeight($height);
}
}
}
}
}
$sizes_item = new SizesItem();
$sizes_item->setMediaQuery($config['media']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment