2

I'm currently developing a module which provides a content type called "Structural machinery" . This type should also provide a taxonomy term based on different manufacturers providing the machine (eg CAT or Komatsu) , I know how to create a taxonomy vocabulary programmatically but the question is how to attach a taxonomy field to my bundle.

this is the code used to implement the taxonomy term :

$term = new stdClass();
$term -> name = $t('Manufacture');
$term -> machine_name = 'manufacture';
$term -> description = $t('Description');
$term -> hierarchy = 2;
$term -> module = 'construction_machinery';
$term -> weight = 1;
taxonomy_vocabulary_save($term);
  • "Salam Dada", Did you create content type programmatically or you have content type and just want add it programmatically to that content type ? or you have content type and a field that referenced to this vocabulary and you just want save the data in it? Please provide more details about what you want – Yusef Sep 11 '15 at 10:32
  • @zhilevan "Aleyke Salam dada" , the module provides a new content type and attaches some fields to the content (fields like Engine, Transmission and ... which are implemented using Field API in some other modules) , another property which should be added to provided content type is the builder of the machine that should be saved as a term reference. so every thing is being implemented in hook_install() o the module. – Alireza Tabatabaeian Sep 11 '15 at 10:55
3

First create your field

$field_name = 'field_manufacture';

$field = field_info_field($field_name);
if(!$field){
  $field = array(
    'settings' => array(
      'allowed_values' => array(
        0 => array(
          'vocabulary' => 'manufacture',
          'parent' => '0',
        ),
      ),
    ),
    'field_name' => $field_name,
    'type' => 'taxonomy_term_reference',
    'module' => 'taxonomy',
    'active' => '1',
    'cardinality' => '1',
  );
  field_create_field($field);
}

Then create your instance(s)

$node_types = array('page','whatever');
foreach($node_types as $type){

  $instance = field_info_instance('node',$field_name,$type);
  if(!$instance){
    $instance = array(
      'label' => 'Manufacture',
      'widget' => 
      array(
        'weight' => 3,
        'type' => 'options_select',
        'module' => 'options',
        'active' => 1,
      ),
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => $type,
    );

    field_create_instance($instance);
  }

}

if you do var_export the return from field_info_field and field_info_instance of an existing field, you can see how the field arrays are structured & use this structure when creating new ones.

3

Programmatically create a taxonomy and attach a field to it, then create a content type and attach that taxonomy to it.

<?php
// 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
}

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.