Skip to content

Commit

Permalink
fixed translation bug when menu should be returned as array
Browse files Browse the repository at this point in the history
  • Loading branch information
yominuma committed Dec 5, 2019
1 parent 1321d11 commit 2acb037
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Services/MenuService.php
Expand Up @@ -5,6 +5,7 @@
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
Expand Down Expand Up @@ -102,12 +103,14 @@ protected function buildMenuStructure($menuItems, $menuConfiguration, $level) {

protected function buildMenuStructureArray($menuItems, $menuConfiguration, $level) {
$menuStructure = [];
$languages = $this->configResolver->getParameter( 'languages' );
foreach ($menuItems->searchHits as $menuItem) {
$content = $this->contentService->loadContentByContentInfo($menuItem->valueObject->contentInfo);
$route = $this->routeRefGenerator->generate($menuItem->valueObject);
$languageCode = $this->getEligablelanguageCode($content, $languages);

$menuStructure[] = [
'name'=> $content->getName(),
'name'=> $content->getName($languageCode),
'url' => $this->router->generate($route, []),
'locationId' => $menuItem->valueObject->id,
'active' => $this->currentLocationId == $menuItem->valueObject->id ? true : false,
Expand Down Expand Up @@ -156,4 +159,20 @@ protected function fetchChildren(

return $this->searchService->findLocations($query);
}

/**
* @param Content $content
* @param array $languages
* @return string
*/
private function getEligablelanguageCode(Content $content, array $languages) :string
{
foreach ($languages as $language) {
if (in_array($language, $content->versionInfo->languageCodes)) {
return $language;
}
}

return $content->contentInfo->mainLanguageCode;
}
}

0 comments on commit 2acb037

Please sign in to comment.