Question

I'm trying to understand the helper.php file in the mod_menu folder of Joomla 3.x. The section of code which I am looking it is where we come across the first isset call to check the values of our new introduced elements

In particular Im trying to figure out how $item->shallower is able to calculate the depth of the menu structure which differentiates from the deeper, It allows us to create lists in list elements via the default output template for this particular module.

    // add these elements to each item

    $item->deeper     = false;
    $item->shallower  = false;
    $item->level_diff = 0;

if (isset($items[$lastitem])) {

    $items[$lastitem]->deeper     = ($item->level > $items[$lastitem]->level);
    $items[$lastitem]->shallower  = ($item->level < $items[$lastitem]->level);
    $items[$lastitem]->level_diff = ($items[$lastitem]->level - $item->level);

    var_dump($items[$lastitem]->shallower);//(bool)true or false
    var_dump($item->level);//string
    var_dump($items[$lastitem]->level);//string
    }

I am just struggling to get my head round this piece of code, more so both $item->deeper and $item->level seem to working as they should, but I have no understanding of what $lastitem]->shallower is doing. Any explanation would be most helpful.

regards

w9914420

Was it helpful?

Solution

The key is $lastitem, which is the index of the previous item created; it is set right after the code you pasted:

$lastitem     = $i;

where $i is the key in the foreach loop above that iterates through all $items with the variable $item.

Thus, on the next iteration, $items[$lastitem] is the previous item created.

In order to determine if an item has subitems, it's sufficient to compare the current and previous elements levels. This is an efficient way to achieve this, as only one iteration through all the items is necessary to build the data structure and integrate the deeper/shallower fields.

Shallower is the opposite of deeper:

+ item
+-- item // this is deeper;
+-- item
+-- item
+ item   // this is shallower;
+ item
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top