0

I've added a vocabulary(accommodation_type) in my accommodation module and referenced it to a field that I created in a (accommodation)content type I also created programmatically.

The Content type, the vocabulary AND the referencing field are all created. But when I create a term in the newly found Vocabulary it's not showing up in the dropdown field when I try to add content.

The weird part is: If I manually add a field to the Accommodation content-type. Or switch the Vocabulary of the existing referencing field to a different vocabulary and switch back to the accommodation_type vocabulary it works....

Howcome the terms I've added to my newly found Vocabulary are not showing up in the dropdown field of my content type?

Here's my complete module:

<?php
/**
 * Implements hook_install().
 */
function accommodation_install() {
    node_types_rebuild();
    $types = node_type_get_types();
    variable_set('comment_accommodation', '0');
    node_add_body_field($types['accommodation']);

    accommodation_add_vocabularies(); 
    accommodation_add_custom_fields();


    drupal_flush_all_caches();
}

function accommodation_add_vocabularies () {
    foreach(_accommodation_get_vocabularies() as $voc) {
        $vocabulary = (object) $voc;
        taxonomy_vocabulary_save($vocabulary);

        // Store the vid so we can delete the vocabulary later.   
        // Storing the vid is also useful for when we want to programmatically insert terms.    
        variable_set($vocabulary->machine_name . '_vid', $vocabulary->vid); 
    }
}




function accommodation_uninstall() {
    $ournewtype = 'accommodation';
    $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
    $result = db_query($sql, array(':type' => $ournewtype));
    $nodeids = array();
    foreach ($result as $row) {
        $nodeids[] = $row->nid;
    }
    node_delete_multiple($nodeids);

    accommodation_delete_vocabularies();

    accommodation_delete_custom_fields();
    node_type_delete($ournewtype);
    field_purge_batch(500);
}

function _accommodation_get_vocabularies () {
    $t = get_t();
    return array(
        array(
            'name' => $t('Accommodation type'),
            'machine_name' => 'accommodation_type',
            'description' => t('For example: Villa, chalet etc...'),
            'module' => 'taxonomy',
        ),
    );
}

function _accommodation_installed_fields() {
    $t = get_t();
    return array(
        'accommodation_price' => array(
            'field_name' => 'accommodation_price',
            'label' => $t('Price of the accommodation'),
            'type' => 'text'
        ),

        'accommodation_type' => array(
            'field_name' => 'accommodation_type',
            'label' => $t('Accommodation type'),
            'type' => 'taxonomy_term_reference',
            'instance_settings' => array(
                'allowed_values' => array(
                    array(
                        'vocabulary' => 'accommodation_type'
                    ),
                ),
            )
        ),
    );
}

function _accommodation_installed_instances() {
    $t = get_t();
    return array(
        'accommodation_price' => array(
            'field_name' => 'accommodation_price',
            'type' => 'text',
            'label' => $t('Price of the accommodation'),
            'widget' => array(
                'type' => 'text_textfield'
            ),
            'display' => array(
                'example_node_list' => array(
                    'label' => $t('Price of the accommodation'),
                    'type' => 'text'
                )
            )
        ),
        'accommodation_type' => array(
            'field_name' => 'accommodation_type',
            'entity_type' => 'node',
            'label' => $t('Accommodation type'),
            'bundle' => 'accommodation',
            'required' => true,
            'widget' => array(
                'type' => 'options_select'
            )
        )
    );
}

function accommodation_add_custom_fields() {
    foreach (_accommodation_installed_fields() as $field) {
        field_create_field($field);
    }
    foreach (_accommodation_installed_instances() as $fieldinstance) {
        $fieldinstance['entity_type'] = 'node';
        $fieldinstance['bundle'] = 'accommodation';

        field_create_instance($fieldinstance);
    }
}

1 Answer 1

1

You're missing the index settings. Try adding these as your term reference and re-install.

Install:

    'accommodation_type' => array(
        'field_name' => 'accommodation_type',
        'label' => $t('Accommodation type'),
        'type' => 'taxonomy_term_reference',
        'active' => 1,
        'cardinality' => 1,
        'deleted' => 0,
        'entity_types' => array(),
        'indexes' => array(
          'tid' => array(
            0 => 'tid',
          ),
        ),
        'locked' => 0,
        'module' => 'taxonomy',
        'settings' => array(
          'allowed_values' => array(
            0 => array(
              'vocabulary' => 'accommodation_type',
              'parent' => 0,
              'depth' => '',
            ),
          ),
        ),
        'translatable' => 0,
        'type' => 'taxonomy_term_reference',
    ),

Instance:

    'accommodation_type' => array(
        'field_name' => 'accommodation_type',
        'entity_type' => 'node',
        'label' => $t('Accommodation type'),
        'bundle' => 'accommodation',
        'required' => true,
        'settings' => array(
          'user_register_form' => FALSE,
        ),
        'widget' => array(
          'active' => 1,
          'module' => 'options',
          'type' => 'options_select',
        ),
        'display' => array(
            'default' => array(
                'label' => 'above',
                'module' => 'taxonomy',
                'settings' => array(
                  'empty_fields_emptyfieldtext_empty_text' => '',
                  'empty_fields_handler' => '',
                ),
                'type' => 'taxonomy_term_reference_link',
                'weight' => 1,
            ),
        ),
    ),

When creating taxonomy, Drupal needs the terms to be indexed for referencing. Take a look in your database's taxonomy_index table for your tids. If they are missing, that's probably the root of the issue.

Personally, I prefer Entity References in lieu of Term References. They are essentially the same, but the field settings might be a little different. That said, the example I shared might not be the exact solution, but I'm sure that's your issue. If you are still having trouble with it, set up your content type so it works as intended and then export it using the Features module. The field include will show you what you are missing.

4
  • It's doesn't seem to be working with above code. It's weird how no tutorial even mentions this... Sep 2, 2015 at 6:48
  • I've updated the example code. I tested this and it should work with a fresh install. I've been in this situation before and I too found that the documentation was somewhat vague, or at least not helpful in resolving the indexing issue. It took me some time messing with it, so It thought I'd help you out. Best of luck.
    – knice
    Sep 2, 2015 at 14:55
  • Thanks a lot! Could you tell my how I can make it to accept multiple values via a checkbox? I've tried adding widget type 'option_buttons' but it doesn't seem to work. Sep 3, 2015 at 7:20
  • Found it; It was cardinality ;) Sep 3, 2015 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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