function twitter_post_entity_insert in Twitter 7.6
Implementation of hook_entity_insert().
Intercepts newly created entities and posts notices to Twitter.
1 call to twitter_post_entity_insert()
- twitter_post_entity_update in twitter_post/
twitter_post.module - Implementation of hook_entity_update().
File
- twitter_post/
twitter_post.module, line 32 - Hook implementations for twitter_post module.
Code
function twitter_post_entity_insert($entity, $entity_type) {
// First we find twitter_post fields.
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$fields_info = field_info_instances($entity_type, $bundle);
foreach ($fields_info as $field_name => $value) {
$field_info = field_info_field($field_name);
if ($field_info['type'] == 'twitter_post') {
// Iterate field instances.
$items = field_get_items($entity_type, $entity, $field_name);
if (empty($items)) {
continue;
}
foreach ($items as $delta => $field_instance) {
if (is_int($delta) && !empty($field_instance['account'])) {
// Extract data out of each field.
$status = $field_instance['status'];
$message = $field_instance['message'];
$twitter_account = entity_load_single('twitter_account', $field_instance['account']);
global $user;
$account = user_load($user->uid);
$access_global = user_access('post to twitter with global account', $account);
// Only allow the tweet to be posted if the Twitter account is either
// a global account and the user has access to global accounts, or it
// is tied to the current user.
if (!($twitter_account->is_global && $access_global || $twitter_account->uid == $account->uid)) {
return;
}
// Post to Twitter if the status checkbox is active.
if ($status) {
module_load_include('inc', 'twitter');
$message = token_replace($message, array(
$entity_type => $entity,
));
$status = twitter_set_status($twitter_account, $message);
if ($status) {
drupal_set_message(t('Successfully posted "%node" to Twitter: <a href="@status" target="_blank">@status</a>', array(
'%node' => isset($entity->title) ? $entity->title : '',
'@status' => _twitter_status_url($status),
)));
}
else {
drupal_set_message(t('An empty status was returned. Check the system log for details.'), 'error');
}
}
}
}
}
}
}