You are here

wp_blog.install in WP Blog - a WordPress-style blogging module. 7

Install functions for the WP Blog module.

File

wp_blog.install
View source
<?php

/**
 * @file
 * Install functions for the WP Blog module.
 */

// Default pathauto path for WP Blog nodes.
define('WP_BLOG_DEFAULT_NODE_PATH', 'blog/[node:created:custom:Y]/[node:created:custom:m]/[node:created:custom:d]/[node:title]');

/**
 * Implements hook_requirements().
 */
function wp_blog_requirements($phase) {
  $t = get_t();

  // The blog-post URL structure works best with:
  // - pathauto enabled.
  // - the path set to /blog/YYYY/MM/DD/title
  $requirements = array();
  if ($phase == 'runtime') {
    $has_pathauto = module_exists('pathauto');
    $has_correct_path = variable_get('pathauto_node_wp_blog_pattern', '') == WP_BLOG_DEFAULT_NODE_PATH;
    if (!$has_pathauto) {
      $requirements['wp_blog'] = array(
        'title' => $t('WP Blog'),
        'description' => $t("Pathauto is recommended to provide WP Blog's recommended URL structure. Download pathauto from the <a href='@pathauto_url'>pathauto project page</a>.", array(
          '@pathauto_url' => 'http://drupal.org/project/pathauto',
        )),
        'value' => $t('Missing pathauto'),
        'severity' => REQUIREMENT_WARNING,
      );
    }
    elseif (!$has_correct_path) {
      $requirements['wp_blog'] = array(
        'title' => $t('WP Blog'),
        'description' => $t("The pattern for all WP blog post paths is not set to the recommended pattern.<br />It's currently %current_pattern and should be %recommended_pattern.  This can be changed on the <a href='@url_patterns_url'>URL aliases' patterns page</a>.", array(
          '%current_pattern' => variable_get('pathauto_node_wp_blog_pattern', $t('not set')),
          '%recommended_pattern' => $recommended_url_path,
          '@url_patterns_url' => url('admin/config/search/path/patterns'),
        )),
        'value' => $t('Incorrect URL alias pattern'),
        'severity' => REQUIREMENT_WARNING,
      );
    }
    else {
      $requirements['wp_blog'] = array(
        'title' => $t('WP Blog'),
        'description' => $t("Pathauto is enabled and the WP Blog path is correctly set."),
        'value' => $t('Configuration correct'),
        'severity' => REQUIREMENT_OK,
      );
    }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function wp_blog_install() {

  // Include the module-file to load the constants.
  require_once dirname(__FILE__) . '/wp_blog.module';
  $t = get_t();

  // Add the body field to the WP Blog content type.
  node_types_rebuild();
  $types = node_type_get_types();
  node_add_body_field($types[WP_BLOG_DEFAULT_CTYPE], $t('Body'));

  // Add a taxonomy vocabulary.
  _wp_blog_create_vocabulary();

  // Configure pathauto with the appropriate path structure:
  // /blog/yyyy/mm/dd/node-title.
  variable_set('pathauto_node_wp_blog_pattern', WP_BLOG_DEFAULT_NODE_PATH);
}

/**
 * Create the 'wp_blog_tags' vocabulary (if it doesn't already exist).
 */
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);
  }
}

Functions

Namesort descending Description
wp_blog_install Implements hook_install().
wp_blog_requirements Implements hook_requirements().
_wp_blog_create_vocabulary Create the 'wp_blog_tags' vocabulary (if it doesn't already exist).

Constants