Commit a9cc9fc4 authored by JonBob's avatar JonBob

Split field.module up into separate field-type modules.

parent 22b02ebf
......@@ -6,10 +6,14 @@ This is still in development. Do not use on production sites.
To test, add cck.mysql to your database. Place the cck folder
into your modules directory. Go to administer -> modules and enable
the content and field modules (optionally, list.module).
the content module and one or more field type modules:
- text.module
- number.module
- date.module
Now go to administer -> content -> content types. Create a new
content type and edit it to add some fields. Then test by
creating a new node of your new type.
jvandyk [at] iastate.edu
jchaffer [at] structureinteractive.com
\ No newline at end of file
# content.module
CREATE TABLE node_type (
type_name varchar(31) NOT NULL default '',
......@@ -27,42 +28,48 @@ CREATE TABLE node_field_instance (
) TYPE=MyISAM;
# field data
# text.module
CREATE TABLE `node_field_float_data` (
CREATE TABLE `node_field_longtext_data` (
`vid` int unsigned NOT NULL default '0',
`field_name` varchar(31) NOT NULL default '',
`delta` int unsigned NOT NULL default '0',
`field_float` float NOT NULL default '0',
`field_longtext` mediumtext NOT NULL,
`format` int NOT NULL default '0',
PRIMARY KEY (`vid`,`field_name`,`delta`)
) TYPE=MyISAM;
CREATE TABLE `node_field_int_data` (
CREATE TABLE `node_field_shorttext_data` (
`vid` int unsigned NOT NULL default '0',
`field_name` varchar(31) NOT NULL default '',
`delta` int unsigned NOT NULL default '0',
`field_int` int NOT NULL default '0',
`field_shorttext` varchar(255) NOT NULL default '',
`format` int NOT NULL default '0',
PRIMARY KEY (`vid`,`field_name`,`delta`)
) TYPE=MyISAM;
CREATE TABLE `node_field_longtext_data` (
# number.module
CREATE TABLE `node_field_float_data` (
`vid` int unsigned NOT NULL default '0',
`field_name` varchar(31) NOT NULL default '',
`delta` int unsigned NOT NULL default '0',
`field_longtext` mediumtext NOT NULL,
`format` int NOT NULL default '0',
`field_float` float NOT NULL default '0',
PRIMARY KEY (`vid`,`field_name`,`delta`)
) TYPE=MyISAM;
CREATE TABLE `node_field_shorttext_data` (
CREATE TABLE `node_field_int_data` (
`vid` int unsigned NOT NULL default '0',
`field_name` varchar(31) NOT NULL default '',
`delta` int unsigned NOT NULL default '0',
`field_shorttext` varchar(255) NOT NULL default '',
`format` int NOT NULL default '0',
`field_int` int NOT NULL default '0',
PRIMARY KEY (`vid`,`field_name`,`delta`)
) TYPE=MyISAM;
# date.module
CREATE TABLE `node_field_date_data` (
`vid` int unsigned NOT NULL default '0',
`field_name` varchar(31) NOT NULL default '',
......
......@@ -482,16 +482,20 @@ function _content_admin_field_add($type_name) {
}
if ($options) {
$form = array();
$form['field_name'] = array(
$form['existing'] = array(
'#type' => 'fieldset',
'#title' => t('Add existing field'),
);
$form['existing']['field_name'] = array(
'#type' => 'select',
'#required' => TRUE,
'#options' => $options,
);
$form['submit'] = array(
$form['existing']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add field'),
);
$form['type_name'] = array(
$form['existing']['type_name'] = array(
'#type' => 'value',
'#value' => $type_name,
);
......@@ -499,7 +503,11 @@ function _content_admin_field_add($type_name) {
}
$form = array();
$form['label'] = array(
$form['new'] = array(
'#type' => 'fieldset',
'#title' => t('Create new field'),
);
$form['new']['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => '',
......@@ -510,16 +518,17 @@ function _content_admin_field_add($type_name) {
foreach ($field_types as $field_name => $field_type) {
$options[$field_name] = $field_type['label'];
}
$form['type'] = array(
'#type' => 'select',
$form['new']['type'] = array(
'#type' => 'radios',
'#title' => t('Field type'),
'#required' => TRUE,
'#options' => $options,
);
$form['submit'] = array(
$form['new']['submit'] = array(
'#type' => 'submit',
'#value' => t('Create field'),
);
$form['type_name'] = array(
$form['new']['type_name'] = array(
'#type' => 'value',
'#value' => $type_name,
);
......
<?php
// $Id$
/**
* @file
* Defines standard simple field types.
*/
/**
* Implementation of hook_help().
*/
function date_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Defines a date/time field type.');
}
}
/**
* Implementation of hook_field_info().
*/
function date_field_info() {
return array(
'date' => array('label' => 'Date'),
);
}
/**
* Implementation of hook_field_settings().
*/
function date_field_settings($op, $field, $scope) {
switch ($op) {
case 'form':
$form = array();
switch ($scope) {
case 'instance':
$options = array(
0 => t('Times are entered and displayed with site\'s time zone'),
1 => t('Times are entered and displayed with user\'s time zone'),
);
$form['time_zone'] = array(
'#type' => 'radios',
'#title' => t('Time zone handling'),
'#default_value' => $field['time_zone'] ? $field['time_zone'] : 0,
'#options' => $options,
);
break;
case 'global':
$options = array(
0 => t('Year'),
1 => t('Year and month'),
2 => t('Date'),
3 => t('Date and time'),
4 => t('Time only'),
);
$form['granularity'] = array(
'#type' => 'radios',
'#title' => t('Granularity'),
'#default_value' => $field['granularity'] ? $field['granularity'] : 0,
'#options' => $options,
);
break;
}
return $form;
case 'validate':
break;
case 'save':
switch ($scope) {
case 'instance':
return array('time_zone');
case 'global':
return array('granularity');
}
break;
}
}
/**
* Implementation of hook_field().
*/
function date_field($op, $node, $field, $a2, $a3, $a4) {
switch ($op) {
case 'load':
$result = db_query("SELECT field_date FROM {node_field_date_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
if ($field['multiple']) {
$values = array();
while ($value = db_fetch_object($result)) {
$values[] = array('value' => $value->field_date);
}
$additions = array($field['field_name'] => $values);
}
else {
$value = db_fetch_object($result);
$additions = array($field['field_name'] => array('value' => $value->field_date));
}
return $additions;
case 'view':
$output = '';
$node_field = $node->$field['field_name'];
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = check_plain($item['value']);
}
}
else {
$node_field['view'] = check_plain($node_field['value']);
}
$node->$field['field_name'] = $node_field;
if ($field['multiple']) {
$output = '';
foreach ($node_field as $delta => $item) {
$output .= '<div class="'. $field['field_name'] .'">'. $item['view'] .'</div>';
}
return $output;
}
else {
return '<div class="'. $field['field_name'] .'">'. $node_field['view'] .'</div>';
}
case 'form':
$form = array();
$node_field = $node->$field['field_name'];
$form[$field['field_name']] = array('#tree' => TRUE);
if ($field['multiple']) {
foreach (range(0, 2) as $delta) {
$form[$field['field_name']][$delta]['value'] = array(
'#type' => 'textfield',
'#title' => t($field['label']),
'#default_value' => $node_field[$delta]['value'],
'#required' => ($delta == 0) ? $field['required'] : FALSE,
);
}
}
else {
$form[$field['field_name']]['value'] = array(
'#type' => 'textfield',
'#title' => t($field['label']),
'#default_value' => $node_field['value'],
'#required' => $field['required'],
);
}
return $form;
case 'validate':
$node_field = $node->$field['field_name'];
if ($field['multiple']) {
if (is_array($node_field)) {
foreach ($node_field as $delta => $item) {
if ($item['value'] != '') {
if (preg_match('/[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}/', $item['value'])) {
form_set_error($field['field_name'] .']['. $delta .'][value', t('%name must be entered in ISO 8601 format (YYYYMMDDThh:mm:ss).', array('%name' => t($field['label']))));
}
}
}
}
}
else {
if (isset($node_field['value'])) {
if ($node_field['value'] != '') {
if (preg_match('/[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}/', $node_field['value'])) {
form_set_error($field['field_name'] .'][value', t('%name must be entered in ISO 8601 format (YYYYMMDDThh:mm:ss).', array('%name' => t($field['label']))));
}
}
}
}
return;
case 'insert':
$node_field = $node->$field['field_name'];
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
db_query("INSERT INTO {node_field_date_data} (vid, field_name, delta, field_date) VALUES (%d, '%s', %d, '%s')", $node->vid, $field['field_name'], $delta, $item['value']);
}
}
else {
db_query("INSERT INTO {node_field_date_data} (vid, field_name, field_date) VALUES (%d, '%s', '%s')", $node->vid, $field['field_name'], $node_field['value']);
}
return;
case 'update':
$node_field = $node->$field['field_name'];
// Delete and insert, rather than update, in case a field was added.
db_query("DELETE FROM {node_field_date_data} WHERE vid = %d AND field_name = '%s'", $node->vid, $field['field_name']);
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
db_query("INSERT INTO {node_field_date_data} (vid, field_name, delta, field_date) VALUES (%d, '%s', %d, '%s')", $node->vid, $field['field_name'], $delta, $item['value']);
}
}
else {
db_query("INSERT INTO {node_field_date_data} (vid, field_name, field_date) VALUES (%d, '%s', '%s')", $node->vid, $field['field_name'], $node_field['value']);
}
return;
case 'delete':
db_query("DELETE FROM {node_field_date_data} WHERE vid = %d AND field_name = '%s'", $node->vid, $field['field_name']);
return;
}
}
/**
* Implementation of hook_field_query().
*/
function date_field_query($field, $op, $value) {
$query_additions = array();
switch ($op) {
case 'is':
$query_additions['tables'] = array('%table' => 'node_field_date_data');
$query_additions['where'] = "%table.field_date = '%s'";
$query_additions['data'] = array($value);
break;
case 'after':
$query_additions['tables'] = array('%table' => 'node_field_date_data');
$query_additions['where'] = "%table.field_date > '%s'";
$query_additions['data'] = array($value);
break;
case 'before':
$query_additions['tables'] = array('%table' => 'node_field_date_data');
$query_additions['where'] = "%table.field_date < '%s'";
$query_additions['data'] = array($value);
break;
}
return $query_additions;
}
?>
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Defines standard simple field types.
*/
/**
* Implementation of hook_help().
*/
function field_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Defines standard simple field types.');
}
}
/**
* Implementation of hook_field_info().
*/
function field_field_info() {
return array(
'field_shorttext' => array('label' => 'Short Text'),
'field_longtext' => array('label' => 'Long Text'),
'field_integer' => array('label' => 'Integer'),
'field_decimal' => array('label' => 'Decimal'),
'field_date' => array('label' => 'Date'),
);
}
/**
* Implementation of hook_field_settings().
*/
function field_field_settings($op, $field, $scope) {
switch ($op) {
case 'form':
$form = array();
switch ($field['type']) {
case 'field_shorttext':
case 'field_longtext':
switch ($scope) {
case 'instance':
$form['rows'] = array(
'#type' => 'textfield',
'#title' => t('Rows'),
'#default_value' => $field['rows'] ? $field['rows'] : 1,
'#required' => TRUE,
);
break;
case 'global':
$options = array(0 => t('Plain text'), 1 => t('Filtered text (user selects input format)'));
$form['text_processing'] = array(
'#type' => 'radios',
'#title' => t('Text processing'),
'#default_value' => $field['text_processing'] ? $field['text_processing'] : 0,
'#options' => $options,
);
break;
}
break;
case 'field_integer':
case 'field_decimal':
switch ($scope) {
case 'global':
$form['min'] = array(
'#type' => 'textfield',
'#title' => t('Minimum'),
'#default_value' => $field['min'] ? $field['min'] : '',
);
$form['max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum'),
'#default_value' => $field['min'] ? $field['min'] : '',
);
break;
}
break;
case 'field_date':
switch ($scope) {
case 'instance':
$options = array(
0 => t('Times are entered and displayed with site\'s time zone'),
1 => t('Times are entered and displayed with user\'s time zone'),
);
$form['time_zone'] = array(
'#type' => 'radios',
'#title' => t('Time zone handling'),
'#default_value' => $field['time_zone'] ? $field['time_zone'] : 0,
'#options' => $options,
);
break;
case 'global':
$options = array(
0 => t('Year'),
1 => t('Year and month'),
2 => t('Date'),
3 => t('Date and time'),
4 => t('Time only'),
);
$form['granularity'] = array(
'#type' => 'radios',
'#title' => t('Granularity'),
'#default_value' => $field['granularity'] ? $field['granularity'] : 0,
'#options' => $options,
);
break;
}
break;
}
return $form;
case 'validate':
switch ($field['type']) {
case 'field_shorttext':
case 'field_longtext':
switch ($scope) {
case 'instance':
if (!is_numeric($field['rows']) || intval($field['rows']) != $field['rows'] || $field['rows'] <= 0) {
form_set_error('rows', t('"Rows" must be a positive integer.'));
}
break;
}
break;
case 'field_integer':
case 'field_decimal':
switch ($scope) {
case 'global':
if ($field['min'] && !is_numeric($field['min'])) {
form_set_error('rows', t('"Minimum" must be a number.'));
}
if ($field['max'] && !is_numeric($field['max'])) {
form_set_error('rows', t('"Maximum" must be a number.'));
}
break;
}
break;
case 'field_date':
break;
}
break;
case 'save':
switch ($field['type']) {
case 'field_shorttext':
case 'field_longtext':
switch ($scope) {
case 'instance':
return array('rows');
case 'global':
return array('text_processing');
}
break;
case 'field_integer':
case 'field_decimal':
switch ($scope) {
case 'instance':
return array();
case 'global':
return array('min', 'max');
}
break;
case 'field_date':
switch ($scope) {
case 'instance':
return array('time_zone');
case 'global':
return array('granularity');
}
break;
}
break;
}
}
/**
* Implementation of hook_field().
*/
function field_field($op, $node, $field, $a2, $a3, $a4) {
switch ($op) {
case 'load':
switch($field['type']) {
case 'field_shorttext':
$result = db_query("SELECT field_shorttext, format FROM {node_field_shorttext_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
if ($field['multiple']) {
$values = array();
while ($value = db_fetch_object($result)) {
$values[] = array('value' => $value->field_shorttext, 'format' => $value->format);
}
$additions = array($field['field_name'] => $values);
}
else {
$value = db_fetch_object($result);
$additions = array($field['field_name'] => array('value' => $value->field_shorttext, 'format' => $value->format));
}
break;
case 'field_longtext':
$result = db_query("SELECT field_longtext, format FROM {node_field_longtext_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
if ($field['multiple']) {
$values = array();
while ($value = db_fetch_object($result)) {
$values[] = array('value' => $value->field_longtext, 'format' => $value->format);
}
$additions = array($field['field_name'] => $values);
}
else {
$value = db_fetch_object($result);
$additions = array($field['field_name'] => array('value' => $value->field_longtext, 'format' => $value->format));
}
break;
case 'field_integer':
$result = db_query("SELECT field_int FROM {node_field_int_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
if ($field['multiple']) {
$values = array();
while ($value = db_fetch_object($result)) {
$values[] = array('value' => $value->field_int);
}
$additions = array($field['field_name'] => $values);
}
else {
$value = db_fetch_object($result);
$additions = array($field['field_name'] => array('value' => $value->field_int));
}
break;
case 'field_decimal':
$result = db_query("SELECT field_float FROM {node_field_float_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
if ($field['multiple']) {
$values = array();
while ($value = db_fetch_object($result)) {
$values[] = array('value' => $value->field_int);
}
$additions = array($field['field_name'] => $values);
}
else {
$value = db_fetch_object($result);
$additions = array($field['field_name'] => array('value' => $value->field_float));
}
break;
case 'field_date':
$result = db_query("SELECT field_date FROM {node_field_date_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
if ($field['multiple']) {
$values = array();
while ($value = db_fetch_object($result)) {
$values[] = array('value' => $value->field_date);
}
$additions = array($field['field_name'] => $values);
}
else {
$value = db_fetch_object($result);
$additions = array($field['field_name'] => array('value' => $value->field_date));
}
break;
}
return $additions;
case 'view':
$output = '';
$node_field = $node->$field['field_name'];
switch($field['type']) {
case 'field_shorttext':
case 'field_longtext':
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
if ($field['text_processing']) {
$node_field[$delta]['view'] = check_markup($item['value'], $item['format'], isset($node->in_preview) ? $node->in_preview : FALSE);
}
else {
$node_field[$delta]['view'] = check_plain($item['value']);
}
}
}
else {
if ($field['text_processing']) {
$node_field['view'] = check_markup($node_field['value'], $node_field['format'], isset($node->in_preview) ? $node->in_preview : FALSE);
}
else {
$node_field['view'] = check_plain($node_field['value']);
}
}
break;
case 'field_integer':
case 'field_decimal':
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = check_plain($item['value']);
}
}
else {
$node_field['view'] = check_plain($node_field['value']);
}
break;
case 'field_date':
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = check_plain($item['value']);
}
}
else {
$node_field['view'] = check_plain($node_field['value']);
}
break;
}
$node->$field['field_name'] = $node_field;
if ($field['multiple']) {
$output = '';
foreach ($node_field as $delta => $item) {
$output .= '<div class="'. $field['field_name'] .'">'. $item['view'] .'</div>';
}
return $output;
}
else {
return '<div class="'. $field['field_name'] .'">'. $node_field['view'] .'</div>';
}
case 'form':
$form = array();
$node_field = $node->$field['field_name'];
$form[$field['field_name']] = array('#tree' => TRUE);
switch($field['type']) {
case 'field_shorttext':
case 'field_longtext':
if ($field['multiple']) {
foreach (range(0, 2) as $delta) {
if ($field['rows'] == 1) {
$form[$field['field_name']][$delta]['value'] = array(
'#type' => 'textfield',
'#title' => t($field['label']),
'#default_value' => $node_field[$delta]['value'],
'#required' => ($delta == 0) ? $field['required'] : FALSE,
);
}
else {
$form[$field['field_name']][$delta]['value'] = array(
'#type' => 'textarea',
'#title' => t($field['label']),
'#default_value' => $node_field[$delta]['value'],
'#required' => ($delta == 0) ? $field['required'] : FALSE,
'#rows' => $field['rows'],
);
}
if ($field['text_processing']) {
$form[$field['field_name']][$delta] = array_merge($form[$field['field_name']][$delta], filter_form($node_field[$delta]['format']));
}
}
}
else {
if ($field['rows'] == 1) {
$form[$field['field_name']]['value'] = array(
'#type' => 'textfield',
'#title' => t($field['label']),
'#default_value' => $node_field['value'],
'#required' => $field['required'],
);
}
else {
$form[$field['field_name']]['value'] = array(
'#type' => 'textarea',
'#title' => t($field['label']),
'#default_value' => $node_field['value'],
'#required' => $field['required'],
'#rows' => $field['rows'],
);
}
if ($field['text_processing']) {
$form[$field['field_name']] = array_merge($form[$field['field_name']], filter_form($node_field['format']));
}
}
break;