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

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

This example Drupal source code file (content_migrate.number.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, break, case, decimal_separator, function, if, number_decimal, number_integer, php, prefix_suffix, scale, thousand_separator, true

The content_migrate.number.inc Drupal example source code

<?php
/**
 * @file content_migrate.number.inc
 * Code to implement hook_content_migrate_field_alter, content_migrate_instance_alter() and content_migrate_data_record_alter()
 * on behalf of the former number module, moved into a separate file for efficiency.
 */

/**
 * Implements hook_content_migrate_field_alter().
 * 
 * Use this to tweak the conversion of field settings
 * from the D6 style to the D7 style for specific
 * situations not handled by basic conversion,
 * as when field types or settings are changed.
 */
function content_migrate_number_field_alter(&$field_value, $instance_value) {
  switch ($field_value['type']) {
   
    case 'number_integer':
    case 'number_decimal':
    case 'number_float':  
      
      // Changed name of setting from 'decimal' to 
      // 'decimal_separator'.
      if (isset($field_value['settings']['decimal'])) {
        $field_value['settings']['decimal_separator'] = $field_value['settings']['decimal'];
        unset($field_value['settings']['decimal']);
      }
      // Add a decimal_separator setting to floats.
      if ($field_value['type'] == 'number_float') {
        $field_value['settings']['decimal_separator'] = '.';
      }
      
      // Number fields using optionwidgets are
      // now List fields.
      switch ($instance_value['widget']['type']) {
        case 'optionwidgets_buttons':
        case 'optionwidgets_select':
          if ($field_value['type'] == 'number_integer') {
            $field_value['type'] = 'list_integer';
          }
          else {
            $field_value['type'] = 'list_float';
          }
          $field_value['module'] = 'list';
          break;
        case 'optionwidgets_onoff':
          $field_value['type'] = 'list_boolean';
          $field_value['module'] = 'list';
          break;
      }

      // The allowed values list should now be stored as an array.
      $allowed_values = array();
      if (!empty($field_value['settings']['allowed_values'])) {
        $allowed_values = content_migrate_extract_allowed_values($field_value['settings']['allowed_values'], $field_value['type']);
      }
      $field_value['settings']['allowed_values'] = $allowed_values;
      break;
  }

}

/**
 * Implements hook_content_migrate_instance_alter().
 *
 * Use this to tweak the conversion of instance or widget settings
 * from the D6 style to the D7 style for specific
 * situations not handled by basic conversion, as when
 * formatter or widget names or settings are changed.
 */
function content_migrate_number_instance_alter(&$instance_value, $field_value) {
  switch ($field_value['module']) {
    case 'number':
      // The number formatters and formatter settings
      // have changed.
      $new_type = array(
        'unformatted' => 'number_unformatted',
        'default' => 'number_decimal',
        'us_0' => 'number_integer',
        'us_1' => 'number_decimal',
        'us_2' => 'number_decimal',
        'be_0' => 'number_integer',
        'be_1' => 'number_decimal',
        'be_2' => 'number_decimal',        
        'fr_0' => 'number_integer',
        'fr_1' => 'number_decimal',
        'fr_2' => 'number_decimal',      
      );
      $new_settings = array(
        'default' => array(
          'thousand_separator' => '',
          'decimal_separator' => '.',
          'scale' => 0,
          'prefix_suffix' => TRUE,
        ),
        'us_0' => array(
          'thousand_separator' => ',',
          'decimal_separator' => '.',
          'scale' => 0,
          'prefix_suffix' => TRUE,
        ),        
        'us_1' => array(
          'thousand_separator' => ',',
          'decimal_separator' => '.',
          'scale' => 1,
          'prefix_suffix' => TRUE,
        ),
        'us_2' => array(
          'thousand_separator' => ',',
          'decimal_separator' => '.',
          'scale' => 2,
          'prefix_suffix' => TRUE,
        ),  
        'be_0' => array(
          'thousand_separator' => '',
          'decimal_separator' => ',',
          'scale' => 0,
          'prefix_suffix' => TRUE,
        ),        
        'be_1' => array(
          'thousand_separator' => '.',
          'decimal_separator' => ',',
          'scale' => 1,
          'prefix_suffix' => TRUE,
        ),
        'be_2' => array(
          'thousand_separator' => '.',
          'decimal_separator' => ',',
          'scale' => 2,
          'prefix_suffix' => TRUE,
        ),         
        'fr_0' => array(
          'thousand_separator' => '',
          'decimal_separator' => ', ',
          'scale' => 0,
          'prefix_suffix' => TRUE,
        ),        
        'fr_1' => array(
          'thousand_separator' => ' ',
          'decimal_separator' => ', ',
          'scale' => 1,
          'prefix_suffix' => TRUE,
        ),
        'fr_2' => array(
          'thousand_separator' => ' ',
          'decimal_separator' => ', ',
          'scale' => 2,
          'prefix_suffix' => TRUE,
        ),
      );
      foreach ($instance_value['display'] as $context => $settings) {
        $instance_value['display'][$context]['type'] = $new_type[$settings['type']];
        $instance_value['display'][$context]['settings'] = $new_settings[$settings['type']];
      }
      break;
  }

}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal content_migrate.number.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.