You are here

social_content_twitter.install in Social Content 7

Same filename and directory in other branches
  1. 7.2 modules/twitter/social_content_twitter.install

Install/uninstall code for Social Content: Twitter.

File

modules/twitter/social_content_twitter.install
View source
<?php

/**
 * @file
 * Install/uninstall code for Social Content: Twitter.
 */

/**
 * Implements hook_enable().
 */
function social_content_twitter_enable() {
  module_load_include('inc', 'social_content_twitter', 'social_content_twitter.filters');
  $tweet_format = array(
    'format' => 'tweet',
    'name' => 'Tweet',
    'status' => 1,
  );
  foreach (social_content_twitter_filter_info() as $name => $filter) {
    $tweet_format['filters'][$name]['status'] = 1;
  }
  $tweet_format = (object) $tweet_format;
  filter_format_save($tweet_format);
}

/**
 * Implements hook_install().
 */
function social_content_twitter_install() {
  $t = get_t();

  // 1) Create and save a new content object.
  // Machine name of the content type.
  $name = 'tweet';

  // define the node type
  $tweet = array(
    'type' => $name,
    'name' => $t('Tweet'),
    'base' => 'node_content',
    'title_label' => $t('Title'),
    'description' => $t('A tweet imported from Twitter.'),
    'custom' => TRUE,
  );

  // Set other node defaults not declared above.
  $content_type = node_type_set_defaults($tweet);

  // Add the body field.
  node_add_body_field($content_type, $t('Body'));

  // Save the content type.
  node_type_save($content_type);

  // 2) Update persistant variables with settings.
  // Add persistent variables that control settings.
  variable_set('additional_settings__active_tab_' . $name, 'edit-menu');
  variable_set('node_preview_' . $name, 0);

  // 0 = disabled, 1 = optional, 2 = required
  variable_set('node_options_' . $name, array(
    0 => 'status',
  ));

  // array(0 => 'status', 1 => 'promote', 2 => 'sticky', 3 => 'revision') remove to uncheck
  variable_set('node_submitted_' . $name, 0);

  // 1 = Display author and date information, 0 = none
  variable_set('menu_options_' . $name, array());
  variable_set('menu_parent_' . $name, 'main-menu:0');

  // 3) Create and add instances of new fields.
  // Create all the fields we are adding to our content type.
  module_load_include('inc', 'social_content_twitter', 'social_content_twitter.fields');
  foreach (social_content_twitter_create_fields() as $field) {
    field_create_field($field);
  }

  // Create all the instances for our fields.
  foreach (social_content_twitter_create_instances() as $instance) {
    $instance['entity_type'] = 'node';
    $instance['bundle'] = $tweet['type'];
    field_create_instance($instance);
  }
}

/**
 * Implements hook_uninstall().
 *
 * Remove variables and content type.
 */
function social_content_twitter_uninstall() {

  // Remove variables.
  variable_del('social_content_twitter_account');
  variable_del('social_content_twitter_hashtag');

  // Machine name of the content type.
  $name = 'tweet';

  // Gather all tweet nodes created.
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(
    ':type' => $name,
  ));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }

  // Delete all the tweet nodes at once.
  node_delete_multiple($nids);

  // Remove persistant variables that control settings.
  variable_del('additional_settings__active_tab_' . $name);
  variable_del('node_preview_' . $name);
  variable_del('node_options_' . $name);
  variable_del('node_submitted_' . $name);
  variable_del('menu_options_' . $name);
  variable_del('menu_parent_' . $name);

  // Delete our content type.
  node_type_delete($name);

  // Find all fields and delete them.
  module_load_include('inc', 'social_content_twitter', 'social_content_twitter.fields');
  foreach (array_keys(social_content_twitter_create_fields()) as $field) {
    field_delete_field($field);
  }

  // Find all fields and delete instance.
  $instances = field_info_instances('node', $name);
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }

  // Purge all field info.
  field_purge_batch(1000);
}