Data model

Events happening in the community are now at Drupal community events on www.drupal.org.
You are viewing a wiki page. You are welcome to join the group and then edit it. Be bold!

CCK in Core

-- (from adrian rossouw in development mailing list)

Second phase : data model.

As I specified in my data model presentation at the last DrupalCon, we have a data model currently, it's just hidden in the form array structure.

We use an incredible amount of complex logic to try and determine the data model from the form array.

Just about all the complexity in form_builder comes from this process.

CCK does this the right way around, it creates a data model (by defining the fields) and then converts the data model into a form / display. For consistency, we should then look at making all forms use the same mechanism.

This will greatly simplify the amount of twiddling we need to do in form_builder.

The form / view is actually a superstructure built on top of the data model, with added display / form specific properties added.

In my example, they took the form of a data model function, which defines only the fields:

function model_objecttype() {
// these constructor functions return arrays with all the default values for that type already populated.
// this improves cacheability, in that all the array merging is done in the first step, not recursively called on everything.
// it also lessens the amount of code needed, and imo improves readability.

$model['id'] = drupal_field('id'); // defaults to edit_widget => hidden
$model['title'] = drupal_field('title'); // Additional default field lengths , and defaults to a textfield.
$model['body'] = drupal_field('body');
return $model;
}

This can then be turned into a view / form :

function form_objecttype($model) {
$form['id'] = drupal_widget($model['id']);
$form['fieldset'] = drupal_widget('fieldset', array('title' => 'something') );
$form['fieldset']['title'] = drupal_widget($model['title'], array ('widget' => 'textarea') );
$form['fieldset']['body'] = drupal_widget($model['body'], array ('widget' => 'textfield'));

return $form;
}

By just automatically recurring through the model data structure, we can change it into a form without even needing to have a separate function (the same way we can do drupal_render without needing to specify a theme function).

Other things that will get added into the display, is stuff like tables.

At this point, we have got the Data API as i mentioned created. After this we can look at making the CRUD functions (which imo shouild be standardised into save_objecttype, create_objecttype, delete_objecttype, load_objecttype) work with a query builder, as it does for cck.

Comments

2 thoughts on models

ray007's picture

Having a data model and building the form from there sounds good to me.
I'd like to add 2 points to think about:

1.) A content-type uses a model, so we should be able to have two or more contenttypes pointing to the same data-model and only specifying different publishing options (and other parameters). Changing the model then affects all contenttypes using the model.
2.) It would be really cool if we had models with (single) inheritance, so one model can extend another and add some datafields (and maybe modify some?). If we look at what modules like 'event' and 'location' do that almost may look like multiple inheritance then ...

do I make sense?

--
best regards

    Ray

If you need a drupal developer contact me!

--
best regards

    Ray

If you need a drupal developer contact me!

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week