You are here

content.devel.inc in Content Construction Kit (CCK) 5

Functions needed for Devel module integration.

File

content.devel.inc
View source
<?php

/**
 * @file
 * Functions needed for Devel module integration.
 */

/**
 * Enrich the $node that is about to be saved with arbitrary 
 * information in each of its CCK fields.
 **/
function content_generate_fields(&$node) {
  $type_name = $node->type;
  $type = content_types($type_name);
  $field_types = _content_field_types();
  if (!empty($type['fields'])) {
    foreach ($type['fields'] as $field) {
      $node_field = array();
      $module = $field_types[$field['type']]['module'];
      $function = $module . '_content_generate';
      if (function_exists($function)) {
        $result = $function($node, $field);

        // $items, $teaser, $page
        $node_field = $result;
      }
      $node->{$field}['field_name'] = $node_field;
    }
  }
}

/**
 * A simple function to return multiple values for fields that use
 * custom multiple value widgets but don't need any other special multiple 
 * values handling. This will call the field generation function
 * a random number of times and compile the results into a node array.
 */
function content_devel_multiple($function, $node, $field) {
  $node_field = array();
  if (function_exists($function)) {
    switch ($field['multiple']) {
      case 0:
        $max = 0;
        break;
      case 1:
        $max = rand(0, 3);

        //just an arbitrary number for 'unlimited'
        break;
      default:
        $max = $field['multiple'];
        break;
    }
    for ($i = 0; $i <= $max; $i++) {
      $node_field[$i] = $function($node, $field);
    }
  }
  return $node_field;
}
if (module_exists('text')) {
  function text_content_generate($node, $field) {
    return content_devel_multiple('_text_content_generate', $node, $field);
  }
  function _text_content_generate($node, $field) {
    $node_field = array();
    if ($field['widget']['rows'] > 1) {
      $format = $field['text_processing'] ? rand(0, 3) : 0;
      $node_field['value'] = devel_create_content($format);
      $node_field['format'] = $format;
    }
    else {
      $allowed_values = text_allowed_values($field);
      if (!empty($allowed_values)) {

        // Just pick one of the specified allowed values.
        $node_field['value'] = array_rand($allowed_values);
      }
      else {

        // Generate a value that respects max_length.
        if (empty($field['max_length'])) {
          $field['max_length'] = 12;
        }
        $node_field['value'] = user_password($field['max_length']);
      }
    }
    return $node_field;
  }
}
if (module_exists('number')) {
  function number_content_generate($node, $field) {
    return content_devel_multiple('_number_content_generate', $node, $field);
  }
  function _number_content_generate($node, $field) {
    $node_field = array();
    $allowed_values = number_allowed_values($field);
    if (!empty($allowed_values)) {

      // Just pick one of the specified allowed values.
      $node_field['value'] = array_rand($allowed_values);
    }
    else {
      $min = is_numeric($field['min']) ? $field['min'] : 0;
      switch ($field['type']) {
        case 'number_integer':
          $max = is_numeric($field['max']) ? $field['max'] : 10000;
          $scale = 0;
          break;
        case 'number_decimal':
          $precision = is_numeric($field['precision']) ? $field['precision'] : 10;
          $scale = is_numeric($field['scale']) ? $field['scale'] : 2;
          $max = is_numeric($field['max']) ? $field['max'] : pow(10, $precision);
          break;
        case 'number_float':
          $precision = rand(10, 32);
          $scale = rand(0, 2);
          $max = is_numeric($field['max']) ? $field['max'] : pow(10, $precision);
          break;
      }
      $node_field['value'] = round(rand($min, $max), $scale);
    }
    return $node_field;
  }
}
if (module_exists('nodereference')) {
  function nodereference_content_generate($node, $field) {
    return content_devel_multiple('_nodereference_content_generate', $node, $field);
  }
  function _nodereference_content_generate($node, $field) {
    $node_field = array();
    $allowed_values = nodereference_allowed_values($field);
    unset($allowed_values[0]);
    if (!empty($allowed_values)) {

      // Just pick one of the specified allowed values.
      $node_field['nid'] = array_rand($allowed_values);
    }
    return $node_field;
  }
}
if (module_exists('userreference')) {
  function userreference_content_generate($node, $field) {
    return content_devel_multiple('_userreference_content_generate', $node, $field);
  }
  function _userreference_content_generate($node, $field) {
    $node_field = array();
    $allowed_values = userreference_allowed_values($field);
    if (isset($allowed_values['none'])) {
      unset($allowed_values['none']);
    }
    if (!empty($allowed_values)) {

      // Just pick one of the specified allowed values.
      $node_field['uid'] = array_rand($allowed_values);
    }
    return $node_field;
  }
}

Functions

Namesort descending Description
content_devel_multiple A simple function to return multiple values for fields that use custom multiple value widgets but don't need any other special multiple values handling. This will call the field generation function a random number of times and compile the…
content_generate_fields Enrich the $node that is about to be saved with arbitrary information in each of its CCK fields.