You are here

twitter_actions.module in Twitter 7.4

Exposes Drupal actions for sending Twitter messages.

File

twitter_actions/twitter_actions.module
View source
<?php

/**
 * @file
 * Exposes Drupal actions for sending Twitter messages.
 */

/**
 * Implements hook_action_info().
 */
function twitter_actions_action_info() {
  return array(
    'twitter_actions_set_status_action' => array(
      'type' => 'system',
      'label' => t('Post a message to Twitter'),
      'configurable' => TRUE,
      'triggers' => array(
        'node_view',
        'node_insert',
        'node_update',
        'node_delete',
        'comment_view',
        'comment_insert',
        'comment_update',
        'comment_delete',
        'user_view',
        'user_insert',
        'user_update',
        'user_delete',
        'user_login',
        'cron',
      ),
    ),
  );
}

/**
 * Returns a form definition so the Twitter action can be configured.
 *
 * @param $context
 *   Default values (if we are editing an existing action instance).
 * @return
 *   Form definition.
 */
function twitter_actions_set_status_action_form($context) {
  $options = array();
  $results = db_query("SELECT screen_name FROM {twitter_account}");
  foreach ($results as $result) {
    $options[$result->screen_name] = $result->screen_name;
  }

  // Set default values for form.
  $form['screen_name'] = array(
    '#type' => 'select',
    '#title' => t('Twitter account name'),
    '#options' => $options,
    '#default_value' => isset($context['screen_name']) ? $context['screen_name'] : '',
    '#required' => TRUE,
  );
  if (!count($options)) {
    $form['screen_name']['#description'] = t('You first need to add a Twitter account to one of ' . 'your users with rights for posting to Twitter.');
  }
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#default_value' => isset($context['message']) ? $context['message'] : '',
    '#cols' => '80',
    '#rows' => '3',
    '#description' => t('The message that should be sent. You may include the following variables: ' . '%site_name, %username, %node_url, %node_type, %title, %summary, %body, ' . '%tinyurl. Not all variables will be available in all contexts.'),
    '#required' => TRUE,
  );
  return $form;
}

/**
 * Submits the form and sets the twitter account pulling the data from the
 * twitter_account table.
 */
function twitter_actions_set_status_action_submit($form, $form_state) {
  $form_values = $form_state['values'];
  $twitter_uid = db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = :screen_name", array(
    ':screen_name' => $form_values['screen_name'],
  ))
    ->fetchField();

  // Process the HTML form to store configuration. The keyed array that
  // we return will be serialized to the database.
  $params = array(
    'twitter_uid' => $twitter_uid,
    'screen_name' => $form_values['screen_name'],
    'message' => $form_values['message'],
  );
  return $params;
}

/**
 * Implementation of a configurable Twitter action.
 * @todo Implementation for language negotiation for the body and sumary. Also
 * need implementation for bodies with multiple values. Right now it is hard
 * coded and it will only get body and summary for 'und' language and only
 * the first value of the body field.
 * If the final message is over 140 chars, there is no feedback to the user.
 */
function twitter_actions_set_status_action($object, $context) {
  global $user;
  $variables['%site_name'] = variable_get('site_name', 'Drupal');

  // Seting variables array depending on action's group
  switch ($context['group']) {
    case 'node':
      $node = $context['node'];
      if (isset($node)) {
        $variables = array_merge($variables, array(
          '%uid' => $node->uid,
          '%username' => $node->name,
          '%node_url' => url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          '%node_type' => node_type_get_name($node),
          '%title' => $node->title,
          '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
          '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
        ));
      }
      break;
    case 'comment':
      $node = node_load($context['comment']->nid);
      if (isset($node)) {
        $variables = array_merge($variables, array(
          '%uid' => $context['comment']->uid,
          '%username' => $context['comment']->name,
          '%node_url' => url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          '%node_type' => node_type_get_name($node),
          '%title' => $node->title,
          '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
          '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
        ));
      }
      break;
    case 'user':
      $variables['%username'] = $context['user']->name;
      break;
    case 'cron':
      break;
    default:

      // We are being called directly.
      $node = $object;
      if (isset($node) && is_object($node)) {
        $variables = array_merge($variables, array(
          '%uid' => $node->uid,
          '%username' => $node->name,
          '%node_url' => url('node/' . $node->nid, array(
            'absolute' => TRUE,
          )),
          '%node_type' => node_type_get_name($node),
          '%title' => $node->title,
          '%summary' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['summary'] : '',
          '%body' => isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '',
        ));
      }
  }

  // Only make a tinyurl if it was asked for.
  if (strstr($context['message'], '%tinyurl') !== FALSE) {
    $variables = array_merge($variables, array(
      '%tinyurl' => twitter_shorten_url(url('node/' . $node->nid, array(
        'absolute' => TRUE,
      ))),
    ));
  }
  $message = strtr($context['message'], $variables);
  module_load_include('inc', 'twitter');
  $twitter_account = twitter_account_load($context['twitter_uid']);
  twitter_set_status($twitter_account, $message);
}

Functions

Namesort descending Description
twitter_actions_action_info Implements hook_action_info().
twitter_actions_set_status_action Implementation of a configurable Twitter action. @todo Implementation for language negotiation for the body and sumary. Also need implementation for bodies with multiple values. Right now it is hard coded and it will only get body and summary for…
twitter_actions_set_status_action_form Returns a form definition so the Twitter action can be configured.
twitter_actions_set_status_action_submit Submits the form and sets the twitter account pulling the data from the twitter_account table.