joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.73k stars 3.64k forks source link

Joomla 4.3.3 Alternate Layouts don't work when there is a subline in the template #41270

Closed coolcat-creations closed 1 year ago

coolcat-creations commented 1 year ago

Steps to reproduce the issue

have a template named yourtemplate_child

make a copy of html/com_content/category/default.php html/com_content/category/default.xml html/com_content/category/default_articles.php html/com_content/category/default_children.php

rename them and put in this template an alternate layout for a category list: html/com_content/category/mylayout.php html/com_content/category/mylayout.xml html/com_content/category/mylayout_myarticles.php html/com_content/category/mylayout_mychildren.php

In mylayout.php change articles to myarticles (I did that but did not mentio in the test instructions) Change something in the _myarticles file or put a die(); at the top to see if your template is loaded. Change the layout title in the mylayout.xml so you recognize it in the menu item types overview

Create a new menu item and select my layout as a menu item type

Expected result

your alternate layout is loaded when you selected it as a menu type

Actual result

Several issues here: 1) the default view is loading a layout that needs to be adjusted as well. and no matter how your _articles and _children files are named it will always load children

so a step in between would be to create a file named: /templates/yourtemplate/html/layouts/joomla/content/category_mylayout.php and rename also the subtemplates in the $displayData->loadTemplate call

But even if you do this - you will get a 500 Error when your template has an underscore in it.

If you move the whole structure to a template without underscore it works.

System information (as much as possible)

I tried to debug within the libraries/src/MVC/View/HtmlView.php file and when I print out the path Joomla is looking for I get:

mytemplate_nlh_nlh_nlh/html/ altough my templates name is only mytemplatee_nlh, so I think it has something to do with the underscores in the template name.

richard67 commented 1 year ago

As far as I know this is expected behaviour, and it was always like that. There might be even some tooltip in the template name field telling that only alpha-numeric characters and dashes are allowed.

If someone else confirms this (I don't expect myself to be perfect), I will close this issue as expected behaviour.

brianteeman commented 1 year ago

I have a site using a child template cassiopeia_leadershape As you can see from the screenshot below I have lots of blogcategory overrides that have been renamed as something else and also have an xml file

image

coolcat-creations commented 1 year ago

@brianteeman ok so the issue is not coming from the underscore? Strange :-/

brianteeman commented 1 year ago

silly question but in html/com_content/category/mylayout.php did you update the loadTemplate for your new names $this->loadTemplate('myarticles')

brianteeman commented 1 year ago

It can NOT come from the underscore because that is how child templates are created by joomla

coolcat-creations commented 1 year ago

yes I did - the error dissapears when I move the complete structure from the child to the main - so something else is interfering

brianteeman commented 1 year ago

well its something in your code and not a core issue

brianteeman commented 1 year ago

@richard67 image

coolcat-creations commented 1 year ago

Not in my code but maybe some exception for not child template templates with underscore - i will investigate further

coolcat-creations commented 1 year ago

@brianteeman and @richard67 thanks for testing - I will report back what the issue was.

chmst commented 1 year ago

@coolcat-creations if you make a template override for any default and call it mylayout.php then this script still contains a statement like

echo $this->loadTemplate('item');

it will load mylayout_item.php if yo did not override this statement to make

echo $this->loadTemplate('myitem');

brianteeman commented 1 year ago

@chmst thats what I wrote https://github.com/joomla/joomla-cms/issues/41270#issuecomment-1654055840

coolcat-creations commented 1 year ago

@brianteeman @chmst I wrote both correctly in the layout. As said it works as indented when I move it in another template. there must be something else interfering. I am investigating further.

brianteeman commented 1 year ago

I just repeated it from scratch with no problem at all

coolcat-creations commented 1 year ago

I just tested with a not template and named it test_test and it worked too. So the issue is not in the core but something in the maintemplate probably. Thank you all for testing.

hendrikbehncke commented 1 year ago

The problem is because the child template folder name begins with the same name as the template (e.g. cassiopeia and cassiopeia_child).

Here is a fix for it in libraries/MVC/HtmlView.php on line 377 Add a directory separator to prevent replacing the template path twice.

// Change the template folder if alternative layout is in different template
if (isset($layoutTemplate) && $layoutTemplate !== '_' && $layoutTemplate != $template->template) {
    $this->_path['template'] = str_replace(
        JPATH_THEMES . DIRECTORY_SEPARATOR . $template->template . DIRECTORY_SEPARATOR,
        JPATH_THEMES . DIRECTORY_SEPARATOR . $layoutTemplate . DIRECTORY_SEPARATOR,
        $this->_path['template']
    );
}
brianteeman commented 1 year ago

so why is it working perfectly for me (and others)

hendrikbehncke commented 1 year ago

so why is it working perfectly for me (and others)

This depends on how many subtemplates are loaded, therefore the HtmlView::loadTemplate() method is called multiple times.

$this->subtemplatename = 'myarticles';
echo LayoutHelper::render('joomla.content.category_default', $this);
brianteeman commented 1 year ago

Should be closed as there is a PR

alikon commented 1 year ago

please test #41274