Skip to content

Translations for the menu #360

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
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
44 changes: 44 additions & 0 deletions Api/Data/NodeTranslationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace Snowdog\Menu\Api\Data;

interface NodeTranslationInterface
{
public const NODE_ID = 'node_id';
public const STORE_ID = 'store_id';
public const TITLE = 'title';

/**
* @return int
*/
public function getNodeId(): int;

/**
* @param int $nodeId
* @return NodeTranslationInterface
*/
public function setNodeId(int $nodeId): NodeTranslationInterface;

/**
* @return int
*/
public function getStoreId(): int;

/**
* @param int $storeId
* @return NodeTranslationInterface
*/
public function setStoreId(int $storeId): NodeTranslationInterface;

/**
* @return string|null
*/
public function getTitle(): ?string;

/**
* @param string $title
* @return NodeTranslationInterface
*/
public function setTitle(string $title): NodeTranslationInterface;
}
93 changes: 93 additions & 0 deletions Api/NodeTranslationRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);

namespace Snowdog\Menu\Api;

use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Snowdog\Menu\Api\Data\NodeTranslationInterface;

interface NodeTranslationRepositoryInterface
{
/**
* Save node translation
*
* @param NodeTranslationInterface $translation
* @return NodeTranslationInterface
* @throws CouldNotSaveException
*/
public function save(NodeTranslationInterface $translation): NodeTranslationInterface;

/**
* Get node translation by ID
*
* @param int $translationId
* @return NodeTranslationInterface
* @throws NoSuchEntityException
*/
public function getById(int $translationId): NodeTranslationInterface;

/**
* Get node translation by node ID and store ID
*
* @param int $nodeId
* @param int $storeId
* @return NodeTranslationInterface
* @throws NoSuchEntityException
*/
public function getByNodeAndStore(int $nodeId, int $storeId): NodeTranslationInterface;

/**
* Get all translations for multiple nodes in a specific store
*
* @param array $nodeIds
* @param int $storeId
* @return NodeTranslationInterface[]
*/
public function getByNodeIds(array $nodeIds, int $storeId): array;

/**
* Get all translations for a node
*
* @param int $nodeId
* @return NodeTranslationInterface[]
*/
public function getByNodeId(int $nodeId): array;

/**
* Delete node translation
*
* @param NodeTranslationInterface $translation
* @return bool
* @throws CouldNotDeleteException
*/
public function delete(NodeTranslationInterface $translation): bool;

/**
* Delete node translation by ID
*
* @param int $translationId
* @return bool
* @throws CouldNotDeleteException
* @throws NoSuchEntityException
*/
public function deleteById(int $translationId): bool;

/**
* Delete all translations for a node
*
* @param int $nodeId
* @return bool
* @throws CouldNotDeleteException
*/
public function deleteByNodeId(int $nodeId): bool;

/**
* @param SearchCriteriaInterface $searchCriteria
* @return SearchResultsInterface
*/
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface;
}
90 changes: 89 additions & 1 deletion Block/Adminhtml/Edit/Tab/Nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Snowdog\Menu\Model\Menu\Node\Image\File as ImageFile;
use Snowdog\Menu\Model\NodeTypeProvider;
use Snowdog\Menu\Model\VueProvider;
use Magento\Store\Model\System\Store;
use Snowdog\Menu\Api\NodeTranslationRepositoryInterface;

/**
* @api
Expand Down Expand Up @@ -51,6 +53,16 @@ class Nodes extends Template implements TabInterface
*/
private $customerGroupsProvider;

/**
* @var Store
*/
private $systemStore;

/**
* @var NodeTranslationRepositoryInterface
*/
private $nodeTranslationRepository;

public function __construct(
Template\Context $context,
NodeRepositoryInterface $nodeRepository,
Expand All @@ -59,6 +71,8 @@ public function __construct(
Registry $registry,
VueProvider $vueProvider,
CustomerGroupsProvider $customerGroupsProvider,
Store $systemStore,
NodeTranslationRepositoryInterface $nodeTranslationRepository,
array $data = []
) {
parent::__construct($context, $data);
Expand All @@ -68,6 +82,8 @@ public function __construct(
$this->imageFile = $imageFile;
$this->vueProvider = $vueProvider;
$this->customerGroupsProvider = $customerGroupsProvider;
$this->systemStore = $systemStore;
$this->nodeTranslationRepository = $nodeTranslationRepository;
}

public function renderNodes()
Expand Down Expand Up @@ -175,7 +191,43 @@ private function renderNodeList($level, $parent, $data)
}
$nodes = $data[$level][$parent];
$menu = [];

// Create store view lookup array
$storeViewLabels = [];
$websites = $this->systemStore->getWebsiteCollection();
$websiteNames = [];

// Create website name lookup array
foreach ($websites as $website) {
$websiteNames[$website->getId()] = $website->getName();
}

foreach ($this->systemStore->getStoreCollection() as $store) {
if ($store->isActive()) {
$websiteName = isset($websiteNames[$store->getWebsiteId()])
? $websiteNames[$store->getWebsiteId()]
: '';
$storeViewLabels[$store->getId()] = [
'value' => $store->getId(),
'label' => sprintf('%s -> %s', $websiteName, $store->getName())
];
}
}

Comment on lines +194 to +216
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like duplicate piece of code. I think you can use the function getStoreViews().

foreach ($nodes as $node) {
$translations = $this->nodeTranslationRepository->getByNodeId($node->getId());
$translationData = [];
foreach ($translations as $translation) {
$storeId = $translation->getStoreId();
if (isset($storeViewLabels[$storeId])) {
$translationData[] = [
'store_id' => $storeViewLabels[$storeId]['value'],
'value' => $translation->getTitle(),
'label' => $storeViewLabels[$storeId]['label']
];
}
}

$menu[] = [
'is_active' => $node->getIsActive(),
'is_stored' => true,
Expand All @@ -194,7 +246,8 @@ private function renderNodeList($level, $parent, $data)
'image_height' => $node->getImageHeight(),
'columns' => $this->renderNodeList($level + 1, $node->getId(), $data) ?: [],
'selected_item_id' => $node->getSelectedItemId(),
'customer_groups' => $node->getCustomerGroups()
'customer_groups' => $node->getCustomerGroups(),
'translations' => $translationData
];
}
return $menu;
Expand Down Expand Up @@ -222,4 +275,39 @@ public function getCustomerGroups()
{
return $this->customerGroupsProvider->getAll();
}

/**
* Get store views for translations
*
* @return array
*/
public function getStoreViews()
{
$storeViews = [];
$stores = $this->systemStore->getStoreCollection();
$websites = $this->systemStore->getWebsiteCollection();
$websiteNames = [];

// Create website name lookup array
foreach ($websites as $website) {
$websiteNames[$website->getId()] = $website->getName();
}

foreach ($stores as $store) {
if (!$store->isActive()) {
continue;
}

$websiteName = isset($websiteNames[$store->getWebsiteId()])
? $websiteNames[$store->getWebsiteId()]
: '';

$storeViews[] = [
'value' => $store->getId(),
'label' => sprintf('%s -> %s', $websiteName, $store->getName())
];
}

return $storeViews;
}
}
53 changes: 52 additions & 1 deletion Block/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Snowdog\Menu\Model\NodeTypeProvider;
use Snowdog\Menu\Model\TemplateResolver;
use Magento\Store\Model\Store;
use Snowdog\Menu\Api\NodeTranslationRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand All @@ -40,9 +42,19 @@ class Menu extends Template implements DataObject\IdentityInterface
*/
private $nodeTypeProvider;

private $nodes;
/**
* @var NodeTranslationRepositoryInterface
*/
private $nodeTranslationRepository;

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

private $nodes;
private $menu = null;
private $nodeTranslations = [];

/**
* @var EventManager
Expand Down Expand Up @@ -97,6 +109,8 @@ public function __construct(
ImageFile $imageFile,
Escaper $escaper,
Context $httpContext,
NodeTranslationRepositoryInterface $nodeTranslationRepository,
StoreManagerInterface $storeManager,
array $data = []
) {
parent::__construct($context, $data);
Expand All @@ -110,6 +124,8 @@ public function __construct(
$this->setTemplate($this->getMenuTemplate($this->_template));
$this->submenuTemplate = $this->getSubmenuTemplate();
$this->httpContext = $httpContext;
$this->nodeTranslationRepository = $nodeTranslationRepository;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -447,6 +463,20 @@ private function fetchData()
$customerGroupEnabled = $this->_scopeConfig->getValue(self::XML_SNOWMENU_GENERAL_CUSTOMER_GROUPS);
$result = [];
$types = [];
$nodeIds = [];

foreach($nodes as $node) {
$nodeIds[] = $node->getId();
}

if (!empty($nodeIds)) {
$storeId = (int)$this->storeManager->getStore()->getId();
$collection = $this->nodeTranslationRepository->getByNodeIds($nodeIds, $storeId);
foreach ($collection as $translation) {
$this->nodeTranslations[$translation->getNodeId()] = $translation;
}
}

foreach ($nodes as $node) {
if (!$node->getIsActive()) {
continue;
Expand All @@ -455,6 +485,8 @@ private function fetchData()
continue;
}

$node->setTitle($this->getNodeTitle($node));

$level = $node->getLevel();
$parent = $node->getParentId() ?: 0;
if (!isset($result[$level])) {
Expand All @@ -470,6 +502,7 @@ private function fetchData()
}
$types[$type][] = $node;
}

$this->nodes = $result;

foreach ($types as $type => $nodes) {
Expand Down Expand Up @@ -513,4 +546,22 @@ public function getCustomerGroupId()
{
return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_GROUP);
}

/**
* Get translated node title based on current store view
*
* @param NodeInterface $node
* @return string
*/
private function getNodeTitle(NodeInterface $node): string
{
$nodeId = $node->getId();
if (isset($this->nodeTranslations[$nodeId])) {
$title = $this->nodeTranslations[$nodeId]->getTitle();
if ($title) {
return $title;
}
}
return $node->getTitle();
}
}
Loading