Dynamically Changing the Edit Site View Field Values #1192
Labels
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: joomla/Component-Builder#1192
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Steps to reproduce the issue
Open Admin View as an edit Site View.
Expected result
I went thru the below wiki items and many other topics during the last two weeks.
Field Types
Basic Fields
Admin Views
Advanced Fields
dynamicGet
How to overwrite the custom fields
Setup Site Edit View in JCB
"Setup Site Edit View in JCB" was able to direct me
on how to give the users the ability to add their own record without having access to the administrator view from the admin.
Here is where I am stuck with:
When the site view open on edit mode, the value are the set values in the fields that where setup.
Example: on the enclosed screenshot "roomid" shows value 1 since that is the default. I have a variable named "$newroomid" which value is 831765236I would like to dynamically replace the 1 for 831765236. The way arrive to the new number is that the first 3 digits, 831 is the generic user current user id. The rest, 765236 is a random calculated number with the PHP function mt_rand(100000,1000000). The two are combined to get 831765236. This allows me to have a unique "roomid" since the first 3 digits will prevent me from the random be duplicated and assigned to another user.
In short, I want to override dynamically the values on the edit view as it opens. Is there a way to do this in the JCB function or from your experience is there another short and simple way to do it?
The fields that I would like to dynamically override are:
a.roomid AS roomid
a.roomname AS roomname
a.moderatorid AS moderatorid
a.moderatorpass AS moderatorpass
a.meetdate AS meetdate
a.meettype AS meettype
a.meetstatus AS meetstatus
a.meetlength AS meetlength
a.maxparticipants AS maxparticipants
Actual result
Once the edit Site View opens it shows the default fields values from the Admin View.
System information (as much as possible)
Additional comments
The JCB wiki is great and has a lot of information. JCB has excellent features that I this point I am not
ready to use. I am focusing on basic component building. I was able to rewrite my first Joomla 3.10 component into
my Joomla 5.2 without ever done a Joomla Component before. I am converting my second Joomla 3.10 from also from scratch.
To dynamically override field values in the site edit view, follow this updated and accurate workflow:
1. Create a Dynamic GET
Dynamic GET is used to control and populate the data for your site view. Here’s how to create a custom Dynamic GET to handle your logic:
Steps to Create a Dynamic GET:
Custom PHP for the Dynamic GET:
Key Notes:
$data
must be lowercase, as this is the expected variable name in JCB’s dynamic GET.$data
, depending on your preference and how the view handles it.2. Link the Dynamic GET to the Site View
Once you’ve created the dynamic GET, you need to link it to your site view as the main GET.
Steps to Link the Dynamic GET:
How This Works:
$data
variable from your dynamic GET will populate the form fields in the view (custom code needed here :) since site views are normally not forms. See next comment for more info...3. Ensure Data Aligns with the View
The form fields in the site view will automatically use the
$data
object to populate their values. For example:$data->roomid
will populate theroomid
field.$data->moderatorid
will populate themoderatorid
field.Make sure the fields in your view match the keys in the
$data
object.Why Use This Approach?
Final Workflow Summary
$data
with your desired dynamic values.Let me know if you have further questions or need additional clarification!
I'll clarify how forms are typically handled in JCB and provide guidance on creating custom site view forms if needed.
Default Approach: Admin Views for Forms
JCB generally doesn’t use site views for forms. Instead, we rely on admin views for handling forms, even when they are displayed on the site side of an application. This method provides flexibility and reduces duplication of effort. Here's why:
Admin Views on the Site Side:
In the admin view, you can simply tick the option to display the view on the site side. This allows the same form to serve both admin and site use cases without needing to recreate or duplicate form logic.
Customization Options in Admin Views:
Admin views allow extensive customization of the datasets and fields through custom field types or within the form function itself.
Joomla
form object, enabling you to adjust the form fields and values programmatically.Conditional Behavior for Site Views:
If you're using the form in both admin and site contexts, you can include a check to ensure your custom code only triggers for the desired context. For example, a simple condition can ensure the code runs only for the site view. If the code applies to both contexts, you can leave it without a check.
Creating Custom Site View Forms
If you prefer to build a standalone custom site view form, that is entirely possible as well. However, this approach typically requires more custom code. A prime example of this in action is the JCB Compiler itself, which uses a custom admin view.
Here’s how to approach it:
When to Use Admin Views vs. Custom Views
The primary difference between admin views and custom site/admin views lies in their complexity:
Admin views also allow moving dynamic behavior to custom field types. For example, you can:
Final Thoughts
The admin view’s dual-purpose design (admin and site editing) is one of JCB’s core strengths. While creating a custom site view is absolutely possible, leveraging an admin view and ticking the site option is often the faster and more efficient route. JCB's focus has always been on giving experienced developers tools to speed up their production process.
For further insights, I recommend reviewing the custom admin view used by the JCB Compiler. It showcases how dynamic and custom behavior can be implemented effectively. Let me know if you need a link to this example or further details!
Apologies for the delayed response—our workload has been quite heavy recently. I hope this points you in the right direction!