Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#102271 Adding image size (width and height) to nodes #295

Merged
merged 11 commits into from
Apr 4, 2024
28 changes: 26 additions & 2 deletions Api/Data/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface NodeInterface
const TARGET = 'target';
const IMAGE = 'image';
const IMAGE_ALT_TEXT = 'image_alt_text';
const IMAGE_WIDTH = 'image_width';
const IMAGE_HEIGHT = 'image_heigth';
const CREATION_TIME = 'creation_time';
const UPDATE_TIME = 'update_time';
const IS_ACTIVE = 'is_active';
Expand Down Expand Up @@ -192,18 +194,40 @@ public function setImage($image);
/**
* Get image alt text
*
* @return string
* @return string|null
*/
public function getImageAltText();

/**
* Set image alt text
*
* @param string $altText
* @param string|null $altText
* @return $this
*/
public function setImageAltText($altText);

/**
* @return int|null
*/
public function getImageWidth();

/**
* @param int|null $width
* @return $this
*/
public function setImageWidth($width);

/**
* @return int|null
*/
public function getImageHeight();

/**
* @param int|null $height
* @return $this
*/
public function setImageHeight($height);

/**
* Get creation time
*
Expand Down
2 changes: 2 additions & 0 deletions Block/Adminhtml/Edit/Tab/Nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ private function renderNodeList($level, $parent, $data)
'image' => $node->getImage(),
'image_url' => $node->getImage() ? $this->imageFile->getUrl($node->getImage()) : null,
'image_alt_text' => $node->getImageAltText(),
'image_width' => $node->getImageWidth(),
'image_height' => $node->getImageHeight(),
'columns' => $this->renderNodeList($level + 1, $node->getId(), $data) ?: [],
'selected_item_id' => $node->getSelectedItemId()
];
Expand Down
10 changes: 7 additions & 3 deletions Block/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\View\Element\Template;
use Magento\Framework\Event\Manager as EventManager;
use Magento\Framework\Escaper;
use Snowdog\Menu\Api\Data\NodeInterface;
use Snowdog\Menu\Api\MenuRepositoryInterface;
use Snowdog\Menu\Api\NodeRepositoryInterface;
use Snowdog\Menu\Model\Menu\Node\Image\File as ImageFile;
Expand Down Expand Up @@ -268,7 +269,7 @@ public function isViewAllLinkAllowed($nodeType)
}

/**
* @param NodeRepositoryInterface $node
* @param NodeInterface $node
* @return string
*/
public function renderViewAllLink($node)
Expand All @@ -279,7 +280,7 @@ public function renderViewAllLink($node)
}

/**
* @param NodeRepositoryInterface $node
* @param NodeInterface $node
* @return string
*/
public function renderMenuNode($node)
Expand Down Expand Up @@ -381,7 +382,7 @@ public function getMenuCssClass($defaultClass = '')
}

/**
* @param NodeRepositoryInterface $node
* @param NodeInterface $node
* @return Template
*/
private function getMenuNodeBlock($node)
Expand All @@ -404,6 +405,9 @@ private function getMenuNodeBlock($node)
->setImage($node->getImage())
->setImageUrl($node->getImage() ? $this->imageFile->getUrl($node->getImage()) : null)
->setImageAltText($node->getImageAltText())
->setImageWidth($node->getImageWidth())
->setImageHeight($node->getImageHeight())
->setImageAltText($node->getImageAltText())
->setCustomTemplate($node->getNodeTemplate())
->setAdditionalData($node->getAdditionalData())
->setSelectedItemId($node->getSelectedItemId());
Expand Down
2 changes: 2 additions & 0 deletions Model/GraphQl/Resolver/DataProvider/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ private function convertData(NodeInterface $node): array
NodeInterface::TARGET => (bool) $node->getTarget(),
NodeInterface::IMAGE => $node->getImage(),
NodeInterface::IMAGE_ALT_TEXT => $node->getImageAltText(),
NodeInterface::IMAGE_WIDTH => $node->getImageWidth(),
NodeInterface::IMAGE_HEIGHT => $node->getImageHeight(),
self::TEMPLATE_FIELD => $node->getNodeTemplate(),
self::SUBMENU_TEMPLATE_FIELD => $node->getSubmenuTemplate(),
NodeInterface::CREATION_TIME => $node->getCreationTime(),
Expand Down
32 changes: 32 additions & 0 deletions Model/Menu/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,38 @@ public function setImageAltText($altText)
return $this->setData(NodeInterface::IMAGE_ALT_TEXT, $altText);
}

/**
* @inheritdoc
*/
public function getImageWidth()
{
return $this->_getData(NodeInterface::IMAGE_WIDTH);
}

/**
* @inheritdoc
*/
public function setImageWidth($width)
{
return $this->setData(NodeInterface::IMAGE_WIDTH, $width);
}

/**
* @inheritdoc
*/
public function getImageHeight()
{
return $this->_getData(NodeInterface::IMAGE_HEIGHT);
}

/**
* @inheritdoc
*/
public function setImageHeight($height)
{
return $this->setData(NodeInterface::IMAGE_HEIGHT, $height);
}

/**
* @inheritdoc
*/
Expand Down
23 changes: 22 additions & 1 deletion Model/Menu/Node/Image/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\MediaStorage\Model\File\UploaderFactory;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Snowdog\Menu\Api\Data\NodeInterface;

class File
{
Expand Down Expand Up @@ -72,7 +73,11 @@ public function upload(): array

$result = $uploader->save($this->getAbsolutePath());

return ['file' => $result['file'], 'url' => $this->getUrl($result['file'])];
return [
'file' => $result['file'],
'url' => $this->getUrl($result['file']),
'size' => $this->getReadableImageSizes($this->getImageSize($result['file']))
];
}

public function getUrl(string $file): string
Expand Down Expand Up @@ -113,9 +118,25 @@ public function clone(string $file): string
return $fileClonePath;
}

public function getImageSize(string $file): array
{
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$fileFullPath = $mediaDirectory->getAbsolutePath(self::PATH . $file);

return getimagesize($fileFullPath);
}

private function getAbsolutePath(): string
{
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
return $mediaDirectory->getAbsolutePath(self::PATH);
}

private function getReadableImageSizes(array $imageSize): array
{
return [
NodeInterface::IMAGE_WIDTH => $imageSize[0],
NodeInterface::IMAGE_HEIGHT => $imageSize[1]
];
}
}
5 changes: 5 additions & 0 deletions Model/Menu/Node/Image/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function updateNodeImage(int $nodeId, ?string $image): void

try {
$node->setImage($image);
if (!$image) {
$node
->setImageWidth(null)
->setImageHeight(null);
}
$this->nodeRepository->save($node);
} catch (CouldNotSaveException $exception) {
// Normally, this error should never happen.
Expand Down
2 changes: 2 additions & 0 deletions Model/MenuManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ private function getCategoriesNodeList($level, $parent, array $data): array
'node_template' => null,
'image' => null,
'image_alt_text' => null,
'image_width' => null,
'image_height' => null,
'submenu_template' => null,
'columns' => $this->getCategoriesNodeList($level + 1, $nodeId, $data) ?: []
];
Expand Down
38 changes: 35 additions & 3 deletions Service/Menu/SaveRequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,46 @@ private function processNodeObject(
$nodeObject->setLevel((string) $level);
$nodeObject->setPosition((string) $position);

$this->processImageParameters($nodeData, $nodeObject);

$nodeObject->setSelectedItemId($nodeData['selected_item_id'] ?? null);
}

private function processImageParameters(array $nodeData, NodeInterface &$nodeObject): void
{
$nodeObject->setImageAltText($nodeData[NodeInterface::IMAGE_ALT_TEXT] ?? null);

if ($nodeObject->getImage() && empty($nodeData['image'])) {
$this->nodeImageFile->delete($nodeObject->getImage());
}

$nodeObject->setImage($nodeData['image'] ?? null);
$nodeObject->setImageAltText($nodeData['image_alt_text'] ?? null);
if (empty($nodeData[NodeInterface::IMAGE])) {
$nodeObject->setImageWidth(null);
$nodeObject->setImageHeight(null);
$nodeObject->setImage(null);
return;
}

$nodeObject->setSelectedItemId($nodeData['selected_item_id'] ?? null);
if (empty($nodeData[NodeInterface::IMAGE_WIDTH])
|| empty($nodeData[NodeInterface::IMAGE_HEIGHT])
) {
$imageSize = $this->nodeImageFile->getImageSize($nodeData[NodeInterface::IMAGE]);

if (!empty($imageSize)) {
$nodeObject
->setImageWidth(
!empty($nodeData[NodeInterface::IMAGE_WIDTH])
? $nodeData[NodeInterface::IMAGE_WIDTH]
: $imageSize[0]
);
$nodeObject
->setImageHeight(
!empty($nodeData[NodeInterface::IMAGE_HEIGHT])
? $nodeData[NodeInterface::IMAGE_HEIGHT]
: $imageSize[1]
);
}
}
}

/**
Expand Down
38 changes: 38 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class UpgradeSchema implements UpgradeSchemaInterface
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
Expand Down Expand Up @@ -59,6 +60,10 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
$this->addNodeSelectedItemId($setup);
}

if (version_compare($context->getVersion(), '0.2.7', '<')) {
$this->addNodeImageSizeFields($setup);
}

$setup->endSetup();
}

Expand Down Expand Up @@ -298,4 +303,37 @@ private function addNodeSelectedItemId(SchemaSetupInterface $setup)

return $this;
}

/**
* @return $this
*/
private function addNodeImageSizeFields(SchemaSetupInterface $setup)
{
$connection = $setup->getConnection();
$table = $setup->getTable('snowmenu_node');

$connection->addColumn(
$table,
NodeInterface::IMAGE_WIDTH,
[
'type' => Table::TYPE_INTEGER,
'nullable' => true,
'after' => NodeInterface::IMAGE_ALT_TEXT,
'comment' => 'Image Width'
]
);

$connection->addColumn(
$table,
NodeInterface::IMAGE_HEIGHT,
[
'type' => Table::TYPE_INTEGER,
'nullable' => true,
'after' => NodeInterface::IMAGE_WIDTH,
'comment' => 'Image Height'
]
);

return $this;
}
}
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Snowdog_Menu" setup_version="0.2.6">
<module name="Snowdog_Menu" setup_version="0.2.7">
<sequence>
<module name="Magento_GraphQl"/>
</sequence>
Expand Down
2 changes: 2 additions & 0 deletions view/adminhtml/templates/menu/nodes.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ $vueComponents = $block->getVueComponents();
"product" : "<?= __('Product') ?>",
"productId" : "<?= __('Product ID') ?>",
"imageAltText" : "<?= __('Image Alt text') ?>",
"imageWidth" : "<?= __('Image Width') ?>",
"imageHeight" : "<?= __('Image Height') ?>",
"selectedItemId" : "<?= __('Selected Item Id') ?>"
}
}
Expand Down
2 changes: 2 additions & 0 deletions view/adminhtml/web/vue/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@
content: null,
image: null,
image_alt_text: '',
image_width: null,
image_height: null,
node_template: null,
submenu_template: null,
columns: [],
Expand Down
Loading
Loading