You are here

import_node.install in Import 6

The install file for the import_node example. This module should not be used on a production installation of Drupal ... it is for illustration purposes only. The install hook removes all traces of what was created by this module and could potentially be destructive on a production site.

File

examples/import_node/import_node.install
View source
<?php

/**
 * @file
 * The install file for the import_node example.  This module should not be used
 * on a production installation of Drupal ... it is for illustration purposes
 * only.  The install hook removes all traces of what was created by this module
 * and could potentially be destructive on a production site.
 */

/**
 * Implementation of hook_install().
 *
 * Remove the variables, nodes and schema corresponding to the module.
 */
function import_node_install() {

  // Create this variable to indicate during cron that our import has not been
  // staged.
  define('IMPORT_NODE_STAGED', FALSE);

  // Create tables.
  drupal_install_schema('import_node');
  _import_node_create_content_type();
  _import_node_insert_data();
}

/**
 * Implementation of hook_uninstall().
 *
 * Remove the variables, nodes and schema corresponding to the module.
 */
function import_node_uninstall() {
  drupal_uninstall_schema('import_node');
  _import_node_delete_data();
}

/**
 * Implementation of hook_schema().
 */
function import_node_schema() {
  $schema['import_example_node'] = array(
    'description' => t('Example table for a node import.'),
    'fields' => array(
      'id' => array(
        'description' => t('Primary key: ID'),
        'type' => 'serial',
      ),
      'headline' => array(
        'description' => t('A headline for this content.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'body' => array(
        'description' => t('The body of this content.'),
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
        'default' => '',
      ),
      'author' => array(
        'description' => t('The author of this content.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  return $schema;
}

/**
 * Helper function to install a cck content type via the content_copy module
 * @param array An array of content arrays as exported from the content_copy module
 */
function _import_node_install_cck($content_types) {
  foreach ($content_types as $content) {
    $form_state = array();
    $form = content_copy_import_form($form_state);
    $form_state['values']['type_name'] = $type_name ? $type_name : '<create>';
    $form_state['values']['macro'] = '$content = ' . var_export($content, 1) . ';';
    $form_state['values']['op'] = t('Import');

    // Call the submit function directly.
    // Using drupal_execute() leads to problems with the form_state when called
    // again within this submit function.
    content_copy_import_form_submit($form, $form_state);
  }
}
function _import_node_create_content_type() {
  $content_types = array();
  $content = array();
  $content['type'] = array(
    'name' => 'Example Node Import',
    'type' => 'example_node',
    'description' => 'A node for use in an example content import.',
    'title_label' => 'Title',
    'body_label' => 'Body',
    'min_word_count' => '0',
    'help' => '',
    'node_options' => array(
      'status' => true,
      'promote' => true,
      'sticky' => false,
      'revision' => false,
    ),
    'old_type' => 'example_node',
    'orig_type' => '',
    'module' => 'node',
    'custom' => '1',
    'modified' => '1',
    'locked' => '0',
    'signup_node_default_state' => 'disabled',
    'comment' => '2',
    'comment_default_mode' => '4',
    'comment_default_order' => '1',
    'comment_default_per_page' => '50',
    'comment_controls' => '3',
    'comment_anonymous' => 0,
    'comment_subject_field' => '1',
    'comment_preview' => '1',
    'comment_form_location' => '0',
  );
  $content['fields'] = array(
    0 => array(
      'label' => 'Original Author',
      'field_name' => 'field_original_author',
      'type' => 'text',
      'widget_type' => 'text_textfield',
      'change' => 'Change basic information',
      'weight' => '1',
      'rows' => 5,
      'size' => '60',
      'description' => 'The original author of this content.',
      'default_value' => array(
        0 => array(
          'value' => '',
          '_error_element' => 'default_value_widget][field_original_author][0][value',
        ),
      ),
      'default_value_php' => '',
      'default_value_widget' => NULL,
      'required' => 0,
      'multiple' => '0',
      'text_processing' => '0',
      'max_length' => '255',
      'allowed_values' => '',
      'allowed_values_php' => '',
      'op' => 'Save field settings',
      'module' => 'text',
      'widget_module' => 'text',
      'columns' => array(
        'value' => array(
          'type' => 'varchar',
          'length' => '255',
          'not null' => false,
          'sortable' => true,
          'views' => true,
        ),
      ),
      'display_settings' => array(
        'label' => array(
          'format' => 'above',
          'exclude' => 0,
        ),
        'teaser' => array(
          'format' => 'default',
          'exclude' => 0,
        ),
        'full' => array(
          'format' => 'default',
          'exclude' => 0,
        ),
        4 => array(
          'format' => 'default',
          'exclude' => 0,
        ),
      ),
    ),
  );
  $content['extra'] = array(
    'title' => '-5',
    'body_field' => '0',
    'menu' => '-2',
  );
  $content_types[] = $content;
  _import_node_install_cck($content_types);
}

/**
 * Helper function to delete a content type and all content of that type.
 */
function _import_node_delete_content_type($type) {
  $nodes = db_query("SELECT n.nid FROM {node} n WHERE n.type = '%s'", $type);
  while ($node = db_fetch_object($nodes)) {
    node_delete($node->nid);
  }
  node_type_delete($type);
}
function _import_node_insert_data() {

  // insert 100 random users to be imported
  _import_node_generate_random_content(10);
}
function _import_node_random_string() {
  return preg_replace("/([0-9])/e", "chr((\\1+112))", rand());
}

/**
 * Helper function to insert random users
 * @param
 *   $count integer The number of random users to generate
 * @param
 *   $fail boolean Indicates if account should be valid or invalid for purposes
 *   of testing import_pass and import_fail
 */
function _import_node_generate_random_content($count) {
  $sql_insert = "INSERT INTO {import_example_node} (headline, body, author) VALUES('%s', '%s', '%s')";
  while ($count > 0) {
    $headline = ucwords(_import_node_random_string() . ' ' . _import_node_random_string() . ' ' . _import_node_random_string());
    $body = '';
    $x = 500;
    while ($x > 0) {
      $body .= _import_node_random_string() . ' ';
      $x--;
    }
    $author = ucwords(_import_node_random_string() . ' ' . _import_node_random_string());
    db_query($sql_insert, $headline, $body, $author);
    $count--;
  }
}

/**
 * Helper function to clean up data created by this module
 */
function _import_node_delete_data() {
  _import_node_delete_content_type('example_node');
  db_query("DELETE FROM {import} WHERE type = '%s'", 'node');
  db_query("DELETE FROM {import_pass} WHERE type = '%s'", 'node');
  db_query("DELETE FROM {import_fail} WHERE type = '%s'", 'node');

  // remove the variable used for staging
  variable_del('import_node_staged');
}

Functions

Namesort descending Description
import_node_install Implementation of hook_install().
import_node_schema Implementation of hook_schema().
import_node_uninstall Implementation of hook_uninstall().
_import_node_create_content_type
_import_node_delete_content_type Helper function to delete a content type and all content of that type.
_import_node_delete_data Helper function to clean up data created by this module
_import_node_generate_random_content Helper function to insert random users
_import_node_insert_data
_import_node_install_cck Helper function to install a cck content type via the content_copy module
_import_node_random_string