Custom widgets?
Archived from the Xataface Users forum.
gorzek — Tue Jul 15, 2008 7:47 am
I’ve been making good progress so far. I think, at this point, I need to figure out how to make custom widgets. One should be easy: a hidden “autouser” field. Basically, all it does is take the currently logged-in user and makes their user id populate a hidden “users_id” field. This is to automatically associate a user to the records they create without them having to select their own username from a select box. If there is an easier way to do this, I’d like to know.
In any case, though, what is the simplest way to make a custom widget, and how would you then use it? I looked at the examples in HTML/QuickForm, but none of them looked all that simple. Just a “bare bones” custom widget is what I’d like to see, if possible.
shannah — Tue Jul 15, 2008 9:31 am
Hi Gorzek,
First, the easy way to do what you want with the username is to either:
- Implement a beforeSave() trigger that sets this value.
or
- Implement a fieldname__default() method in your delegate class to provide a default value for the field.
Option 1:
- Code: Select all
function beforeInsert(&$record){ $record->setValue('ownerID', getUserID()); }
(Assuming you have defined a function called getUserID() that obtains the ID of the currently logged in user.
Option 2:
- Code: Select all
function ownerID__default(){ return getUserID(); }
Both examples you can use a hidden widget for.
On the larger scale, if you do want to create custom widgets (for any purpose whatsoever) there are two ways to do it.
- A one-off solution for your application. (easy)
- Create a reusable widget. (more complex)
For a one-off solution all you have to do is implement the fieldname_widget block for the field in question. E.g. to accomplish what you want to do with the username you could do:
- Code: Select all
function block__ownerID_widget(){ echo '<input type="hidden" value="'.getUserID().'">'; }
Creating a reusable widget is a little more complex. A simple widget can be created by simply extending HTML_QuickForm_element (see HTML/QuickForm for some custom widgets that have been created for Xataface or lib/HTML/QuickForm for examples of widgets that are distributed with HTML_QuickForm .
In order to keep the widgets decoupled from the Xataface framework (so that they can be used outside of Xataface) i have tried not to use any Xataface specific calls inside any widget definitions. However you can create a wrapper that Xataface uses to create the widget at runtime (see Dataface/FormTool for examples of these wrappers).
I hope to get some documentation on custom widgets out at some point, but for now it would be best to just look at the existing widgets and use them as a base.
Best regards
Steve
gorzek — Tue Jul 15, 2008 12:55 pm
I think that’s the information I was looking for. I should have thought of the events! I’m already using some of them to make associated records.
Now I understand why the widgets were difficult to parse: no coupling with Xataface! I will probably stick with one-off widgets, for now. That’s probably the least trouble.
I greatly appreciate all your help!