Recently the need to explore the topic of applying user specific fields to a Sugar 7 view made its way to my desk. With past versions of Sugar, this type of customization could be implemented by creating the user specific metadata definitions, a custom view with the logic that drove the selection of the metadata and some logic that flushed out the cache so as to force the new metadata to be displayed upon the view being rendered. Given my history with the topic, I was curious to see how different the process would be in Sugar 7.
For those of you that are new to Sugar 7, it is worth noting that the metadata for a given view is stored in a JSON object accessible through the namespace this.meta. Said object can be easily manipulated through the controller relating to that view, thus we can leverage this.meta to adjust the metadata for a given view.
In our example we will assume we have two different users that should be viewing different fields on the RecordView. User 1 is the default admin user, identified by the ID value of "1" within the database, and User 2 is the user Jim, identified by the ID value of "seed_jim_id" within the database. Our goal will be to modify the default RecordView on the Contacts module such that User 1 automatically receives the default set of fields upon accessing the RecordView, but User 2 receives a modified version of that view, i.e. a different set of fields, with some overlap.
How do we go about applying this customization?
To accomplish this task we need to do the following:
1. Create the modified version of the metadata applicable to User 2
2. Create a custom view based off the modified metadata
3. Create a custom controller for the default RecordView to introduce our logic
Completing the first step should be pretty straightforward, even if your experience with Sugar 7 development is limited. For this example I merely took the standard record.php metadata found in ./modules/Contacts/clients/base/views/record/, copied it to ./custom/modules/Contacts/clients/base/views/group1/ and applied the following modifications:
1. Changed the file name from record.php to group1.php
2. Changed $viewdefs['Contacts']['base']['view']['record'] to $viewdefs['Contacts']['base']['view']['group1']
Once the above changes had been applied, I proceeded to manually apply my desired metadata changes to group1.php. That included the removal and addition of fields, as well as modifications to their positioning.
Changing the file name to group1.php, along with the folder name and the change to the array key from record to group1 effectively addresses our second requirement. Thus, we have already defined our new view named group1 by following the steps illustrated above.
Lastly, we need to create a custom controller for the RecordView and apply our change. To do this, place the following file into ./custom/modules/Contacts/clients/base/views/record/
...and finally, apply the change by purging the contents of the ./cache folder and reloading Sugar in your browser.
Side note: Given that this.meta represents a JSON object of the metadata for the view, it is possible to simplify this further to only affect the specific elements of the JSON object that need manipulation. For this scenario, the use of the full metadata file is used only for the purposes of simplifying the example.