Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 2033

Joomla! 5.x Coding • Dynamic sql custom field

$
0
0
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:

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);            }        }    }}

Statistics: Posted by fotonio — Sat Jan 11, 2025 11:26 am



Viewing all articles
Browse latest Browse all 2033

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>