Adding target blank to menu item

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
aldo
User
Posts: 8

Adding target blank to menu item

Post by aldo »

I've read a lot on already posted topics on how to add a Target _blank to one of the menu items but since the versions have changed, I'm finding answers but they don't work.

I am adding the below code to my Menu_Rendering function and I'm getting

C:\xampp\htdocs\tracking\src\userfn.php(75): Attempt to assign property "Target" on null

I am able to get the menu item object by FindItem(2) and it makes sense...and then ->Target to access the objects target value...

function Menu_Rendering() {
  $this->FindItem(2)->Target = "_blank";
}

I can go about adding a target _blank in other ways which is not my issue, but like to know how I can grab the menu's object values, for example, Text, Url, Name etc.

Any help is appreciated.


mobhar
User
Posts: 11726

Post by mobhar »

If you're using v2024, it should be findItem instead of FindItem.


arbei
User
Posts: 9384

Post by arbei »

The error meant $this->findItem(2) returns null, your id is wrong. (PHP function/method names are case-insensitive.)


aldo
User
Posts: 8

Post by aldo »

I try the below and it does find the menu item object and shows everything using the var_dump function. I have ID 1 and 2 so when I switch the ID's it does show the items in the var_dump for each menu item.


function Menu_Rendering()
{
    //$this->findItem(2)->Target = "_blank";

    $finditem = $this->finditem(2);

    var_dump($finditem);
}

result

NULL object(PHPMaker2024\phpmakerTestProject\MenuItem)#399 (18) { ["SubMenu"]=> NULL ["Target"]=> string(0) "" ["Href"]=> string(0) "" ["Active"]=> bool(false) ["Attrs"]=> object(PHPMaker2024\phpmakerTestProject\Attributes)#401 (1) { ["attrs":"PHPMaker2024\phpmakerTestProject\Attributes":private]=> array(0) { } } ["Level"]=> int(0) ["Id"]=> int(2) ["Name"]=> string(14) "mi_ws_tracking" ["Text"]=> string(11) "ws tracking" ["Url"]=> string(14) "wstrackinglist" ["ParentId"]=> int(-1) ["Allowed"]=> bool(true) ["IsHeader"]=> bool(false) ["IsCustomUrl"]=> bool(false) ["Icon"]=> string(0) "" ["Label"]=> string(0) "" ["IsNavbarItem"]=> bool(false) ["IsSidebarItem"]=> bool(true) }


mobhar
User
Posts: 11726

Post by mobhar »

You should check your menu id from the generated views/menu.php file.


arbei
User
Posts: 9384

Post by arbei »

As you can see in the dump, you did find the item with id=2 (not null now), and you can see the Target property which you can set. You can also verify the value of the property of the menu item in Menu_Rendered server event.


aldo
User
Posts: 8

Post by aldo »

Hi, yes exactly, that is what I stated before...I see the dump as I'm pulling the correct menu item object using the menu ID which I got from menu.php file. My issue is not being able to set the Target property, when I call the with the function below, I get the NULL since its not being able to access the objects Target property or any of them.

function Menu_Rendering() {
  $this->findItem(2)->Target = "_blank";
}

mobhar
User
Posts: 11726

Post by mobhar »

You may put this following code in MenuItem_Adding server event:

if ($item->Id == 2) 
    $item->Target = "_blank";

aldo
User
Posts: 8

Post by aldo »

Thanks, retrieving the objects ID and Target item worked within methods function MenuItem_Adding. I was able to set the Target to _blank. I know in Menu_Rendering function before the $menu argument had to be used and then $menu->FindItem(222)->Target or $menu->Text, and now we have to use $this inside the method.

So I tried doing:

function Menu_Rendering() {
  
   var_dump($this->Id); // prints string(6) "navbar" navbarstring(4) "menu"

   echo $this->Id; // prints menu
 
}

So I guess the way to get the menu item object values is via method MenuItem_Adding.

Again thanks for the inputs, at least I got somewhere :)


arbei
User
Posts: 9384

Post by arbei »

Note that the Menu_Rendering is fired for both the top menu and the side menu. If the menu item does not exists in the top menu (or vice versa), you cannot find the item and hence the error. You should check the menu ID first, see examples under Menu_Rendering, e.g.

if ($this->Id == "menu") { // Check "menu" for side menu, or "navbar" for the top menu
    // Your code
}

mobhar
User
Posts: 11726

Post by mobhar »

Thanks. It really helps.


aldo
User
Posts: 8

Post by aldo »

Thank you and all for your inputs. Yes I found that out after var_dump($this->Id); // prints string(6) "navbar" navbarstring(4) "menu" and $this->Id; // prints menu.
The reference really clears things up. 👌


Post Reply