Page 1 of 1

How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Sat May 11, 2024 10:17 am
by JanoD
Hello,

I followed https://docs.joomla.org/J4.x:Developing ... _Site_Part and created a HelloWorld component in J4.

I am now trying to add a second view called Hello2 but this second view does not appear in the admin part in Joomla menu system as a link target (e.g. when linking this component to a menu). The first view will appear as an option but the second one does not. I am attaching the component. I am probably missing some detail. Any help appreciated.

Thanks,
Jan

Re: How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Sat May 11, 2024 10:55 am
by Webdongle

Re: How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Sat May 11, 2024 9:21 pm
by JanoD
Thanks for the link. Indeed, the same principle of adding an XML file for the menu item description of the "Hello" or "Hello2" page template (view) as with articles. It, however, still does not work for me.

Having

Code: Select all

com_helloworld/site/src/View/Hello2/HtmlView.php
com_helloworld/site/tmpl/hello2/default.php
makes the view available directly via a link, e.g. http://localhost/index.php?option=com_h ... iew=hello2.

So far so good.

Now adding a

Code: Select all

com_helloworld/site/tmpl/hello2/default.xml 
should create a menu item description which is then available in Joomla admin back-end but this only works for the first "Hello" view but not for the second view "Hello2".

I am attaching my simple component to this post. Any hint appreciated. Thanks.

Re: How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Sat May 11, 2024 10:48 pm
by Webdongle
You have no xml or php files for your alternative layouts. Without those they will not show up as a choice in the menu edit screen.

Re: How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Sun May 12, 2024 7:49 am
by SharkyKZ
Apparently, this has to be unique:

Code: Select all

<layout title="Hello World!">
If you change it to "Hello World 2" or something else that's different from the first layout, it will appear.

Re: How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Sun May 12, 2024 9:45 am
by Webdongle
And the xml and php files should match the unique name. I have done it sometime ago (following the page I linked to) as a Template override but not as a second view in a component.

Re: How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Sun May 12, 2024 5:12 pm
by ceford
The tutorial you were following is incomplete. You could try following this article:

https://jdocmanual.org/jdocmanual?manua ... started.md

It follows a take it apart to see how it works approach rather than a build it step by step approach. It has two sub-menu items in the Administrator menu.

Re: How to add a second view to a component (so that it is visible as an option when adding to a menu in administration)

Posted: Mon May 13, 2024 7:19 am
by JanoD
Thanks for all the helpful replies and links. The problem was the layout title that apparently must be unique (@sharkyKZ thanks for the tip).

With e.g.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="Hello World 2!">
        <message><![CDATA[My second Joomla! page]]></message>
    </layout>
</metadata>
the menu item for the "Hello2" view will appear. Thanks!!
com_helloworld_multiple_views_available_as_menu_items.png