function tripal_chado_bundle_create_user_field

3.x tripal_chado.fields.inc tripal_chado_bundle_create_user_field($new_field, $bundle)

Implements hook_bundle_create_user_field().

A priviledged user has the ability to add new fields to the bundle. The chado_linker__prop and chado_linker__cvterm fields are allowed to be added dynamically by the user. But, Drupal doesn't know how to deal with it, so this function is called for any field attached to a TripalEntity bundle type. Any fields whose TripalField::$module argument is set to 'tripal_chado' and that can be added dynamically will result in a call to this function.

File

tripal_chado/includes/tripal_chado.fields.inc, line 2287

Code

function tripal_chado_bundle_create_user_field($new_field, $bundle) {

  // Get the table this bundle is mapped to.
  $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  $vocab = $term->vocab;
  $params = array(
    'vocabulary' => $vocab->vocabulary,
    'accession' => $term->accession,
  );
  $chado_table = $bundle->data_table;
  $chado_type_table = $bundle->type_linker_table;
  $chado_type_column = $bundle->type_column;
  $chado_type_id = $bundle->type_id;
  $chado_type_value = $bundle->type_value;

  // We allow site admins to add new chado_linker__prop fields to an entity.
  // This function will allow us to properly add them.  But at this point we
  // don't know the controlled vocabulary term.  We'll have to use the
  // defaults and let the user set it using the interface.
  if ($new_field['type'] == 'chado_linker__prop') {
    $table_name = $chado_table . 'prop';

    if (chado_table_exists($table_name)) {
      $schema = chado_get_schema($table_name);
      $pkey = $schema['primary key'][0];
      $field_name = $new_field['field_name'];
      $field_type = 'chado_linker__prop';

      // First add the field.
      field_create_field(array(
        'field_name' => $field_name,
        'type' => $field_type,
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
        'locked' => FALSE,
        'storage' => array(
          'type' => 'field_chado_storage',
        ),
      ));

      // Now add the instance
      field_create_instance(array(
        'field_name' => $field_name,
        'entity_type' => 'TripalEntity',
        'bundle' => $bundle->name,
        'label' => $new_field['label'],
        'description' => '',
        'required' => FALSE,
        'settings' => array(
          'auto_attach' => TRUE,
          'base_table' => $chado_table,
          'chado_table' => $table_name,
          'chado_column' => $pkey,
          'term_vocabulary' => '',
          'term_accession' => '',
          'term_name' => ''
        ),
        'widget' => array(
          'type' => 'chado_linker__prop_widget',
          'settings' => array(
            'display_label' => 1,
          ),
        ),
        'display' => array(
          'default' => array(
            'label' => 'inline',
            'type' => 'chado_linker__prop_formatter',
            'settings' => array(),
          ),
        ),
      ));
    }
    else {
      drupal_set_message('Cannot add a property field to this entity. Chado does not support properties for this data type.', 'error');
    }
  }

  // We allow site admins to add new chado_linker__cvterm fields to an entity.
  // This function will allow us to properly add them.  But at this point we
  // don't know the controlled vocabulary term.  We'll have to use the
  // defaults and let the user set it using the interface.

  if ($new_field['type'] == 'chado_linker__cvterm') {
    $table_name = $chado_table . '_cvterm';

    if (chado_table_exists($table_name)) {
      $schema = chado_get_schema($table_name);
      $pkey = $schema['primary key'][0];
      $field_name = $new_field['field_name'];
      $field_type = 'chado_linker__cvterm';

      // First add the field.
      field_create_field(array(
        'field_name' => $field_name,
        'type' => $field_type,
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
        'locked' => FALSE,
        'storage' => array(
          'type' => 'field_chado_storage',
        ),
        'settings' => array(
          'base_table' => $chado_table,
          'chado_table' => $table_name,
          'chado_column' => $pkey,
        ),
      ));

      // Now add the instance
      field_create_instance(array(
        'field_name' => $field_name,
        'entity_type' => 'TripalEntity',
        'bundle' => $bundle->name,
        'label' => $new_field['label'],
        'description' => '',
        'required' => FALSE,
        'settings' => array(
          'auto_attach' => TRUE,
        ),
        'widget' => array(
          'type' => 'chado_linker__cvterm_widget',
          'settings' => array(
            'display_label' => 1,
          ),
        ),
        'display' => array(
          'default' => array(
            'label' => 'above',
            'type' => 'chado_linker__cvterm_formatter',
            'settings' => array(),
          ),
        ),
      ));
    }
    else {
      drupal_set_message('Cannot add a property field to this entity. Chado does not support annotations for this data type.', 'error');
    }
  }
}