You are here

twitter_post.install in Twitter 7.6

Install, update and uninstall functions for the twitter module.

File

twitter_post/twitter_post.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the twitter module.
 */

/**
 * Implement hook_field_schema().
 */
function twitter_post_field_schema($field) {
  $columns = array();
  $indexes = array();
  if ($field['type'] == 'twitter_post') {
    $columns = array(
      'status' => array(
        'description' => 'Control whether or not this entity will be tweeted.',
        'type' => 'int',
        'length' => 1,
        'not null' => TRUE,
        'size' => 'tiny',
        'default' => 0,
      ),
      'message' => array(
        'description' => "The text of the Twitter post.",
        // Using a blob instead of a text type make it possible for MySQL to
        // handle extended UTF8 characters, like emoji.
        // @see https://www.drupal.org/node/1910376
        'type' => 'blob',
        // Balance size vs performance. The August 2015 update allows for DMs
        // that are 10,000 characters in length, so in theory MySQL's default
        // blob length of 16KB should be enough.
        'size' => 'normal',
        'not null' => FALSE,
      ),
      'account' => array(
        'description' => "Unique identifier for the {twitter_account} this tweet is posting from to.",
        'type' => 'int',
        'unsigned' => TRUE,
        'size' => 'big',
        'not null' => FALSE,
        'default' => 0,
      ),
    );
    $indexes = array();
  }
  return array(
    'columns' => $columns,
    'indexes' => $indexes,
  );
}

/**
 * Implements hook_install().
 */
function twitter_post_install() {
}

/**
 * Implements hook_enable().
 */
function twitter_post_enable() {
  drupal_set_message('Twitter Post field enabled. Edit any entity (such as Page, User or Comment) and add a Twitter field to it.');
}

/**
 * Implements hook_uninstall().
 */
function twitter_post_uninstall() {
}

/**
 * Don't default to TinyURL any more.
 */
function twitter_post_update_7300() {
  if (variable_get('twitter_post_default_format', NULL) == 'New post: !title !tinyurl') {
    variable_set('twitter_post_default_format', "New post: !title !url-alias");
  }
  return t('Twitter Post now defaults to using aliases, not TinyURL.');
}

/**
 * Implements hook_update_N().
 *
 * Increase module weight to run after pathauto.
 */
function twitter_post_update_7301() {
  db_update('system')
    ->fields(array(
    'weight' => 10,
  ))
    ->condition('name', 'twitter_post', '=')
    ->execute();
  return t('Updated system weight to 10 for Twitter Post.');
}

/**
 * Add the new "post to twitter with global account" permission to all roles
 * that have the "post to twitter" permission.
 */
function twitter_post_update_7500() {
  $roles = db_query("SELECT rid\n    FROM {role_permission}\n    WHERE permission = 'post to twitter'")
    ->fetchCol();
  foreach ($roles as $rid) {
    $record = new StdClass();
    $record->rid = $rid;
    $record->permission = 'post to twitter with global account';
    $record->module = 'twitter';
    drupal_write_record('role_permission', $record);
  }
  drupal_set_message(t('A new permission "Post a message to Twitter using a global account" as been added to all roles that already had the "Post a message to Twitter" permission. It is recommended to <a href="@url">review the permissions</a> to ensure they are appropriate for this site\'s needs.', array(
    '@url' => url('admin/people/permissions'),
  )));
}

/**
 * Add the missing 'status' field.
 */
function twitter_post_update_7501() {

  // Get the list of fields.
  $types = array(
    'twitter_post',
  );
  $fields = array();
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $types)) {
      $fields[] = $field;
    }
  }
  if (!empty($fields)) {
    $spec = array(
      'description' => 'Control whether or not this entity will be tweeted.',
      'type' => 'int',
      'length' => 1,
      'not null' => TRUE,
      'size' => 'tiny',
      'default' => 0,
    );
    foreach ($fields as $field) {
      $tables = array(
        _field_sql_storage_tablename($field),
        _field_sql_storage_revision_tablename($field),
      );
      foreach ($tables as $table) {
        $column = $field['field_name'] . '_status';
        if (db_field_exists($table, $column)) {
          db_change_field($table, $column, $column, $spec);
        }
        else {
          db_add_field($table, $column, $spec);
        }
      }
    }
    return t('Added the missing "account" field.');
  }
}

/**
 * Change the 'message' field to a BLOB.
 */
function twitter_post_update_7502() {

  // Get the list of fields.
  $types = array(
    'twitter_post',
  );
  $fields = array();
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $types)) {
      $fields[] = $field;
    }
  }
  if (!empty($fields)) {
    $spec = array(
      'description' => "The text of the Twitter post.",
      // Using a blob instead of a text type make it possible for MySQL to
      // handle extended UTF8 characters, like emoji.
      // @see https://www.drupal.org/node/1910376
      'type' => 'blob',
      // Balance size vs performance. The August 2015 update allows for DMs
      // that are 10,000 characters in length, so in theory MySQL's default
      // blob length of 16KB should be enough.
      'size' => 'normal',
      'not null' => FALSE,
    );
    foreach ($fields as $field) {
      $tables = array(
        _field_sql_storage_tablename($field),
        _field_sql_storage_revision_tablename($field),
      );
      foreach ($tables as $table) {
        $column = $field['field_name'] . '_message';
        if (db_field_exists($table, $column)) {
          db_change_field($table, $column, $column, $spec);
        }
        else {
          db_add_field($table, $column, $spec);
        }
      }
    }
    return t('Converted the "message" field to BLOB.');
  }
}

/**
 * Add the missing 'account' field.
 */
function twitter_post_update_7503() {

  // Get the list of fields.
  $types = array(
    'twitter_post',
  );
  $fields = array();
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $types)) {
      $fields[] = $field;
    }
  }
  if (!empty($fields)) {
    $spec = array(
      'description' => "Unique identifier for the {twitter_account} this tweet is posting from to.",
      'type' => 'int',
      'unsigned' => TRUE,
      'size' => 'big',
      'not null' => FALSE,
      'default' => 0,
    );
    foreach ($fields as $field) {
      $tables = array(
        _field_sql_storage_tablename($field),
        _field_sql_storage_revision_tablename($field),
      );
      foreach ($tables as $table) {
        $column = $field['field_name'] . '_account';
        db_add_field($table, $column, $spec);
      }
    }
    return t('Added the missing "account" field.');
  }
}

Functions

Namesort descending Description
twitter_post_enable Implements hook_enable().
twitter_post_field_schema Implement hook_field_schema().
twitter_post_install Implements hook_install().
twitter_post_uninstall Implements hook_uninstall().
twitter_post_update_7300 Don't default to TinyURL any more.
twitter_post_update_7301 Implements hook_update_N().
twitter_post_update_7500 Add the new "post to twitter with global account" permission to all roles that have the "post to twitter" permission.
twitter_post_update_7501 Add the missing 'status' field.
twitter_post_update_7502 Change the 'message' field to a BLOB.
twitter_post_update_7503 Add the missing 'account' field.