CCK: Defines a date/time field type. Note: Requires content.module.'); } } /** * 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) { switch ($op) { case 'form': $form = array(); $options = array( 0 => t('Year'), 1 => t('Year and month'), 2 => t('Date'), 3 => t('Date and time'), ); $form['granularity'] = array( '#type' => 'radios', '#title' => t('Granularity'), '#default_value' => $field['granularity'] ? $field['granularity'] : 0, '#options' => $options, ); return $form; case 'save': return array('granularity'); case 'database columns': $columns = array( 'value' => array('type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'default' => "'0001-01-01T00:00:00'", 'sortable' => TRUE), ); switch ($field['granularity']) { case 0: $columns['value']['length'] = 4; $columns['value']['default'] = "'0001'"; break; case 1: $columns['value']['length'] = 7; $columns['value']['default'] = "'0001-01'"; break; case 2: $columns['value']['length'] = 10; $columns['value']['default'] = "'0001-01-01'"; break; default: $columns['value']['length'] = 19; $columns['value']['default'] = "'0001-01-01T00:00:00'"; break; } return $columns; } } /** * Implementation of hook_field(). */ function date_field($op, $node, &$field, &$node_field, $teaser, $page) { switch ($op) { case 'view': foreach ($items as $delta => $item) { $items[$delta]['view'] = content_format($field, $item, 'default', $node); } return theme('field', $node, $field, $items, $teaser, $page); } } /** * Implementation of hook_field_formatter_info(). */ function date_field_formatter_info() { return array( 'default' => array( 'label' => 'Default', 'field types' => array('date'), ), ); } /** * Implementation of hook_field_formatter(). */ function date_field_formatter($field, $item, $formatter, $node) { $year = substr($item['value'], 2, 2); $month = substr($item['value'], 5, 2); $day = substr($item['value'], 8, 2); if ($item['value'] != '0001-01-01T00:00:00' && intval($month) != 0 && intval($day) != 0) { return ''. intval($month) .'/'. intval($day) .'/'. $year .''; } else { return ''; } } /** * Implementation of hook_widget_info(). */ function date_widget_info() { return array( 'date_text' => array( 'label' => 'Text Field', 'field types' => array('date'), ), ); } /** * Implementation of hook_widget_settings(). */ function date_widget_settings($op, $widget) { switch ($op) { case 'form': $form = array(); $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' => $widget['time_zone'] ? $widget['time_zone'] : 0, '#options' => $options, ); return $form; case 'save': return array('time_zone'); } } /** * Implementation of hook_widget(). */ function date_widget($op, &$node, $field, &$node_field) { switch ($op) { case 'form': $form = array(); $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['widget']['label']), '#default_value' => $node_field[$delta]['value'], '#required' => ($delta == 0) ? $field['required'] : FALSE, ); } } else { $form[$field['field_name']][0]['value'] = array( '#type' => 'textfield', '#title' => t($field['widget']['label']), '#default_value' => $node_field[0]['value'], '#required' => $field['required'], ); } return $form; case 'validate': if (is_array($node_field)) { foreach ($node_field as $delta => $item) { if ($item['value'] != '') { switch ($field['granularity']) { case 0: // Year if (!preg_match('/^[0-9]{4}$/', $item['value'])) { form_set_error($field['field_name'] .']['. $delta .'][value', t('%name must be entered in ISO 8601 format (YYYY).', array('%name' => t($field['widget']['label'])))); } break; case 1: // Year and month if (!preg_match('/^[0-9]{4}-[0-9]{2}$/', $item['value'])) { form_set_error($field['field_name'] .']['. $delta .'][value', t('%name must be entered in ISO 8601 format (YYYY-MM).', array('%name' => t($field['widget']['label'])))); } break; case 2: // Date if (!preg_match('/^[0-9]{4}-[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 (YYYY-MM-DD).', array('%name' => t($field['widget']['label'])))); } break; case 3: // Date and time if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}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 (YYYY-MM-DDThh:mm:ss).', array('%name' => t($field['widget']['label'])))); } break; } } } } return; } } /** * Implementation of hook_views_arguments(). * * The date module defines custom arguments for more granular date filtering. */ function date_views_arguments() { $field_types = _content_field_types(); $arguments = array(); foreach (content_fields() as $field) { $db_info = content_database_info($field); switch ($field['type']) { case 'date': $main_column = reset($db_info['columns']); $argument = array(); $argument['name'] = 'Date (by year): '. $field['widget']['label'] .' ('. $field['field_name'] .')'; $argument['handler'] = 'date_views_argument_handler'; $arguments['date-year: '. $field['field_name']] = $argument; $argument = array(); $argument['name'] = 'Date (by month): '. $field['widget']['label'] .' ('. $field['field_name'] .')'; $argument['handler'] = 'date_views_argument_handler'; $arguments['date-month: '. $field['field_name']] = $argument; $argument = array(); $argument['name'] = 'Date (by day): '. $field['widget']['label'] .' ('. $field['field_name'] .')'; $argument['handler'] = 'date_views_argument_handler'; $arguments['date-day: '. $field['field_name']] = $argument; break; } } return $arguments; } /** * Perform filtering by an argument for field data stored via content.module. */ function date_views_argument_handler($op, &$query, $argtype, $arg = '') { if ($op == 'filter') { $type = $argtype['type']; } else { $type = $argtype; } if (substr($type, 0, 9) == 'date-year') { $field_name = substr($type, 11); $granularity = 'year'; } if (substr($type, 0, 10) == 'date-month') { $field_name = substr($type, 12); $granularity = 'month'; } if (substr($type, 0, 8) == 'date-day') { $field_name = substr($type, 10); $granularity = 'day'; } $field = content_fields($field_name); $db_info = content_database_info($field); $main_column = reset($db_info['columns']); // The table name used here is the Views alias for the table, not the actual // table name. $table = 'node_data_'. $field['field_name']; switch ($op) { case 'summary': $query->ensure_table($table); $query->add_field($main_column['column'], $table); switch ($granularity) { case 'year': return array( 'field' => 'SUBSTRING('. $table .'.'. $main_column['column'] .', 1, 4)', 'fieldname' => $main_column['column'] .'_year', ); case 'month': return array( 'field' => 'SUBSTRING('. $table .'.'. $main_column['column'] .', 1, 7)', 'fieldname' => $main_column['column'] .'_month', ); case 'day': return array( 'field' => 'SUBSTRING('. $table .'.'. $main_column['column'] .', 1, 10)', 'fieldname' => $main_column['column'] .'_day', ); } break; case 'sort': break; case 'filter': $query->ensure_table($table); $query->add_where($table .'.'. $main_column['column'] .' LIKE \'%s%%\'', $arg); break; case 'link': $view_column_name = $main_column['column'] .'_'. $granularity; $time = $query->$view_column_name; return l($time, $arg .'/'. $time, array(), NULL, NULL, FALSE, TRUE); case 'title': $item = array(key($db_info['columns']) => $query); return date_field_view_item($field, $item); } }