first import
This commit is contained in:
12
sites/all/modules/twitter/twitter_post/twitter_post.info
Normal file
12
sites/all/modules/twitter/twitter_post/twitter_post.info
Normal file
@@ -0,0 +1,12 @@
|
||||
name = Twitter Post
|
||||
description = Enables posting to twitter
|
||||
core = 7.x
|
||||
dependencies[] = twitter
|
||||
dependencies[] = oauth_common
|
||||
|
||||
; Information added by drupal.org packaging script on 2011-12-03
|
||||
version = "7.x-3.0-beta4"
|
||||
core = "7.x"
|
||||
project = "twitter"
|
||||
datestamp = "1322940643"
|
||||
|
33
sites/all/modules/twitter/twitter_post/twitter_post.js
Normal file
33
sites/all/modules/twitter/twitter_post/twitter_post.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Attach handlers to toggle the twitter message field and inform the number
|
||||
* of characters remaining to achieve the max length
|
||||
*/
|
||||
(function ($) {
|
||||
Drupal.behaviors.twitter_post = {
|
||||
attach: function (context, settings) {
|
||||
$("#twitter-textfield", context).keyup(function() {
|
||||
var charsLeft = (140 - $(this).val().length);
|
||||
var descDiv = $(this).next();
|
||||
$(descDiv).html("<strong>" + charsLeft + "</strong> characters remaining");
|
||||
if (charsLeft < 0) {
|
||||
$(descDiv).addClass("negative");
|
||||
} else {
|
||||
$(descDiv).removeClass("negative");
|
||||
}
|
||||
});
|
||||
|
||||
if (!$("#twitter-toggle").attr("checked")) {
|
||||
$(".form-item-twitter-status").hide();
|
||||
}
|
||||
|
||||
$("#twitter-toggle").bind("click", function() {
|
||||
if ($("#twitter-toggle").attr("checked")) {
|
||||
$(".form-item-twitter-status").show();
|
||||
}
|
||||
else {
|
||||
$(".form-item-twitter-status").hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}(jQuery));
|
164
sites/all/modules/twitter/twitter_post/twitter_post.module
Normal file
164
sites/all/modules/twitter/twitter_post/twitter_post.module
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Main hooks for twitter post module
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_menu().
|
||||
*/
|
||||
function twitter_post_menu() {
|
||||
$items['admin/config/services/twitter/post'] = array(
|
||||
'title' => 'Post',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('twitter_post_admin_settings'),
|
||||
'access arguments' => array('administer site configuration'),
|
||||
'file' => 'twitter_post.pages.inc',
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
'weight' => 3,
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_permission().
|
||||
*/
|
||||
function twitter_post_permission() {
|
||||
return array(
|
||||
'post to twitter' => array(
|
||||
'title' => t('Post a message to Twitter'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_alter().
|
||||
*/
|
||||
function twitter_post_form_alter(&$form, $form_state, $form_id) {
|
||||
// Alter any node forms.
|
||||
if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {
|
||||
// If we haven't enabled Twitter posting on this node type, nothing to do
|
||||
// here.
|
||||
$type = $form['#node']->type;
|
||||
$allowed_types = variable_get('twitter_post_types', array('story' => 'story', 'blog' => 'blog'));
|
||||
if (empty($allowed_types[$type])) {
|
||||
return;
|
||||
}
|
||||
|
||||
module_load_include('inc', 'twitter');
|
||||
|
||||
$twitter_form = twitter_post_form();
|
||||
if (!$twitter_form) {
|
||||
return;
|
||||
}
|
||||
$form['twitter'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('Post to twitter.com'),
|
||||
'#collapsible' => TRUE,
|
||||
'#collapsed' => FALSE,
|
||||
'#tree' => TRUE,
|
||||
);
|
||||
$form['twitter']['post'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Announce this post on Twitter'),
|
||||
'#default_value' => (empty($form['nid']['#value'])),
|
||||
'#id' => 'twitter-toggle',
|
||||
);
|
||||
$form['twitter'] += $twitter_form;
|
||||
$form['twitter']['status']['#default_value'] = variable_get('twitter_post_default_format', 'New post: !title !tinyurl');
|
||||
$form['twitter']['status']['#description'] = t('The given text will be posted to twitter.com. You can use !url, !url-alias, !tinyurl, !title and !user as replacement text.');
|
||||
$form['twitter']['status']['#maxlength'] = 150;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_node_insert().
|
||||
*
|
||||
* Intercepts newly published nodes and posts noticed to Twitter.
|
||||
*/
|
||||
function twitter_post_node_insert($node) {
|
||||
if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post'])) {
|
||||
module_load_include('inc', 'twitter');
|
||||
|
||||
$twitter_account = twitter_account_load($node->twitter['account']);
|
||||
$replacements = array(
|
||||
'!title' => $node->title,
|
||||
'!url' => url('node/' . $node->nid, array('absolute' => TRUE, 'alias' => TRUE)),
|
||||
'!url-alias' => url('node/' . $node->nid, array('absolute' => TRUE)),
|
||||
'!user' => $node->name,
|
||||
);
|
||||
|
||||
// Only generate the shortened URL if it's going to be used. No sense
|
||||
// burning through TinyURLs without a good reason.
|
||||
if (strstr($node->twitter['status'], '!tinyurl') !== FALSE) {
|
||||
$replacements['!tinyurl'] = twitter_shorten_url(url('node/' . $node->nid, array('absolute' => TRUE)));
|
||||
}
|
||||
|
||||
$status = strtr($node->twitter['status'], $replacements);
|
||||
try {
|
||||
$result = twitter_set_status($twitter_account, $status);
|
||||
drupal_set_message(t('Successfully posted to Twitter'));
|
||||
}
|
||||
catch (TwitterException $e) {
|
||||
drupal_set_message(t('An error occurred when posting to twitter: %code %error',
|
||||
array('%code' => $result->code, '%error' => $result->error)), 'warning');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_node_update().
|
||||
*/
|
||||
function twitter_post_node_update($node) {
|
||||
twitter_post_node_insert($node);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate a twitter posting form for the given user.
|
||||
*
|
||||
* @param $account
|
||||
* A Drupal user object.
|
||||
*/
|
||||
function twitter_post_form($account = NULL) {
|
||||
drupal_add_js(drupal_get_path('module', 'twitter_post') . '/twitter_post.js');
|
||||
|
||||
if (empty($account)) {
|
||||
$account = user_load($GLOBALS['user']->uid);
|
||||
}
|
||||
|
||||
if (!user_access('post to twitter', $account)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$options = array();
|
||||
foreach ($account->twitter_accounts as $twitter_account) {
|
||||
$options[$twitter_account->id] = $twitter_account->screen_name;
|
||||
}
|
||||
|
||||
if (count($options)) {
|
||||
$form = array();
|
||||
$form['status'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#id' => 'twitter-textfield',
|
||||
);
|
||||
|
||||
if (count($options) > 1) {
|
||||
$form['account'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Account'),
|
||||
'#options' => $options,
|
||||
'#id' => 'twitter-account',
|
||||
);
|
||||
}
|
||||
else {
|
||||
$form['account'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => array_pop(array_keys($options)),
|
||||
);
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Settings form callback
|
||||
*/
|
||||
function twitter_post_admin_settings($form, &$form_state) {
|
||||
$form['twitter_post_types'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('Node types'),
|
||||
'#options' => node_type_get_names(),
|
||||
'#default_value' => variable_get('twitter_post_types', array('story' => 'story', 'blog' => 'blog')),
|
||||
);
|
||||
|
||||
$form['twitter_post_default_format'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Default format string'),
|
||||
'#maxlength' => 140,
|
||||
'#description' => t('The given text will be posted to twitter.com. You can use !url, !url-alias, !tinyurl, !title, and !user as replacement text.'),
|
||||
'#default_value' => variable_get('twitter_post_default_format', 'New post: !title !tinyurl'),
|
||||
);
|
||||
|
||||
return system_settings_form($form);
|
||||
}
|
Reference in New Issue
Block a user