ayathullah 0 Newbie Poster

Hi,

I'm using Drupal6 with the twitter module.

How do you set up a rule to post a message to the current user's Twitter account? At the moment, the form in the "Post a message to Twitter" action only permits a absolute value. I want to use a token for the current user's Twitter account.


Obviously this will require some customisations to the module.
It seems that Drupal doesnt allow the use of tokens on textfields.


I'd like to expose tokens to a the text field.
I've managed to track down the page which actaully applies this action.

Any help with customising the code below would be helpful.

// $Id: twitter_actions.module,v 1.2.2.3 2010/10/20 15[s]59[/s]11 walkah Exp $

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

/**
 * Implementation of hook_action_info().
 */
function twitter_actions_action_info() {
  return array(
    'twitter_actions_set_status_action' => array(
      'type' => 'system',
      'description' => t('Post a message to Twitter'),
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array('view', 'insert', 'update', 'delete'),
        'comment' => array('view', 'insert', 'update', 'delete'),
        'user' => array('view', 'insert', 'update', 'delete', 'login'),
        'cron' => array('run'),
      ),
    ),
  );
}

/**
 * Return a form definition so the Send email 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 = array()) {
  // Set default values for form.
  $context += array(
    'account_id' => -1,
    'screen_name' => '',
    'message' => '',
  );

  $form['screen_name'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Twitter account name'),
    '#default_value' => $context['screen_name'],
    '#size'          => 25,
    '#required'      => TRUE,
  );

  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#default_value' => $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, %teaser, %body,  %tinyurl. Not all variables will be available in all contexts.'),
    '#required'      => TRUE,
  );
  return $form;
}

function twitter_actions_set_status_action_validate($form, $form_state) {
  if (!_twitter_use_oauth()) {
    form_set_error('screen_name', t('Oath has not yet been setup.'));
  }
  if (!db_result(db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = '%s'", $form_state['values']['screen_name']))) {
    form_set_error('screen_name', t('Twitter authentication failed. Please check your account name and try again.'));
  }
}

function twitter_actions_set_status_action_submit($form, $form_state) {
  $form_values = $form_state['values'];
  $twitter_uid = db_result(db_query("SELECT twitter_uid FROM {twitter_account} WHERE screen_name = '%s'", $form_values['screen_name']));
  // 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 Drupal action.
 * Sends an email.
 */
function twitter_actions_set_status_action($object, $context) {
  global $user;
  $variables['%site_name'] = variable_get('site_name', 'Drupal');

  switch ($context['hook']) {
    case 'nodeapi':
    case 'workflow':
      // Because this is not an action of type 'node' the node
      // will not be passed as $object, but it will still be available
      // in $context.
      $node = $context['node'];
      break;
    // The comment hook provides nid, in $context.
    case 'comment':
      $comment = $context['comment'];
      $node = node_load($comment->nid);
    case 'user':
      // Because this is not an action of type 'user' the user
      // object is not passed as $object, but it will still be available
      // in $context.
      $account = $context['account'];
      if (isset($context['node'])) {
        $node = $context['node'];
      }
      elseif ($context['recipient'] == '%author') {
        // If we don't have a node, we don't have a node author.
        watchdog('error', 'Cannot use %author token in this context.');
        return;
      }
      break;
    case 'taxonomy':
      $account = $user;
      $vocabulary = taxonomy_vocabulary_load($object->vid);
      $variables = array_merge($variables, array(
        '%term_name' => $object->name,
        '%term_description' => $object->description,
        '%term_id' => $object->tid,
        '%vocabulary_name' => $vocabulary->name,
        '%vocabulary_description' => $vocabulary->description,
        '%vocabulary_id' => $vocabulary->vid,
        )
      );
      break;
    default:
      // We are being called directly.
      $node = $object;
  }

  if (isset($node)) {
    if (!isset($account)) {
      $account = user_load(array('uid' => $node->uid));
    }
    if ($recipient == '%author') {
      $recipient = $account->mail;
    }
  }

  $variables['%username'] = $account->name;

  // Node-based variable translation is only available if we have a node.
  if (isset($node) && is_object($node)) {
    $variables = array_merge($variables, array(
        '%uid' => $node->uid,
        '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)),
        '%node_type' => node_get_types('name', $node),
        '%title' => $node->title,
        '%teaser' => $node->teaser,
        '%body' => $node->body
      )
    );
  }

  // 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);
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.