Skip to content

Instantly share code, notes, and snippets.

@JustAdam
Created August 2, 2012 09:49
Show Gist options
  • Select an option

  • Save JustAdam/3235992 to your computer and use it in GitHub Desktop.

Select an option

Save JustAdam/3235992 to your computer and use it in GitHub Desktop.

Revisions

  1. JustAdam revised this gist Aug 2, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions drupal_taxonomy_content_type.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    // Machine name for our custom node
    define('NODE_NAME', 'the_node_machine_name');
    // Machine name for our custom taxonomy
  2. JustAdam created this gist Aug 2, 2012.
    102 changes: 102 additions & 0 deletions drupal_taxonomy_content_type.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    // Machine name for our custom node
    define('NODE_NAME', 'the_node_machine_name');
    // Machine name for our custom taxonomy
    define('TAXONOMY_NAME', 'the_taxonomy_machine_name');

    function module_install() {
    _create_taxonomy();
    _create_content_type();
    }

    /**
    * Create a taxonomy and attach a field to it.
    */
    function _create_taxonomy() {
    $t = get_t();

    $term = new stdClass();
    $term->name = $t('Name');
    $term->machine_name = TAXONOMY_NAME;
    $term->description = $t('Description');
    $term->heirarchy = 1;
    $term->module = 'module_name';
    $term->weight = 1;
    taxonomy_vocabulary_save($term);

    // Create a field
    $field = array(
    'field_name' => 'field_tax_fieldname',
    'type' => 'text',
    'label' => $t('Label')
    );
    field_create_field($field);

    // Attach the field to our taxonomy entity
    $instance = array(
    'field_name' => 'field_tax_fieldname',
    'entity_type' => 'taxonomy_term',
    'bundle' => TAXONOMY_NAME,
    'label' => $t('Label'),
    'description' => $t('Description'),
    'required' => true,
    'widget' => array(
    'type' => 'text_textfield',
    'weight' => 3
    )
    );
    field_create_instance($instance);

    // Done
    }

    /**
    * Create a content type and attach our created taxonomy to it.
    */
    function _create_content_type() {
    $t = get_t();

    $node = array(
    'type' => NODE_NAME,
    'name' => $t('Name'),
    'base' => 'node_content',
    'description' => $t('Description'),
    'title_label' => $t('Title'),
    'custom' => TRUE
    );
    $content_type = node_type_set_defaults($node);
    node_add_body_field($content_type, $t('Article'));
    node_type_save($content_type);

    // Create a taxonomy field and use the taxonomy entity we created earlier
    $field = array(
    'field_name' => 'field_tax_name',
    'type' => 'taxonomy_term_reference',
    'label' => $t('Label'),
    'settings' => array(
    'allowed_values' => array(
    array(
    'vocabulary' => TAXONOMY_NAME,
    'parent' => 0
    )
    )
    )
    );
    field_create_field($field);

    // Add the field to the content type as a HTML select box.
    $instance = array(
    'field_name' => 'field_tax_name',
    'entity_type' => 'node',
    'bundle' => NODE_NAME,
    'label' => $t('Label'),
    'description' => '',
    'required' => TRUE,
    'widget' => array(
    'type' => 'options_select',
    'weight' => -10,
    )
    );
    field_create_instance($instance);

    // Done
    }