Skip to content

Commit

Permalink
Add namespace to templates
Browse files Browse the repository at this point in the history
As discussed in the #30272 report, Joomla does not support namespaced templates, this commit solves that.

**Changes**
* the `create()` method now checks for namespaced `template` extension types
* the `getNamespaces()` method properly recognize *Administrator* and *Site* templates; in addition, it will look for `templateDetails.xml` file instead of `extensionName.xml` and read the designated namespace (Line 182)


**Other change**
* replaced `defined('_JEXEC') or die;` with `defined('JPATH_PLATFORM') or die;`, perhaps this change was also expected? Either way, it was my best guess.


**Test setup - also a good piece of documentation draft**
In order to enable namespace for templates, here are the few steps, we'll use the default site template:
* open `site/templates/cassiopeia/templateDetails.xml` and add between `<description>` and `<files>` the following
```markup
<namespace path="src">Joomla\Template\Cassiopeia</namespace>
```
* delete `site/administrator/cache/autoload_psr4.php`
* login to your `site/administrator`
* open the newly created `site/administrator/cache/autoload_psr4.php`, the last element of the return object should be
```
'Joomla\\Template\\Cassiopeia\\Site\\' => [JPATH_SITE . '/templates/cassiopeia/src']
```
* *optional*:  create a custom field in `site/templates/cassiopeia/src/Field` folder and add it to the `templateDetails.xml` along with `addfieldprefix="Joomla\Template\Cassiopeia\Site\Field"` attribute added to the parent `<fieldset>`
* *optional* try and call the newly added custom field from your namespaced template into a module/plugin, test it out


**Questions**
* should I commit this change to the `4.1-dev` branch as well?
* should I commit this change to the `staging` branch? If yes, where is the `namespacemap.php` file?
  • Loading branch information
thednp committed Sep 30, 2020
1 parent e45709c commit 472c66c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions libraries/namespacemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;
defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
Expand Down Expand Up @@ -35,7 +35,7 @@ class JNamespacePsr4Map
*/
public function exists()
{
return is_file($this->file);
return file_exists($this->file);
}

/**
Expand Down Expand Up @@ -65,6 +65,7 @@ public function create()
$extensions = array_merge(
$this->getNamespaces('component'),
$this->getNamespaces('module'),
$this->getNamespaces('template'),
$this->getNamespaces('plugin'),
$this->getNamespaces('library')
);
Expand Down Expand Up @@ -139,7 +140,7 @@ protected function writeNamespaceFile($elements)
*/
private function getNamespaces(string $type): array
{
if (!in_array($type, ['component', 'module', 'plugin', 'library'], true))
if (!in_array($type, ['component', 'module', 'plugin', 'template', 'library'], true))
{
return [];
}
Expand All @@ -153,6 +154,10 @@ private function getNamespaces(string $type): array
{
$directories = [JPATH_SITE . '/modules', JPATH_ADMINISTRATOR . '/modules'];
}
elseif ($type === 'template')
{
$directories = [JPATH_SITE . '/templates', JPATH_ADMINISTRATOR . '/templates'];
}
elseif ($type === 'plugin')
{
$directories = Folder::folders(JPATH_PLUGINS, '.', false, true);
Expand All @@ -172,11 +177,13 @@ private function getNamespaces(string $type): array
$extensionPath = $directory . '/' . $extension . '/';

// Strip the com_ from the extension name for components
// $name = $type === 'component' ? str_replace('com_', '', $extension, $count) : $extension;
$name = str_replace('com_', '', $extension, $count);
$name = $type === 'template' ? 'templateDetails' : $name;
$file = $extensionPath . $name . '.xml';

// If there is no manifest file, ignore. If it was a component check if the xml was named with the com_ prefix.
if (!is_file($file))
if (!file_exists($file))
{
if (!$count)
{
Expand All @@ -185,7 +192,7 @@ private function getNamespaces(string $type): array

$file = $extensionPath . $extension . '.xml';

if (!is_file($file))
if (!file_exists($file))
{
continue;
}
Expand Down

0 comments on commit 472c66c

Please sign in to comment.