0

Previously I was looking into how to get the template I'm currently editing, now I need to make use of a templates/templateName/helper.php file which would need a different kind of request.

// this gets me the template
$app = Factory::getApplication('site');
$template = $app->input->getValue('template');

Question:

How can I get the params from the input for the known $template?

1

If you want to access the parameters of a specific template inside the Helper, you could write a small database query, like so:

use Joomla\CMS\Factory;
use Joomla\Registry\Registry;

class TplCassiopeiaHelper
{
    private $params = null;

    public function __construct()
    {
        $this->params = $this->getParams();

        // You can now access specific parameters, inside the class, for example:
        // $this->params->get('colorName')
    }

    protected function getParams()
    {
        $db = Factory::getDbo();

        $db->setQuery(
            $db->getQuery(true)
                ->select('params')
                ->from('#__template_styles')
                ->where('template = ' . $db->q('cassiopeia'))
                ->where('client_id = 0')
        );
        $registry = new Registry;
        $registry->loadString($db->loadResult());

        return $registry;
    }
}

Or, when you instantiate the helper class, you could pass $this->params as a constructor argument.

2
  • Say I instantiate a new TplCassiopeiaHelper inside its doSomeAjax method, where/what would be the source of $this->params in that case?
    – thednp
    Mar 12 at 14:35
  • 1
    You don't need to instantiate the class inside its own ajax method. Instantiate the class once and pass in the template parameters via the constructor if you want
    – Lodder
    Mar 12 at 16:52
0

You can do it like this example:

$this->params->get('sitedescription'))

In this instance my XML looks like this:

<config>
    <fields name="params">
        <fieldset name="advanced">
            <field name="sitedescription"
                   type="text"
                   default=""
                   label="JGLOBAL_DESCRIPTION"
                   description="JGLOBAL_SUBHEADING_DESC"
                   filter="string"/>
            <field name="framework" type="list" default="bootstrap4" required="true" label="TPL_STARTER_BOOTSTRAP_4_LABEL" description="TPL_STARTER_BOOTSTRAP_4_DESCRIPTION">
                <option value="">TPL_STARTER_BOOTSTRAP_4_DEFAULT_OPTION</option>
                <option value="bootstrap4">Bootstrap4</option>
                <option value="foundation6">Foundation 6</option>
            </field>
        </fieldset>
    </fields>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.