SocialHub Post examples

Last updated on
30 April 2025

Creating a Post that is send to SocialHub

$post = new SocialHubPost();
$post_wrapper = entity_metadata_wrapper('socialhub_post', $post);

// Required properties.
$post_wrapper->created->set(strtotime('now'));

// Mark this post as published and enqueue it to be send to SocialHub.
$post_wrapper->status->set(SocialHubPost::POST_TO_PUBLISH);

// Optional properties: Publishing filters.
// Note that each of these must be valid and existing configuration id's.
$post_wrapper->channels->set(array('channel-id1', 'channel-id2'));
$post_wrapper->posttypes->set(array('posttype-id1', 'posttype-id2'));
$post_wrapper->networktargets->set(array('network-id1', 'network-id2'));

// Optional scheduling.
$post_wrapper->scheduled->set(strtotime('tomorrow'));

// Optional advanced properties.
$post_wrapper->fail_on->set(SocialHubPost::FAIL_ON_WARNING);
$post_wrapper->strict->set(FALSE);

// The actual post contents, the payload.
$post_wrapper->payload->set(array('content field' => array('values' => array('testpost'))));

// Save this post (This actually triggers the API request).
$post_wrapper->save();

Altering a Post that is send to SocialHub

/**
 * Implements hook_socialhub_post_presave().
 */
function MODULE_socialhub_post_presave(SocialHubPost $post, $source) {
  if ($source == SocialHubEntityController::INTERNAL && $post->status == SocialHubPost::POST_TO_PUBLISH) {
    ...
  }
}

Reacting on an updated post status

/**
 * Implements hook_socialhub_post_presave().
 */
function MODULE_socialhub_post_update(SocialHubPost $post, $source) {
  if ($source == SocialHubEntityController::EXTERNAL && $post->status != $post->original->status) {
    ...
  }
}

Deleting an existing post

$post = socialhub_post_load($id);
$post->delete();

Getting API-corresponding representation of an entity

$post = socialhub_post_load($id);
$api_data = $post->getApiData();

Setting API-corresponding representation on an entity

$api_data = array(
  'postId' => 'some-unique-post-id',
  'scheduled' => '2005-08-15T15:52:01+0000',
  'payload' => array(
    array('some-random-payload')
  ),
);
$post = new SocialHubPost();
$post->setApiData($api_data);
$post->save();

Using SocialHub reference with an article

$post = new SocialHubPost();
$post_wrapper = entity_metadata_wrapper('socialhub_post', $post);
$post_wrapper->created->set(strtotime('now'));
$post_wrapper->status->set(SocialHubPost::POST_TO_PUBLISH);

// Set the referenced node, either by the node object or the node's id.
$post_wrapper->socialhub_reference_node->set($node);
// Set the fields of the referenced node to add to the payload when generating
// API-corresponding representation of this entity.
$post_wrapper->reference_fields->set(array('title', 'body', 'field_image'));

$post_wrapper->save();

Loading all posts referenced to a node

$posts = socialhub_reference_posts($node);

Help improve this page

Page status: Not set

You can: