You are here

text.module in Content Construction Kit (CCK) 5

Defines simple text field types.

File

text.module
View source
<?php

/**
 * @file
 * Defines simple text field types.
 */

/**
 * Implementation of hook_field_info().
 */
function text_field_info() {
  return array(
    'text' => array(
      'label' => t('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 list'),
        '#default_value' => !empty($field['allowed_values']) ? $field['allowed_values'] : '',
        '#required' => FALSE,
        '#rows' => 10,
        '#description' => t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database, and it must match the field storage type (%type). The label is optional, and the key will be used as the label if no label is specified.<br />Allowed HTML tags: @tags', array(
          '%type' => $field['type'],
          '@tags' => _content_filter_xss_display_allowed_tags(),
        )),
      );
      $form['advanced_options'] = array(
        '#type' => 'fieldset',
        '#title' => t('PHP code'),
        '#collapsible' => TRUE,
        '#collapsed' => empty($field['allowed_values_php']),
      );
      if (user_access('Use PHP input for field settings (dangerous - grant with care)')) {
        $form['advanced_options']['allowed_values_php'] = array(
          '#type' => 'textarea',
          '#title' => t('Code'),
          '#default_value' => !empty($field['allowed_values_php']) ? $field['allowed_values_php'] : '',
          '#rows' => 6,
          '#description' => t('Advanced Usage Only: PHP code that returns a keyed array of allowed values. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the array returned by this code will override the allowed values list above.'),
        );
      }
      else {
        $form['advanced_options']['markup_allowed_values_php'] = array(
          '#type' => 'item',
          '#title' => t('Code'),
          '#value' => !empty($field['allowed_values_php']) ? '<code>' . check_plain($field['allowed_values_php']) . '</code>' : t('&lt;none&gt;'),
          '#description' => empty($field['allowed_values_php']) ? t("You're not allowed to input PHP code.") : t('This PHP code was set by an administrator and will override the allowed values list above.'),
        );
      }
      return $form;
    case 'save':
      return array(
        'text_processing',
        'max_length',
        'allowed_values',
        'allowed_values_php',
      );
    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 'arguments':
      $arguments = content_views_field_arguments($field);
      $alias = "content: {$field['field_name']}";
      $argument = array(
        'handler' => 'text_views_argument_handler',
        'option' => 'string',
        'help' => t('Set the option to the number of initial characters to filter by. Leave empty for full term; use 1 for an A/B/C style glossary.'),
      );
      $arguments[$alias] = isset($arguments[$alias]) ? array_merge($arguments[$alias], $argument) : $argument;
      return $arguments;
    case 'filters':
      $allowed_values = text_allowed_values($field);
      if (count($allowed_values)) {
        return array(
          'default' => array(
            'list' => $allowed_values,
            'list-type' => 'list',
            'operator' => 'views_handler_operator_andor',
            'value-type' => 'array',
          ),
        );
      }
      else {
        return array(
          'like' => array(
            'operator' => 'views_handler_operator_like',
            'handler' => 'views_handler_filter_like',
          ),
        );
      }
      break;
  }
}
function text_views_argument_handler($op, &$query, $argtype, $arg = '') {
  $name = explode(':', is_array($argtype) ? $argtype['type'] : $argtype);
  $field_name = trim($name[1]);
  $field = content_fields($field_name);
  $db_info = content_database_info($field);
  $value = $db_info['columns']['value']['column'];
  $table = 'node_data_' . $field['field_name'];
  switch ($op) {
    case 'summary':
      $query
        ->ensure_table($table);
      $query
        ->add_field($value, $table);
      $length = intval($arg);
      $fieldinfo['field'] = $length <= 0 ? "{$table}.{$value}" : "LEFT({$table}.{$value}, {$length})";
      $fieldinfo['fieldname'] = 'letter';
      return $fieldinfo;
    case 'sort':
      break;
    case 'filter':
      $query
        ->ensure_table($table);
      $query
        ->add_field($value, $table);
      $length = intval($argtype['options']);
      $where = $length <= 0 ? "{$table}.{$value} = '%s'" : "LEFT({$table}.{$value}, {$length}) = '%s'";
      $query
        ->add_where($where, $arg);
      break;
    case 'link':
      return l(strtoupper($query->letter), "{$arg}/{$query->letter}");
    case 'title':
      return check_plain(strtoupper($query));
  }
}

/**
 * Implementation of hook_field().
 */
function text_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      $allowed_values = text_allowed_values($field);
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          $error_field = $field['field_name'] . '][' . $delta . '][value';
          if ($item['value'] != '') {
            if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
              form_set_error($error_field, t('Illegal value for %name.', array(
                '%name' => t($field['widget']['label']),
              )));
            }
          }
        }
      }
      if ($field['max_length'] > 0) {
        foreach ($items as $delta => $data) {
          $error_field = $field['field_name'] . '][' . $delta . '][value';
          if (drupal_strlen($data['value']) > $field['max_length']) {
            form_set_error($error_field, t('%label is longer than %max characters.', array(
              '%label' => t($field['widget']['label']),
              '%max' => $field['max_length'],
            )));
          }
        }
      }
      break;
  }
}

/**
 * Implementation of hook_field_formatter_info().
 */
function text_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Default'),
      'field types' => array(
        'text',
      ),
    ),
    'plain' => array(
      'label' => t('Plain text'),
      'field types' => array(
        'text',
      ),
    ),
    'trimmed' => array(
      'label' => t('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 ($allowed_values = text_allowed_values($field)) {
    return $allowed_values[$item['value']];
  }
  switch ($formatter) {
    case 'plain':
      $text = strip_tags($item['value']);
      break;
    case 'trimmed':
      $text = node_teaser($item['value'], $field['text_processing'] ? $item['format'] : NULL);
      break;
    case 'default':
      $text = $item['value'];
  }
  if ($field['text_processing']) {
    return check_markup($text, $item['format'], is_null($node) || isset($node->in_preview));
  }
  else {
    return check_plain($text);
  }
}

/**
 * Implementation of hook_widget_info().
 */
function text_widget_info() {
  return array(
    'text' => array(
      'label' => t('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']]['#description'] = content_filter_xss(t($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' => $delta == 0 ? t($field['widget']['label']) : '',
                '#default_value' => $data['value'],
                '#required' => $delta == 0 ? $field['required'] : FALSE,
                '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
                '#weight' => $field['widget']['weight'],
              );
            }
            else {
              $form[$field['field_name']][$delta]['value'] = array(
                '#type' => 'textarea',
                '#title' => $delta == 0 ? t($field['widget']['label']) : '',
                '#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' => $delta == 0 ? t($field['widget']['label']) : '',
              '#default_value' => '',
              '#required' => $delta == 0 ? $field['required'] : FALSE,
              '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
              '#weight' => $field['widget']['weight'],
            );
          }
          else {
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textarea',
              '#title' => $delta == 0 ? t($field['widget']['label']) : '',
              '#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' => content_filter_xss(t($field['widget']['description'])),
            '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
            '#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' => content_filter_xss(t($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 'process form values':

      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {
        if ($item['value'] == '' && $delta > 0) {
          unset($items[$delta]);
        }
      }
      break;
  }
}

/**
 *  Create an array of the allowed values for this field
 */
function text_allowed_values($field) {
  static $allowed_values;
  if ($allowed_values[$field['field_name']]) {
    return $allowed_values[$field['field_name']];
  }
  $allowed_values[$field['field_name']] = array();
  if ($field['allowed_values_php']) {
    ob_start();
    $result = eval($field['allowed_values_php']);
    if (is_array($result)) {
      $allowed_values[$field['field_name']] = $result;
    }
    ob_end_clean();
  }
  if (!$allowed_values[$field['field_name']]) {
    $list = explode("\n", $field['allowed_values']);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    foreach ($list as $opt) {

      // Sanitize the user input with a permissive filter.
      $opt = content_filter_xss($opt);
      list($key, $value) = explode('|', $opt);
      $allowed_values[$field['field_name']][$key] = isset($value) && $value !== '' ? $value : $key;
    }
  }
  return $allowed_values[$field['field_name']];
}

Functions

Namesort descending Description
text_allowed_values Create an array of the allowed values for this field
text_field Implementation of hook_field().
text_field_formatter Implementation of hook_field_formatter().
text_field_formatter_info Implementation of hook_field_formatter_info().
text_field_info Implementation of hook_field_info().
text_field_settings Implementation of hook_field_settings().
text_views_argument_handler
text_widget Implementation of hook_widget().
text_widget_info Implementation of hook_widget_info().
text_widget_settings Implementation of hook_widget_settings().