/** * Builds the array of Menu items for this variant menu */ protected function buildItems() { $menuItems = []; $targetDimensionsToMatch = []; $allDimensionPresets = $this->configurationContentDimensionPresetSource->getAllPresets(); $includeAllPresets = $this->getIncludeAllPresets(); $pinnedDimensionValues = $this->getPresets(); $pinnedDimensionName = $this->getDimension(); if ($pinnedDimensionName !== null) { $targetDimensionsToMatch = $this->currentNode->getContext()->getTargetDimensions(); unset($targetDimensionsToMatch[$pinnedDimensionName]); } foreach ($this->contentDimensionCombinator->getAllAllowedCombinations() as $allowedCombination) { $targetDimensions = $this->calculateTargetDimensionsForCombination($allowedCombination); if ($pinnedDimensionName !== null && is_array($pinnedDimensionValues)) { if (!in_array($targetDimensions[$pinnedDimensionName], $pinnedDimensionValues)) { continue; } } // skip variants not matching the current target dimensions (except the dimension this menu covers) if ($targetDimensionsToMatch !== []) { foreach ($targetDimensionsToMatch as $dimensionName => $dimensionValue) { if ($targetDimensions[$dimensionName] !== $dimensionValue) { continue 2; } } } $nodeInDimensions = $this->getNodeInDimensions($allowedCombination, $targetDimensions); // no match, so we look further... if ($nodeInDimensions === null && $includeAllPresets) { $nodeInDimensions = $this->findAcceptableNode($allowedCombination, $allDimensionPresets); } if ($nodeInDimensions !== null && $this->isNodeHidden($nodeInDimensions)) { $nodeInDimensions = null; } // determine metadata for target dimensions of node array_walk($targetDimensions, function (&$dimensionValue, $dimensionName, $allDimensionPresets) use($pinnedDimensionName) { $dimensionValue = ['value' => $dimensionValue, 'label' => $allDimensionPresets[$dimensionName]['presets'][$dimensionValue]['label'], 'isPinnedDimension' => $pinnedDimensionName === null || $dimensionName == $pinnedDimensionName ? true : false]; }, $allDimensionPresets); if ($pinnedDimensionName === null) { $itemLabel = $nodeInDimensions->getLabel(); } else { $itemLabel = $targetDimensions[$pinnedDimensionName]['label']; } $menuItems[] = ['node' => $nodeInDimensions, 'state' => $this->calculateItemState($nodeInDimensions), 'label' => $itemLabel, 'dimensions' => $allowedCombination, 'targetDimensions' => $targetDimensions]; } // sort/limit according to configured "presets" if needed if ($pinnedDimensionName !== null && is_array($pinnedDimensionValues)) { $sortedMenuItems = []; foreach ($pinnedDimensionValues as $pinnedDimensionValue) { foreach ($menuItems as $menuItemKey => $menuItem) { if ($menuItem['targetDimensions'][$pinnedDimensionName]['value'] === $pinnedDimensionValue) { $sortedMenuItems[$menuItemKey] = $menuItem; } } } return $sortedMenuItems; } return $menuItems; }
/** * Return the presets in the correct order, taking possibly-overridden presets into account * * @return array * @throws TypoScriptException */ protected function getPresetsInCorrectOrder() { $dimension = $this->getDimension(); $allDimensions = $this->configurationContentDimensionPresetSource->getAllPresets(); if (!isset($allDimensions[$dimension])) { throw new TypoScriptException(sprintf('Dimension "%s" was referenced, but not configured.', $dimension), 1415880445); } $allPresetsOfChosenDimension = $allDimensions[$dimension]['presets']; $presetNames = $this->getPresets(); if ($presetNames === NULL) { $presetNames = array_keys($allPresetsOfChosenDimension); } elseif (!is_array($presetNames)) { throw new TypoScriptException('The configured preset in TypoScript was no array.', 1415888652); } $resultingPresets = array(); foreach ($presetNames as $presetName) { if (!isset($allPresetsOfChosenDimension[$presetName])) { throw new TypoScriptException(sprintf('The preset name "%s" does not exist in the chosen dimension. Valid values are: %s', $presetName, implode(', ', array_keys($allPresetsOfChosenDimension))), 1415889492); } $resultingPresets[$presetName] = $allPresetsOfChosenDimension[$presetName]; } return $resultingPresets; }