You are here

function _wp_blog_create_vocabulary in WP Blog - a WordPress-style blogging module. 7

Create the 'wp_blog_tags' vocabulary (if it doesn't already exist).

1 call to _wp_blog_create_vocabulary()
wp_blog_install in ./wp_blog.install
Implements hook_install().

File

./wp_blog.install, line 82
Install functions for the WP Blog module.

Code

function _wp_blog_create_vocabulary() {
  $t = get_t();

  // If we enable WP blog at the same time as taxonomy we need to call
  // field_associate_fields() as otherwise the field won't be enabled until
  // hook modules_enabled is called which takes place after hook_enable events.
  field_associate_fields('taxonomy');

  // Create the forum vocabulary if it does not exist.
  $vocabulary = taxonomy_vocabulary_load(variable_get('wp_blog_vocabulary', 0));
  if (!$vocabulary) {
    $edit = array(
      'name' => $t('Blog tags'),
      'machine_name' => 'wp_blog_tags',
      'description' => t('Tags to categorise blog posts'),
      'hierarchy' => 1,
      'module' => 'wp_blog',
    );
    $vocabulary = (object) $edit;
    taxonomy_vocabulary_save($vocabulary);
    variable_set('wp_blog_vocabulary', $vocabulary->vid);
  }

  // Create the 'taxonomy_wp_blog_tags' field if it doesn't already exist.
  if (!field_info_field('taxonomy_wp_blog_tags')) {
    $field = array(
      'field_name' => 'taxonomy_' . $vocabulary->machine_name,
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $vocabulary->machine_name,
            'parent' => 0,
          ),
        ),
      ),
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);

    // Create the instance on the bundle.
    $instance = array(
      'field_name' => 'taxonomy_' . $vocabulary->machine_name,
      'entity_type' => 'node',
      'label' => $vocabulary->name,
      'bundle' => 'wp_blog',
      'required' => FALSE,
      'widget' => array(
        'type' => 'taxonomy_autocomplete',
      ),
      'display' => array(
        'default' => array(
          'type' => 'taxonomy_term_reference_link',
          'weight' => 10,
        ),
        'teaser' => array(
          'type' => 'taxonomy_term_reference_link',
          'weight' => 10,
        ),
      ),
    );
    field_create_instance($instance);
  }
}