diff --git a/libraries/src/Menu/AbstractMenu.php b/libraries/src/Menu/AbstractMenu.php index f5562b7ce79da..36931ebb9ffe7 100644 --- a/libraries/src/Menu/AbstractMenu.php +++ b/libraries/src/Menu/AbstractMenu.php @@ -62,6 +62,14 @@ abstract class AbstractMenu */ protected $user; + /** + * Flag for checking if the menu items have been loaded + * + * @var boolean + * @since __DEPLOY_VERSION__ + */ + private $itemsLoaded = false; + /** * Class constructor * @@ -71,17 +79,6 @@ abstract class AbstractMenu */ public function __construct($options = array()) { - // Load the menu items - $this->load(); - - foreach ($this->getMenu() as $item) - { - if ($item->home) - { - $this->default[trim($item->language)] = $item->id; - } - } - $this->user = isset($options['user']) && $options['user'] instanceof User ? $options['user'] : Factory::getUser(); } @@ -166,14 +163,17 @@ public function setDefault($id, $language = '*') */ public function getDefault($language = '*') { + // Get menu items first to ensure defaults have been populated + $items = $this->getMenu(); + if (\array_key_exists($language, $this->default)) { - return $this->getMenu()[$this->default[$language]]; + return $items[$this->default[$language]]; } if (\array_key_exists('*', $this->default)) { - return $this->getMenu()[$this->default['*']]; + return $items[$this->default['*']]; } } @@ -301,6 +301,21 @@ public function getParams($id) */ public function getMenu() { + if (!$this->itemsLoaded) + { + $this->load(); + + foreach ($this->items as $item) + { + if ($item->home) + { + $this->default[trim($item->language)] = $item->id; + } + } + + $this->itemsLoaded = true; + } + return $this->items; } diff --git a/libraries/src/Menu/SiteMenu.php b/libraries/src/Menu/SiteMenu.php index 06aa45c370119..7c5205fb6022b 100644 --- a/libraries/src/Menu/SiteMenu.php +++ b/libraries/src/Menu/SiteMenu.php @@ -190,12 +190,12 @@ public function load() return false; } - foreach ($this->getMenu() as &$item) + foreach ($this->items as &$item) { // Get parent information. $parent_tree = array(); - if (isset($this->getMenu()[$item->parent_id])) + if (isset($this->items[$item->parent_id])) { $item->setParent($this->getMenu()[$item->parent_id]); $parent_tree = $this->getMenu()[$item->parent_id]->tree; @@ -277,14 +277,17 @@ public function getItems($attributes, $values, $firstonly = false) */ public function getDefault($language = '*') { + // Get menu items first to ensure defaults have been populated + $items = $this->getMenu(); + if (\array_key_exists($language, $this->default) && $this->app->isClient('site') && $this->app->getLanguageFilter()) { - return $this->getMenu()[$this->default[$language]]; + return $items[$this->default[$language]]; } if (\array_key_exists('*', $this->default)) { - return $this->getMenu()[$this->default['*']]; + return $items[$this->default['*']]; } } }