Advertisement

Dynamic sql custom field

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
fotonio
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Nov 28, 2017 12:05 am

Dynamic sql custom field

Post by fotonio » Sat Jan 11, 2025 11:26 am

Hi,
I have an sql custom field inside a subform for content aritcles.
If i put in the query SELECT created_by AS value, created_by AS text FROM #__content WHERE id = 6 , i get the user id that created the article.
I would like to have SELECT created_by AS value, created_by AS text FROM #__content WHERE id = article_id ,so the query can take dynamically the article_id when each article is edited without user input.

I have tried to override the render.php but as i learned from searching around, the field value of sql is assigned on onCustomFieldsPrepare before it renders.

Chatgpt suggests this code but i am not sure in which file to use it.

Code: Select all

use Joomla\CMS\Factory;

class PlgFieldsDynamicSQL extends \Joomla\CMS\Plugin\CMSPlugin
{
    public function onCustomFieldsPrepareField($field, $context, $item)
    {
        // Ensure this applies only to the specific custom field and context
        if ($field->type === 'sql' && $context === 'com_content.article')
        {
            // Get the article ID dynamically
            $app = Factory::getApplication();
            $articleId = $app->input->getInt('id', 0); // Get the article ID from the request

            // Replace the placeholder with the article ID in the SQL query
            if ($articleId && strpos($field->element, ':article_id') !== false)
            {
                $field->element = str_replace(':article_id', $articleId, $field->element);
            }
        }
    }
}

Advertisement
User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2990
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Dynamic sql custom field

Post by ceford » Sat Jan 11, 2025 5:52 pm

You could look at the exisiting plugins/xxx for inspiration. There is one for sql field.

You could look at the plugin coding examples: https://manual.joomla.org/docs/building ... n-sqlfile/

Or you could search a current Joomla test installation for onCustomFieldsPrepare - there are none with that whole word but plenty with that as a root.

It is not clear whether you will have to write your own plugin.

fotonio
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Nov 28, 2017 12:05 am

Re: Dynamic sql custom field

Post by fotonio » Sat Jan 11, 2025 9:45 pm

I tried something else. Instead of sql i made a text field and following this solution by Sharky i managed to get the created_by value by creating an override to html/layouts/com_fields/field/cust-render.php with the code

Code: Select all

$created_by = $displayData['item']->created_by;
if ($field->id == '75')  { $value = $created_by; }
And it works. But when i put the text field inside the subform it stops working.

I see that subfield values probably are prepared in plugins/fields/subform/src/Extension/Subform.php

Any tips?

Advertisement

Post Reply

Return to “Joomla! 5.x Coding”