Dynamically Changing the Edit Site View Field Values #1192

Open
opened 2024-11-18 19:14:31 +00:00 by eciudad · 2 comments

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)

  • OS Name & Version: Linux lamp302.cloudaccess.net 3.10.0-962.3.2.lve1.5.85.el6h.x86_64 #1 SMP Thu Apr 18 09:46:51 UTC 2024 x86_64
  • MySql Version: 8.0.34-cll-lve
  • Apache Version:
  • PHP Version: 8.2.23
  • Joomla Version: Joomla! 5.1.4 Stable [ Kudumisha ] 27-August-2024 16:00 GM
  • JCB Version: 5.04
  • Browser: Microsoft Edge Version 130.0.2849.46 (Official build) (64-bit)

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.

### 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) - OS Name & Version: Linux lamp302.cloudaccess.net 3.10.0-962.3.2.lve1.5.85.el6h.x86_64 #1 SMP Thu Apr 18 09:46:51 UTC 2024 x86_64 - MySql Version: 8.0.34-cll-lve - Apache Version: - PHP Version: 8.2.23 - Joomla Version: Joomla! 5.1.4 Stable [ Kudumisha ] 27-August-2024 16:00 GM - JCB Version: 5.04 - Browser: Microsoft Edge Version 130.0.2849.46 (Official build) (64-bit) ### 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.
Owner

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:

    1. Navigate to the Dynamic GET area in JCB.
    2. Create a new Dynamic GET entry.
    3. Configure the following:
      • Main Source: Set this to Custom (since you are writing your own logic).
      • GET Type: Set this to getItem for fetching a single record.
  • Custom PHP for the Dynamic GET:

    // Generate a unique room ID
    $randomPart = mt_rand(100000, 1000000);
    $newRoomId = $this->userId . $randomPart;
    
    // Populate the data object (must be $data =)
    $data = (object) [
        'roomid' => $newRoomId,
        'moderatorid' => $this->userId,
        'moderatorpass' => bin2hex(random_bytes(4)), // Example: Random password
        'meetdate' => date('Y-m-d H:i:s'), // Current date and time
        'meettype' => 'Scheduled', // Example: Static value
        'meetstatus' => 'Pending',
        'meetlength' => 60, // Default meeting length (in minutes)
        'maxparticipants' => 10 // Example default max participants
    ];
    
    // No need to return `$data`, as JCB handles this automatically.
    

    Key Notes:

    • $data must be lowercase, as this is the expected variable name in JCB’s dynamic GET.
    • You can use either an object or an array for $data, depending on your preference and how the view handles it.

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:

    1. Go to the Site Views section in JCB.
    2. Open the site edit view configuration.
    3. Locate the Main GET dropdown in the site view.
    4. Select the dynamic GET you just created.
    5. Save your changes.

    How This Works:

    • When the site view is accessed (e.g., in edit mode), JCB will automatically call the linked dynamic GET.
    • The $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 the roomid field.
  • $data->moderatorid will populate the moderatorid field.

Make sure the fields in your view match the keys in the $data object.


Why Use This Approach?

  • Customizable Logic: Writing custom logic in the dynamic GET allows you full control over the data.
  • Scalability: As your requirements grow, you can extend this dynamic GET or create additional GETs for other use cases.
  • JCB Best Practices: By linking the dynamic GET to the site view via the Main GET, you ensure your solution is clean, maintainable, and fully compatible with JCB’s framework.

Final Workflow Summary

  1. Create a Dynamic GET in JCB.
  2. Write Custom PHP to populate $data with your desired dynamic values.
  3. Link the Dynamic GET to the site view as the Main GET.
  4. Test the site view to ensure the dynamic values are populated correctly.

Let me know if you have further questions or need additional clarification!

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:** 1. Navigate to the **Dynamic GET** area in JCB. 2. Create a new Dynamic GET entry. 3. Configure the following: - **Main Source**: Set this to **Custom** (since you are writing your own logic). - **GET Type**: Set this to **getItem** for fetching a single record. - **Custom PHP for the Dynamic GET:** ```php // Generate a unique room ID $randomPart = mt_rand(100000, 1000000); $newRoomId = $this->userId . $randomPart; // Populate the data object (must be $data =) $data = (object) [ 'roomid' => $newRoomId, 'moderatorid' => $this->userId, 'moderatorpass' => bin2hex(random_bytes(4)), // Example: Random password 'meetdate' => date('Y-m-d H:i:s'), // Current date and time 'meettype' => 'Scheduled', // Example: Static value 'meetstatus' => 'Pending', 'meetlength' => 60, // Default meeting length (in minutes) 'maxparticipants' => 10 // Example default max participants ]; // No need to return `$data`, as JCB handles this automatically. ``` **Key Notes:** - `$data` must be lowercase, as this is the expected variable name in JCB’s dynamic GET. - You can use either an object or an array for `$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:** 1. Go to the **Site Views** section in JCB. 2. Open the site edit view configuration. 3. Locate the **Main GET** dropdown in the site view. 4. Select the dynamic GET you just created. 5. Save your changes. **How This Works:** - When the site view is accessed (e.g., in edit mode), JCB will automatically call the linked dynamic GET. - The `$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 the `roomid` field. - `$data->moderatorid` will populate the `moderatorid` field. Make sure the fields in your view match the keys in the `$data` object. --- ### Why Use This Approach? - **Customizable Logic:** Writing custom logic in the dynamic GET allows you full control over the data. - **Scalability:** As your requirements grow, you can extend this dynamic GET or create additional GETs for other use cases. - **JCB Best Practices:** By linking the dynamic GET to the site view via the **Main GET**, you ensure your solution is clean, maintainable, and fully compatible with JCB’s framework. --- ### Final Workflow Summary 1. **Create a Dynamic GET** in JCB. 2. **Write Custom PHP** to populate `$data` with your desired dynamic values. 3. **Link the Dynamic GET** to the site view as the **Main GET**. 4. Test the site view to ensure the dynamic values are populated correctly. Let me know if you have further questions or need additional clarification!
Owner

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:

  1. 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.

  2. 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.

    • Under the "PHP" tab in the admin view, there’s an option called "Add PHP Get Form Method".
    • Enabling this option opens a section where you can add custom code to manipulate your form dynamically as it is being built.
    • You'll have access to the Joomla form object, enabling you to adjust the form fields and values programmatically.
  3. 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:

  1. Create a custom site view.
  2. Implement the required form logic using custom code, leveraging Joomla’s native form behavior where possible.
  3. Use dynamic methods to build the form, similar to how custom admin views function.

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: Simpler to implement, with much of the behavior relying on Joomla's dynamic infrastructure. You can manage fields and forms with minimal custom code.
  • Custom Views: Require more advanced coding and are suited for developers who need fine-grained control over behavior.

Admin views also allow moving dynamic behavior to custom field types. For example, you can:

  • Use custom field types to write PHP code for dynamically updating field values.
  • Utilize the PHP Get Form customization area to set or manipulate values programmatically based on conditions like user groups.

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!

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: 1. **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. 2. **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. - Under the **"PHP"** tab in the admin view, there’s an option called **"Add PHP Get Form Method"**. - Enabling this option opens a section where you can add custom code to manipulate your form dynamically as it is being built. - You'll have access to the `Joomla` form object, enabling you to adjust the form fields and values programmatically. 3. **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: 1. Create a custom site view. 2. Implement the required form logic using custom code, leveraging Joomla’s native form behavior where possible. 3. Use dynamic methods to build the form, similar to how custom admin views function. **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:** Simpler to implement, with much of the behavior relying on Joomla's dynamic infrastructure. You can manage fields and forms with minimal custom code. - **Custom Views:** Require more advanced coding and are suited for developers who need fine-grained control over behavior. Admin views also allow moving dynamic behavior to custom field types. For example, you can: - Use **custom field types** to write PHP code for dynamically updating field values. - Utilize the **PHP Get Form** customization area to set or manipulate values programmatically based on conditions like user groups. **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!
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: joomla/Component-Builder#1192
No description provided.