]> _ Git - cubist_cms-back.git/commitdiff
wip #3611 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 21 Apr 2020 18:28:01 +0000 (20:28 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 21 Apr 2020 18:28:01 +0000 (20:28 +0200)
src/app/Magic/Menu/Item.php
src/app/Magic/Menu/PageItem.php

index 443831c478a848aa269f1ba3370943d95627726b..c335eb099aacba60643e338b4118c2786058e3fd 100644 (file)
@@ -111,6 +111,11 @@ class Item
      */
     protected $_variant = '';
 
+    /**
+     * @var array
+     */
+    protected $_availableVariants = [];
+
     /**
      * @var CubistMagicPageModel
      */
@@ -439,8 +444,17 @@ class Item
 
     public function getCanonicalURL(): string
     {
+        $canonicalVariant = null;
+        if (App::hasVariant()) {
+            $availableVariants = $this->getAvailableVariants();
+
+            if (count($availableVariants) > 1 && $this->getVariant() !== $availableVariants[0]) {
+                $canonicalVariant = $availableVariants[0];
+            }
+        }
+
         $canonical = $this->getCanonical() === '' ? $this->getHref() : $this->getCanonical();
-        return $this->_getURL($canonical);
+        return $this->_getURL($canonical, null, $canonicalVariant);
     }
 
     public function getURL()
@@ -448,9 +462,16 @@ class Item
         return $this->_getURL($this->getHref());
     }
 
-    protected function _getURL($href)
+    protected function _getURL($href, $locale = null, $variant = null)
     {
-        $domain = Locale::getMainDomain($this->getLocale(), $this->getVariant());
+        if (null === $locale) {
+            $locale = $this->getLocale();
+        }
+        if (null === $variant) {
+            $variant = $this->getVariant();
+        }
+
+        $domain = Locale::getMainDomain($locale, $variant);
 
         if ($href === 'home') {
             $href = '';
@@ -698,6 +719,22 @@ class Item
         $this->_variant = $variant;
     }
 
+    /**
+     * @param array $availableVariants
+     */
+    public function setAvailableVariants(array $availableVariants): void
+    {
+        $this->_availableVariants = $availableVariants;
+    }
+
+    /**
+     * @return array
+     */
+    public function getAvailableVariants(): array
+    {
+        return $this->_availableVariants;
+    }
+
     /**
      * @return bool
      */
index 6daa4387076ed90d571b426c8094cf28bcaacd44..d0a58df26b9d41a1055ddb92c6cadba6e6afa475 100644 (file)
@@ -6,6 +6,7 @@ namespace Cubist\Backpack\app\Magic\Menu;
 
 use Cubist\Backpack\app\Magic\Models\CMSPage;
 use Cubist\Backpack\app\Magic\Models\CubistMagicPageModel;
+use Cubist\Backpack\Facades\App;
 
 class PageItem extends Item
 {
@@ -52,6 +53,7 @@ class PageItem extends Item
         $this->setSlug($data->get('slug'));
         $this->setTitle($data->get('title'));
         $this->setRobots($data->get('robots', true));
+        $this->setAvailableVariants($data->get('variant', App::getVariants()));
 
         $desktop = $entity->menu_desktop == '' ? 'children' : $entity->menu_desktop;