CCK: Defines simple text field types. Note: Requires content.module.'); } } /** * Implementation of hook_field_info(). */ function text_field_info() { return array( 'text' => array('label' => 'Text'), ); } /** * Implementation of hook_field_settings(). */ function text_field_settings($op, $field) { switch ($op) { case 'form': $form = array(); $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' => isset($field['text_processing']) ? $field['text_processing'] : 0, '#options' => $options, ); $form['max_length'] = array( '#type' => 'textfield', '#title' => t('Maximum length'), '#default_value' => isset($field['max_length']) ? $field['max_length'] : '', '#required' => FALSE, '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'), ); $form['allowed_values'] = array( '#type' => 'textarea', '#title' => t('Allowed values'), '#default_value' => isset($field['allowed_values']) ? $field['allowed_values'] : '', '#required' => FALSE, '#rows' => 10, '#description' => t('The possible values this field can contain. Any other values will result in an error. Enter one value per line.'), ); return $form; case 'save': return array('text_processing', 'max_length', 'allowed_values'); case 'database columns': $columns = array( 'value' => array('type' => 'varchar', 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), 'format' => array('type' => 'int', 'length' => 10, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), ); if ($field['max_length'] == 0 || $field['max_length'] > 255) { $columns['value']['type'] = 'longtext'; } else { $columns['value']['length'] = $field['max_length']; } if ($field['text_processing'] == 0) { unset($columns['format']); } return $columns; case 'filters': $allowed_values = explode("\n", $field['allowed_values']); $allowed_values = array_map('trim', $allowed_values); $allowed_values = array_filter($allowed_values, 'strlen'); if (count($allowed_values)) { return array( 'default' => array( 'list' => drupal_map_assoc($allowed_values), 'list-type' => 'list', 'operator' => 'views_handler_operator_or', 'value-type' => 'array', ), ); } else { return array( 'like' => array( 'operator' => 'views_handler_operator_like', 'handler' => 'views_handler_filter_like', ), ); } break; } } /** * Implementation of hook_field(). */ function text_field($op, &$node, $field, &$items, $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); case 'validate': $allowed_values = explode("\n", $field['allowed_values']); $allowed_values = array_map('trim', $allowed_values); $allowed_values = array_filter($allowed_values, 'strlen'); if (is_array($items)) { foreach ($items as $delta => $item) { $error_field = $field['field_name'].']['.$delta.'][value'; if ($item['value'] != '') { if (count($allowed_values) && !in_array($item['value'], $allowed_values)) { form_set_error($error_field, t('Illegal value for %name.', array('%name' => t($field['widget']['label'])))); } } } } break; } } /** * Implementation of hook_field_formatter_info(). */ function text_field_formatter_info() { return array( 'default' => array( 'label' => 'Default', 'field types' => array('text'), ), 'plain' => array( 'label' => 'Plain text', 'field types' => array('text'), ), 'trimmed' => array( 'label' => 'Trimmed', 'field types' => array('text'), ), ); } /** * Implementation of hook_field_formatter(). * * The $node argument is necessary so that filter access can be checked on * node preview. */ function text_field_formatter($field, $item, $formatter, $node) { if (!isset($item['value'])) { return ''; } if ($field['text_processing']) { $text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview)); } else { $text = check_plain($item['value']); } switch ($formatter) { case 'plain': return strip_tags($text); case 'trimmed': return node_teaser($text, $field['text_processing'] ? $item['format'] : NULL); default: return $text; } } /** * Implementation of hook_widget_info(). */ function text_widget_info() { return array( 'text' => array( 'label' => 'Text Field', 'field types' => array('text'), ), ); } /** * Implementation of hook_widget_settings(). */ function text_widget_settings($op, $widget) { switch ($op) { case 'form': $form = array(); $form['rows'] = array( '#type' => 'textfield', '#title' => t('Rows'), '#default_value' => $widget['rows'] ? $widget['rows'] : 1, '#required' => TRUE, ); return $form; case 'validate': if (!is_numeric($widget['rows']) || intval($widget['rows']) != $widget['rows'] || $widget['rows'] <= 0) { form_set_error('rows', t('"Rows" must be a positive integer.')); } break; case 'save': return array('rows'); } } /** * Implementation of hook_widget(). */ function text_widget($op, &$node, $field, &$items) { switch ($op) { case 'form': $form = array(); $form[$field['field_name']] = array('#tree' => TRUE); if ($field['multiple']) { $form[$field['field_name']]['#type'] = 'fieldset'; $form[$field['field_name']]['#title'] = t($field['widget']['label']); $form[$field['field_name']]['#description'] = $field['widget']['description']; $delta = 0; foreach ($items as $data) { if ($data['value']) { if ($field['widget']['rows'] == 1) { $form[$field['field_name']][$delta]['value'] = array( '#type' => 'textfield', '#title' => '', '#default_value' => $data['value'], '#required' => ($delta == 0) ? $field['required'] : FALSE, '#maxlength' => $field['max_length'] ? $field['max_length'] : '', '#weight' => $field['widget']['weight'], ); } else { $form[$field['field_name']][$delta]['value'] = array( '#type' => 'textarea', '#title' => '', '#default_value' => $data['value'], '#required' => ($delta == 0) ? $field['required'] : FALSE, '#rows' => $field['widget']['rows'], '#weight' => $field['widget']['weight'], ); } if ($field['text_processing']) { $form[$field['field_name']][$delta]['format'] = filter_form($data['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array($field['field_name'], $delta, 'format')); } $delta++; } } foreach (range($delta, $delta + 2) as $delta) { if ($field['widget']['rows'] == 1) { $form[$field['field_name']][$delta]['value'] = array( '#type' => 'textfield', '#title' => '', '#default_value' => '', '#required' => ($delta == 0) ? $field['required'] : FALSE, '#maxlength' => $field['max_length'] ? $field['max_length'] : '', '#weight' => $field['widget']['weight'], ); } else { $form[$field['field_name']][$delta]['value'] = array( '#type' => 'textarea', '#title' => '', '#default_value' => '', '#required' => ($delta == 0) ? $field['required'] : FALSE, '#rows' => $field['widget']['rows'], '#weight' => $field['widget']['weight'], ); } if ($field['text_processing']) { $form[$field['field_name']][$delta]['format'] = filter_form($items[$delta]['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array($field['field_name'], $delta, 'format')); } } } else { if ($field['widget']['rows'] == 1) { $form[$field['field_name']][0]['value'] = array( '#type' => 'textfield', '#title' => t($field['widget']['label']), '#default_value' => isset($items[0]['value']) ? $items[0]['value'] : '', '#required' => $field['required'], '#description' => $field['widget']['description'], '#maxlength' => $field['max_length'] ? $field['max_length'] : '', '#weight' => $field['widget']['weight'], ); } else { $form[$field['field_name']][0]['value'] = array( '#type' => 'textarea', '#title' => t($field['widget']['label']), '#default_value' => $items[0]['value'], '#required' => $field['required'], '#rows' => $field['widget']['rows'], '#description' => $field['widget']['description'], '#weight' => $field['widget']['weight'], ); } if ($field['text_processing']) { $form[$field['field_name']][0]['format'] = filter_form($items[0]['format'], $form[$field['field_name']][0]['value']['#weight'] + 1, array($field['field_name'], 0, 'format')); } } return $form; case 'validate': if ($field['max_length'] > 0) { foreach ($items as $delta => $data) { $error_field = $field['field_name'].']['.$delta.'][value'; if (strlen($data['value']) > $field['max_length']) { form_set_error($error_field, t('%label is longer than %max characters.', array('%label' => $field['widget']['label'],'%max' => $field['max_length']))); } } } break; case 'process form values': // Don't save empty fields. if ($field['multiple']) { foreach ($items as $delta => $item) { if ($item['value'] == '') { unset($items[$delta]); } } } break; } }