WT Layout select - plugin for selecting the layout file

Downloads 16 Hits 9 CTR 178% Plg Free

Joomla Custom field plugin: dropdown with PHP layout files from configured folders, including active template overrides.

Description

The plugin adds a custom field type wtlayoutselect.

Joomla field capabilities:

  • scans configured folders with PHP layouts;
  • shows available options in a dropdown, including template overrides (/templates/<template>/html/...);
  • saves the selected value into field rawvalue with layout and basePath (JSON);
  • provides computed value in value (layout_id) that can be used with Joomla LayoutHelper::render().

Why use it?

This field is useful when you want to let a content manager choose a layout variant without editing template PHP files:

  • articles (com_content): article sections, cards, CTA blocks;
  • contacts (com_contact): different contact card templates;
  • categories and lists: different partial layouts for different contexts;
  • module output zones: include content/modules in the required layout variant.
  • ability to create reusable layout blocks for Joomla landing pages.

2 main ways to use the field

You use this field in your overrides for Joomla article/contact layouts or other components that support custom fields. To avoid automatic field output on the page, it is recommended to disable that in field settings. The field acts as a logical layout switch, and its output ($field->value) is primarily intended for developers. 

Option 1: render by computed $field->value

<?php
use Joomla\CMS\Layout\LayoutHelper;

$field = $this->item->jcfields[14] ?? null;

if ($field && !empty($field->value)) {
    echo LayoutHelper::render($field->value, ['item' => $this->item]);
}

Option 2: render from rawvalue with basePath

<?php
use Joomla\CMS\Layout\LayoutHelper;

$field = $this->item->jcfields[14] ?? null;

if ($field && !empty($field->rawvalue)) {
    $raw = json_decode($field->rawvalue);

    if (!empty($raw->layout) && !empty($raw->basePath)) {
        echo LayoutHelper::render($raw->layout, ['item' => $this->item], $raw->basePath);
    }
}

How to set folders in field params and what computed layout_id you get

Examples below are for field wtlayoutselect with selected file pricelist.php.

Folder in field paramsWhere file is locatedrawvalue (key part)Computed layout_id in $field->value
layouts/com_content/article/sections JPATH_ROOT/layouts/com_content/article/sections/pricelist.php basePath=layouts/com_content/article/sectionslayout=pricelist com_content.article.sections.pricelist
layouts/com_content/article/sections templates/<template>/html/com_content/article/sections/pricelist.php (override) basePath=layouts/com_content/article/sectionslayout=pricelist com_content.article.sections.pricelist
templates/multizone/html/com_content/article/sections JPATH_ROOT/templates/multizone/html/com_content/article/sections/pricelist.php basePath=templates/multizone/html/com_content/article/sectionslayout=pricelist templates.multizone.html.com_content.article.sections.pricelist
components/com_contact/layouts/contact/parts JPATH_ROOT/components/com_contact/layouts/contact/parts/pricelist.php basePath=components/com_contact/layouts/contact/partslayout=pricelist components.com_contact.layouts.contact.parts.pricelist

Important:

  • For paths starting with layouts/..., the plugin removes layouts. prefix in $field->value so layout_id can be passed directly to LayoutHelper.
  • For paths outside JPATH_ROOT/layouts (for example, templates/...components/...), layout_id includes full path in dot format.
  • In such cases, rendering via rawvalue with explicit basePath (option 2 above) is usually more reliable.

Example call in article override (/templates/<template>/html/com_content/article/default.php):

<?php
use Joomla\CMS\Layout\LayoutHelper;

$field = null;

foreach (($this->item->jcfields ?? []) as $f) {
    if (($f->name ?? '') === 'article_layout_variant') {
        $field = $f;
        break;
    }
}

if ($field && !empty($field->value)) {
    echo LayoutHelper::render($field->value, ['item' => $this->item]);
}

Layout examples: when item is passed from article context

Below are layout file examples (for instance in layouts/com_content/article/sections/...) when you pass ['item' => $this->item].

Example 1: basic article section layout

File: layouts/com_content/article/sections/lead.php

<?php
/** @var array $displayData */

defined('_JEXEC') or die;

extract($displayData);

if (empty($item)) {
    return;
}
?>
<section class="article-lead">
    <h2><?php echo htmlspecialchars($item->title ?? '', ENT_QUOTES, 'UTF-8'); ?></h2>

    <?php if (!empty($item->images)) :
        $images = json_decode($item->images);
        $intro  = $images->image_intro ?? '';
    ?>
        <?php if (!empty($intro)) : ?>
            <img src="/<?php echo htmlspecialchars($intro, ENT_QUOTES, 'UTF-8'); ?>" alt="">
        <?php endif; ?>
    <?php endif; ?>

    <div>
        <?php echo $item->introtext ?? ''; ?>
    </div>
</section>

Example 2: getting article custom fields inside layout

File: layouts/com_content/article/sections/specs.php

<?php
/** @var array $displayData */

defined('_JEXEC') or die;

extract($displayData);

if (empty($item)) {
    return;
}

$fields = $item->jcfields ?? [];
$sku    = null;
$price  = null;

foreach ($fields as $f) {
    if (($f->name ?? '') === 'sku') {
        $sku = $f->rawvalue;
    }

    if (($f->name ?? '') === 'price') {
        $price = $f->rawvalue;
    }
}
?>
<div class="product-specs">
    <?php if (!empty($sku)) : ?><p>SKU: <?php echo htmlspecialchars($sku, ENT_QUOTES, 'UTF-8'); ?></p><?php endif; ?>
    <?php if (!empty($price)) : ?><p>Price: <?php echo htmlspecialchars($price, ENT_QUOTES, 'UTF-8'); ?></p><?php endif; ?>
</div>

Rendering custom module position via ModuleHelper in Joomla overrides

File: layouts/com_content/article/sections/sidebar-modules.php

<?php
/** @var array $displayData */

defined('_JEXEC') or die;

use Joomla\CMS\Helper\ModuleHelper;

extract($displayData);

// Optionally choose position by article/category
$position = 'article-sidebar';

$modules = ModuleHelper::getModules($position);

if (empty($modules)) {
    return;
}
?>
<aside class="article-sidebar-modules">
    <?php
        // Render all modules from this position
        foreach ($modules as $module) : ?>
        <div class="article-sidebar-module">
            <?php echo ModuleHelper::renderModule($module, ['style' => 'none']); ?>
        </div>
    <?php endforeach; ?>
</aside>

 

Joomla

Extension type:
Plugin
Folder:
Fields
Joomla version:
6.0.3

What's new

Addition

Initial release

First extension version

WebTolk Joomla Extensions

103 Extensions
12 Categories
520 Versions released
656219 Downloads