You are here

function weight_update_7300 in Weight 7.3

Create a Weight field for each enabled content type.

File

./weight.install, line 28
Install, update, and uninstall functions for the Weight module.

Code

function weight_update_7300() {
  field_cache_clear();
  $field_name = 'field_weight';
  $node_types = node_type_get_names();

  // Check for weight_settings table, then create a weight field for each
  // content type that has weight enabled.
  if (db_table_exists('weight_settings')) {
    $types = db_select('weight_settings', 'ws')
      ->fields('ws')
      ->execute();

    // Make sure the field name doesn't exist.
    $field = field_read_field($field_name, array(
      'include_inactive' => TRUE,
    ));
    if (!empty($field)) {
      $i = 2;
      while ($field = field_read_field($field_name . $i, array(
        'include_inactive' => TRUE,
      ))) {
        $i++;
      }
      $field_name = $field_name . $i;
    }
    $field = array(
      'field_name' => $field_name,
      'type' => 'weight',
    );
    field_create_field($field);
    foreach ($types as $type) {
      if ($type->weight_enabled && array_key_exists($type->type, $node_types)) {
        $instance = array(
          'field_name' => $field_name,
          'entity_type' => 'node',
          'label' => t('Weight'),
          'bundle' => $type->type,
          'settings' => array(
            'range' => $type->weight_range,
          ),
          'widget' => array(
            'type' => 'weight_selector',
          ),
        );
        field_create_instance($instance);
      }
    }
  }
  $_SESSION['weight_field_name'] = $field_name;
}