home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (number.devel_generate.inc)

This example Drupal source code file (number.devel_generate.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, bundle, decimal, field, function, instance, is_numeric, math, max, object, php, precision, rand, scale

The number.devel_generate.inc Drupal example source code

<?php
// Id$

function number_devel_generate($object, $field, $instance, $bundle) {
  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
    return devel_generate_multiple('_number_devel_generate', $object, $field, $instance, $bundle);
  }
  else {
    return _number_devel_generate($object, $field, $instance, $bundle);
  }
}

function _number_devel_generate($object, $field, $instance, $bundle) {
  $object_field = array();
  // Make sure the instance settings are all set.
  foreach (array('min', 'max', 'precision', 'scale') as $key) {
    if (empty($instance['settings'][$key])) {
      $instance['settings'][$key] = NULL;
    }
  }
  $min = is_numeric($instance['settings']['min']) ? $instance['settings']['min'] : 0;
  switch ($field['type']) {
    case 'number_integer':
      $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : 10000;
      $decimal = 0;
      $scale = 0;
      break;

    case 'number_decimal':
      $precision = is_numeric($instance['settings']['precision']) ? $instance['settings']['precision'] : 10;
      $scale = is_numeric($instance['settings']['scale']) ? $instance['settings']['scale'] : 2;
      $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
      $decimal = rand(0, (10 * $scale)) / 100;
      break;

    case 'number_float':
      $precision = rand(10, 32);
      $scale = rand(0, 2);
      $decimal = rand(0, (10 * $scale)) / 100;
      $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
      break;
  }
  $object_field['value'] = round((rand($min, $max) + $decimal), $scale);
  return $object_field;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal number.devel_generate.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.