You are here

fraction_entity_test.install in Fraction 7

File

tests/fraction_entity_test/fraction_entity_test.install
View source
<?php

/**
 * Implements hook_schema().
 */
function fraction_entity_test_schema() {
  $schema = array();
  $schema['fraction_entity_test'] = array(
    'fields' => array(
      'id' => array(
        'description' => 'Fraction Entity Test ID',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'Bundle',
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
      ),
      'time_stamp' => array(
        'description' => 'Timestamp',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $schema['fraction_entity_test_type'] = array(
    'fields' => array(
      'id' => array(
        'description' => 'Transaction type id',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Machine name',
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'Human-readable name',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'status' => array(
        'description' => 'Type status used by Entity API',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0x1,
      ),
      'module' => array(
        'description' => 'Module owner of type used by Entity API',
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_enable().
 */
function fraction_entity_test_enable() {

  // Create the default bundle that is defined in fraction_entity_test_entity_info().
  $bundle_info = array(
    'name' => 'fraction_entity_test_bundle',
    'label' => st('Fraction Entity Test Bundle'),
    'status' => 0x1,
    'module' => 'fraction_entity_test',
  );
  $bundle = new FractionEntityTestType($bundle_info);
  $bundle
    ->save();

  // Add fraction field to make it possible to work with decimals w/o rounding errors.
  $instances_default = _fraction_entity_test_default_instances();
  foreach (_fraction_entity_test_default_fields() as $field_name => $field_default) {
    $field = field_info_field($field_name);
    if (empty($field)) {
      field_create_field($field_default);
    }
    if (!empty($instances_default[$field_name])) {
      $instance = field_info_instance('fraction_entity_test', $field_name, 'fraction_entity_test_bundle');
      if (empty($instance)) {
        field_create_instance($instances_default[$field_name]);
      }
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function fraction_entity_test_uninstall() {

  // Delete fraction field and instance.
  foreach (_fraction_entity_test_default_instances() as $instance) {
    field_delete_instance($instance);
  }
  foreach (_fraction_entity_test_default_fields() as $field) {
    field_delete_field($field['field_name']);
  }
}
function _fraction_entity_test_default_fields() {
  return array(
    'fraction_field' => array(
      'field_name' => 'fraction_field',
      'type' => 'fraction',
      'cardinality' => 1,
      'entity_types' => array(
        'fraction_entity_test',
      ),
      'translatable' => FALSE,
    ),
  );
}
function _fraction_entity_test_default_instances() {
  return array(
    'fraction_field' => array(
      'field_name' => 'fraction_field',
      'label' => t('Fraction Field'),
      'type' => 'fraction',
      'widget' => array(
        'type' => 'fraction_decimal',
      ),
      'display' => array(),
      'entity_type' => 'fraction_entity_test',
      'bundle' => 'fraction_entity_test_bundle',
    ),
  );
}