0

I'm trying to create a field with an additional property (or column) to define an extra relationship to a taxonomy term.

I have this in my module file:

/**
 * Implements hook_field_info().
 */
function emtr_field_info() {
    return array(
        // We name our field as the associative name of the array.
        'field_emtr_denominations' => array(
            'label' => t('Denominations'),
            'description' => t('Field linked to target entity as well as denomination taxonomy term holding denomnation value.'),
            'default_widget' => 'number',
            'default_formatter' => 'number_decimal',
        ),
    );
}

And this in my install file:

/**
 * Implements hook_field_schema().
 */
function emtr_field_schema($field) {
    $columns = array(
        'denomination_value' => array(
            'type' => 'int',
            'not null' => FALSE,
        ),
        'denomination_tid' => array(
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
        ),
    );
    $indexes = array(
        'denomination_tid' => array('denomination_tid'),
    );
    return array(
        'columns' => $columns,
        'indexes' => $indexes,
    );
}

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

    field_cache_clear();
    field_associate_fields('emtr');

    if (field_info_field('emtr_denominations')) return;

    // Create a field for denominations to be used by safe checks and till reads
    $field = array(
        'field_name' => 'emtr_denominations',
        'type' => 'field_emtr_denominations',
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
        'translatable' => FALSE,
    );
    field_create_field($field);

    // Attach the emtr_denominations reference field to safe checks instance
    $instance = array(
        'field_name' => 'emtr_denominations',
        'entity_type' => 'emtr_safe_check',
        'bundle' => 'emtr_safe_check',
        'label' => 'Denominations',
        'required' => false,
        'widget' => array(
            'type' => 'number'
        ),
        'settings' => array(
            'target_type' => 'emtr_safe_check',
            'handler_settings' => array('target_bundles' => NULL),
        ),
        'display' => array(
            'default' => array('label' => 'inline', 'type' => 'number_integer'),
        ),
    );
    field_create_instance($instance);
}

But after uninstalling and re-enabling my module (drush devel-reinstall emtr -y ) a table called field_data_emtr_denominations is created but it does not have my 2 custom columns.

At what point in this process should I tell Drupal to use my custom field_schema?

  • I'm not sure what I've done but after a couple more hours repeatedly installing and uninstalling my module it started working. I think maybe there was a conflict with another field created though the field UI but I'm not entirely sure. But for future reference the above code is now working fine. – Felix Eve Feb 13 '14 at 14:52
0

You should remove function emtr_enable() out of the .install file, and place its code in a more proper hook where the field is meaningful to be created, like for instance hook_node_type_insert() or EntityAPIController-derived class for your entity bundle, etc. It depends on what one wants to achieve really.

| improve this answer | |
  • emtr_enable implements hook_enable. I would put the code in there in hook_install however I understood that that would lead to problems as my hook_field_schema would not have run when the hook_install runs. Hook enable can only live in the modules install file so I can't move that anywhere else. – Felix Eve Feb 13 '14 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.