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

Drupal example source code file (content_migrate.install)

This example Drupal source code file (content_migrate.install) 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, as, body_weight, break, empty, field, foreach, if, instance, new_field, php, stored_settings, type_name, weights

The content_migrate.install Drupal example source code

<?php

/**
 * Transfer weights from the D6 Manage Fields screen
 * to the D7 stored settings array. Do this once only, on install,
 * so later changes to these values in the Field UI screen will
 * not be overwritten.
 */
function content_migrate_install() {
  $stored_settings = variable_get('field_bundle_settings', array());

  // $stored_settings might be empty, set up the basic structure of the
  // array to avoid errors.
  if (empty($stored_settings) || !array_key_exists('node', $stored_settings)) {
    $stored_settings['node'] = array();
  }
  foreach (node_type_get_names() as $type_name => $label) {
    // This type name may not yet exist in the array, set it up.
    if (!array_key_exists($type_name, $stored_settings['node'])) {
      $stored_settings['node'][$type_name] = array(
        'extra_fields' => array('form' => array(), 'display' => array()),
        'view_modes' => array(),
      );
    }
    $weights = variable_get('content_extra_weights_'. $type_name, array());
    
    // If body has never been set in D6, it should be set to 0 to match the way D6 worked.
    // The D7 core update may have set it to a value like -4.
    if (empty($weights) || !array_key_exists('body_field', $weights)) {
      $stored_settings['node'][$type_name]['extra_fields']['form']['body'] = array('weight' => 0);
    }
      
    // If a field name provided by a custom module has changed from D6 to D7,
    // they will have to alter this array too. We will set the original
    // field name, other modules could take this information and move it 
    // to a setting with the new field name.

    // @TODO Have any of the other core field names changed?
    // @TODO Should we do anything to alter the display settings?
    foreach ($weights as $field => $weight) {
      switch ($field) {
        case 'body_field':
          $new_field = 'body';
          break;
        default:
          $new_field = $field;
          break;
      }
      $stored_settings['node'][$type_name]['extra_fields']['form'][$new_field] = array('weight' => $weight);
    }

    // While we're here, update the body field with the right weight.
    $body_weight = !empty($weights['body_field']) ? $weights['body_field'] : 0;
    $instance = field_info_instance('node', 'body', $type_name);
    if (!empty($instance)) {
      $instance['widget']['weight'] = $body_weight;
      foreach ($instance['display'] as $context => $format) {
        $instance['display'][$context]['weight'] = $body_weight;
      }
      field_update_instance($instance);
    }
  }
  variable_set('field_bundle_settings', $stored_settings);
  field_info_cache_clear();
}

Other Drupal examples (source code examples)

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