Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions Model/AdminLogo.php

This file was deleted.

12 changes: 1 addition & 11 deletions Model/Config/Backend/AdminLoginLogo.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © element119. All rights reserved.
* See LICENCE.txt for licence details.
* See LICENSE for license details.
*/
declare(strict_types=1);

Expand All @@ -11,16 +11,6 @@

class AdminLoginLogo extends Image
{
public const UPLOAD_DIR = 'admin/logo/custom/login';

/**
* @inheritDoc
*/
protected function _getUploadDir(): string
{
return $this->_mediaDirectory->getAbsolutePath(self::UPLOAD_DIR);
}

/**
* @inheritDoc
*/
Expand Down
12 changes: 1 addition & 11 deletions Model/Config/Backend/AdminMenuLogo.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © element119. All rights reserved.
* See LICENCE.txt for licence details.
* See LICENSE for license details.
*/
declare(strict_types=1);

Expand All @@ -11,16 +11,6 @@

class AdminMenuLogo extends Image
{
public const UPLOAD_DIR = 'admin/logo/custom/menu';

/**
* @inheritDoc
*/
protected function _getUploadDir(): string
{
return $this->_mediaDirectory->getAbsolutePath(self::UPLOAD_DIR);
}

/**
* @inheritDoc
*/
Expand Down
132 changes: 132 additions & 0 deletions Plugin/Backend/Block/Page/HeaderPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Copyright © element119. All rights reserved.
* See LICENSE for license details.
*/
declare(strict_types=1);

namespace Element119\CustomAdminLogo\Plugin\Backend\Block\Page;

use Magento\Backend\Block\Page\Header;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;

/**
* Plugin that injects custom admin logo URLs into the Header block.
*
* Replaces a template override approach: layout XML arguments drive which
* config path and upload directory apply per layout handle (login vs. menu),
* and the core template renders the custom logo without modification.
*/
class HeaderPlugin
{
/** @var ScopeConfigInterface */
private ScopeConfigInterface $scopeConfig;

/** @var StoreManagerInterface */
private StoreManagerInterface $storeManager;

/** @var LoggerInterface */
private LoggerInterface $logger;

/**
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param LoggerInterface $logger
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
LoggerInterface $logger
) {
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->logger = $logger;
}

/**
* Set custom logo URL on the block before rendering, if configured.
*
* Reads the config path and upload directory from layout XML arguments
* to determine which custom logo (login or menu) applies to this context.
*
* @param Header $subject
* @return void
*/
public function beforeToHtml(Header $subject): void
{
if ($subject->getData('show_part') !== 'logo') {
return;
}

$configPath = $subject->getData('custom_logo_config_path');
$uploadDir = $subject->getData('custom_logo_upload_dir');

if (!$configPath || !$uploadDir) {
return;
}

$filename = $this->scopeConfig->getValue($configPath);

if (!is_string($filename) || $filename === '') {
return;
}

$filename = basename($filename);

if ($filename === '') {
return;
}

try {
$mediaUrl = $this->storeManager->getStore()
->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
} catch (NoSuchEntityException $e) {
$this->logger->warning(
'Element119_CustomAdminLogo: Unable to resolve store for media URL.',
['exception' => $e]
);
return;
} catch (\Throwable $e) {
$this->logger->error(
'Element119_CustomAdminLogo: Unexpected error resolving media URL.',
['exception' => $e]
);
return;
}

$subject->setLogoImageSrc($mediaUrl . $uploadDir . '/' . $filename);
}

/**
* Pass through full URLs without asset repository resolution.
*
* When a custom logo is configured, logo_image_src is set to a full media
* URL. The core template passes this to getViewFileUrl(), which would
* attempt static file resolution. This plugin short-circuits that for
* absolute URLs, returning the fileId as-is.
*
* @param Header $subject
* @param string $result
* @param string $fileId
* @return string
*/
public function afterGetViewFileUrl(
Header $subject,
string $result,
string $fileId = ''
): string {
if ($subject->getData('show_part') !== 'logo') {
return $result;
}

if (str_starts_with($fileId, 'http://') || str_starts_with($fileId, 'https://')) {
return $fileId;
}

return $result;
}
}
45 changes: 0 additions & 45 deletions Scope/Config.php

This file was deleted.

Loading