Categories
CMS Joomla Mastering Development

Altering the SQL custom field

If I hardwire an sql custom field, the query works as it should (for this specific userid, 305):

SELECT id AS value, title AS text FROM #__content where catid = 8 and created_by = 305

What I want to do is allow the created_by clause to be dynamic : Substituted with JFactory::getUser()->id;

I figured if I make the custom SQL like this:

SELECT id AS value, title AS text FROM #__content where catid = 8 and created_by = %%userid%%

I could replace the %%userid%% with a dynamic number before the query is used.

I’ve tried bending the existing custom sql field – just to see if it will work in principle, but I’m not having too much luck.

In plugins/fields/sql/tmpl/sql.php I have:

$query = $fieldParams->get(‘query’, ”);

if (substr_count($value,”%%userid%%”) > 0){
$userid = JFactory::getUser()->id;
str_replace(“%%userid%%”, $userid, $query);
}

// Run the query with a having condition because it supports aliases
$db->setQuery($query . ‘ having value in (‘ . trim($condition, ‘,’) . ‘)’);

Ideally, I would create a brand new custom field, and be happy to donate it to the world!

I’m not even having much fun in trying to debug this !

Thanks for any suggestions.

Be safe !

Brian

Leave a Reply

Your email address will not be published. Required fields are marked *