I am working on a module that is meant to extend MagentoCatalogModelCategory
in order to get the thumbnail URL. I based the code on getImageUrl
in the base class.
I have my main Model file CategoryThumbnail.php
:
< ?php
namespace ModuleCategoryThumbnailModel;
use MagentoAuthorizationModelUserContextInterface;
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogApiDataCategoryInterface;
use MagentoCatalogUrlRewriteModelCategoryUrlRewriteGenerator;
use MagentoFrameworkApiAttributeValueFactory;
use MagentoFrameworkAppObjectManager;
use MagentoFrameworkAuthorizationInterface;
use MagentoFrameworkConvertConvertArray;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkProfiler;
use MagentoUrlRewriteModelUrlFinderInterface;
use MagentoUrlRewriteServiceV1DataUrlRewrite;
use MagentoCatalogModelCategory;
class CategoryThumbnail extends MagentoCatalogModelCategory {
/**
* Returns image url
*
* @param string $attributeCode
* @return bool|string
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function getThumbnail($attributeCode = 'thumbnail')
{
$url = false;
$image = $this->getData($attributeCode);
if ($image) {
if (is_string($image)) {
$store = $this->_storeManager->getStore();
$isRelativeUrl = substr($image, 0, 1) === '/';
$mediaBaseUrl = $store->getBaseUrl(
MagentoFrameworkUrlInterface::URL_TYPE_MEDIA
);
if ($isRelativeUrl) {
$url = $image;
} else {
$url = $mediaBaseUrl
. ltrim(MagentoCatalogModelCategoryFileInfo::ENTITY_MEDIA_PATH, '/')
. '/'
. $image;
}
} else {
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while getting the image url.')
);
}
}
return $url;
}
}
And I have this template extension in the Block folder Thumbnail.php
:
< ?php
namespace ModuleCategoryThumbnailBlock;
class Thumbnail extends MagentoFrameworkViewElementTemplate
{
protected $thumbnail;
/**
* @param MagentoFrameworkViewElementTemplateContext $context
* @param ModuleCategoryThumbnailModelCategoryThumbnail $thumbnail
* array $data
*/
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
ModuleCategoryThumbnailModelCategoryThumbnail $thumbnail,
array $data = []
) {
$this->thumbnail = $thumbnail;
parent::__construct($context, $data);
}
public function getThumbnailUrl() {
return $this->thumbnail->getThumbnail();
}
}
Contents of di.xml
and module.xml
below:
< ?xml version="1.0" ?>
< ?xml version="1.0"?>
When I attempt to load the thumbnail URL with $block->getThumbnailUrl
, I don’t get anything. I tried to echo it to the page by itself and using strval
but still empty.
I cleaned and flushed the caches and ran rm -rf generated/
to ensure clean caches and no leftover code. Just for good measure, I also ran setup:di:compile
and didn’t get any errors. I’ve clearly done something wrong but I’m not sure what since I don’t get any errors at any point. Just doesn’t work.