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
\ No newline at end of file
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
This diff is collapsed.
......@@ -26,9 +26,6 @@ function list_menu($may_cache) {
$items[] = array('path' => 'list', 'title' => t('list'),
'callback' => 'list_page', 'access' => user_access('access content'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'list/titles', 'title' => t('list'),
'callback' => 'list_page', 'access' => user_access('access content'),
'type' => MENU_CALLBACK);
}
return $items;
......@@ -42,11 +39,6 @@ function list_page() {
$arguments = explode('/', $_GET['q']);
array_shift($arguments);
if ($arguments[0] == 'titles') {
$titles_only = TRUE;
array_shift($arguments);
}
$conditions = array();
$sort = array();
......@@ -59,22 +51,14 @@ function list_page() {
$conditions[] = array('field' => $field, 'op' => $op, 'value' => $value);
}
if ($titles_only) {
$header = array('title');
$result = content_node_query($conditions, $sort, 'n.nid, n.title');
while ($node = db_fetch_object($result)) {
$rows[] = l($node->title, drupal_get_path_alias("node/$node->nid'"));
}
$output = theme('node_list', $rows);
}
else {
$result = content_node_query($conditions, $sort);
while ($node = db_fetch_array($result)) {
$node = node_load($node);
$output .= node_view($node);
}
$result = content_node_query($conditions, $sort);
while ($node = db_fetch_object($result)) {
$node = node_load($node->nid);
$output .= node_view($node);
}
print theme('page', $output);
return $output;
}
?>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment