/**
* Method to find the item in the menu structure
*
* @param array $needles Array of lookup values
*
* @return mixed
*
* @since 3.1
*/
protected function findItem($needles = array())
{
$app = Factory::getApplication();
$menus = $app->getMenu('site');
$language = $needles['language'] ?? '*';
// $this->extension may not be set if coming from a static method, check it
if ($this->extension === null) {
$this->extension = $app->input->getCmd('option');
}
// Prepare the reverse lookup array.
if (!isset(static::$lookup[$language])) {
static::$lookup[$language] = array();
$component = ComponentHelper::getComponent($this->extension);
$attributes = array('component_id');
$values = array($component->id);
if ($language !== '*') {
$attributes[] = 'language';
$values[] = array($needles['language'], '*');
}
$items = $menus->getItems($attributes, $values);
foreach ($items as $item) {
if (isset($item->query) && isset($item->query['view'])) {
$view = $item->query['view'];
if (!isset(static::$lookup[$language][$view])) {
static::$lookup[$language][$view] = array();
}
if (isset($item->query['id'])) {
if (\is_array($item->query['id'])) {
$item->query['id'] = $item->query['id'][0];
}
/*
* Here it will become a bit tricky
* $language != * can override existing entries
* $language == * cannot override existing entries
*/
if ($item->language !== '*' || !isset(static::$lookup[$language][$view][$item->query['id']])) {
static::$lookup[$language][$view][$item->query['id']] = $item->id;
}
}
}
}
}
if ($needles) {
foreach ($needles as $view => $ids) {
if (isset(static::$lookup[$language][$view])) {
foreach ($ids as $id) {
if (isset(static::$lookup[$language][$view][(int) $id])) {
return static::$lookup[$language][$view][(int) $id];
}
}
}
}
}
$active = $menus->getActive();
if ($active && $active->component === $this->extension && ($active->language === '*' || !Multilanguage::isEnabled())) {
return $active->id;
}
// If not found, return language specific home link
$default = $menus->getDefault($language);
return !empty($default->id) ? $default->id : null;
}