So I have a joomla 5 site that I’d like to add a custom field when someone is posting a new article. The custom field will have a list of titles of other articles they have published in another category in a drop down list.
I’m using the sql query field under the backend editor for the custom fields. Whenever I try to call the function to grab the USERID I’ve found, into that box, it crashes the form to submit the article in either the front end or backend specifically saying I have an issue in that query, where I’ve tried to put the call to get the logged in user’s userid.
Is there any way to put something just in the query* box under the Articles: Edit Field to just pull the user id?
I’ve tried using just [user-id] in the field area, and calling some of the joomla factory calls in the query* field, but they all produce the same thin and crashes the form when trying to create and article to test. This is using only joomla 5 articles custom fields, no extra ACF plugins or components added.
To add a custom field in Joomla 5 that populates a dropdown with titles of other articles published by the logged-in user, you’ll need to execute a SQL query that incorporates the user ID dynamically. However, directly using functions like fetching the user ID within the SQL query field may not work as expected, especially if it causes form crashes.
Using a Custom SQL Query for the Dropdown
Instead of trying to call a function to get the user ID within the SQL query field, you can use a placeholder in your SQL query and set the user ID in the context of your query.
Create a Custom Field
- Go to Content > Fields.
- Create a new custom field, and choose the SQL field type.
Set Up the SQL Query
In the Query field, you might use something like:
sql
SELECT title AS value, id AS text
FROM #__content
WHERE created_by = :userid
AND state = 1
AND catid = [YourCategoryID]
ORDER BY title
Set the User ID Context
In the Options tab of your custom field, use the Context settings to pass the current user ID. You can do this by adding a new field or setting up the existing field to reference the user ID dynamically.
To achieve this, you can use a custom override or a plugin:
Custom Override or Plugin
Create a Plugin:
- If you’re familiar with creating Joomla plugins, you can create a simple plugin that sets the user ID as a parameter when the form is loaded.
- In the plugin, use the
onContentPrepareForm
event to modify the custom field parameters.
Set Parameters Dynamically:
- In the plugin code, retrieve the user ID with:
php
$user = JFactory::getUser();
$userid = $user->id;