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