Realtidbits Comment integration with Drupal

Realtidbits

After spending good amount of time integrating Disqus with SSO on my Drupal site for comments, my search begins to find a better commenting system which is easy to integrate. My search ends here.

The developer documentation here is awesome and provides all the steps and even code to include the commenting system in any site. The catch for writing this blog was to integrate it in Drupal and to make it available as a Drupal module.

I have added a new custom module to my site to integrate the comments on my pages.

Adding a Drupal custom module

Step 1:
Add a custom module say “realtidbits” to your site at sites/all/modules/custom/realtidbits.
Add a file as realtidbits.info as

name = Realtidbits
description = "Forget everything you think you know about a comments company. Try Realtidbits"
package = MNN
version = VERSION
core = 7.x
php = 5.3
files[] = realtidbits.module
files[] = realtidbits.admin.inc
configure = admin/config/services/realtidbits

Step 2:
Add a realtidbits.module file and implement following hooks:
A) Hook Permission to add a new site permission for comments administration.

/**
 * Implements hook_permission().
 */
function realtidbits_permission() {
 return array(
 'administer realtidbits' => array(
 'title' => t('Administer Realtidbits'),
 'description' => t('Perform administrative actions with Realtidbits.'),
 ),
 );
}

B) Hook menu to make a settings page under site configuration “Web services” section.

/**
 * Implements hook_menu()
 */
function realtidbits_menu() {
 $items = array();
 $items['admin/config/services/realtidbits'] = array(
 'title' => 'Realtidbits',
 'description' => 'Provides configuration options for the realtidbits comment system.',
 'access arguments' => array('administer realtidbits'),
 'page callback' => 'drupal_get_form',
 'page arguments' => array('realtidbits_admin_settings'),
 'file' => 'realtidbits.admin.inc',
 );
 return $items;
}

C) Hook block info to Add a custom block for comments.

/**
 * Implements hook_block_info()
 */
function realtidbits_block_info() {
 $blocks = array();
 $blocks['realtidbits_js_code'] = array(
 'info' => t('Realtidbits Comments'),
 );
 return $blocks;
}

D) Hook Block Configure to configure the custom block options.

/**
 * Implements hook_block_configure().
 */
function realtidbits_block_configure($delta='') {
 $form = array();
 switch($delta) {
 case 'realtidbits_js_code' :
 // Text field form element
 $form['realtidbits_comments_js_code_field'] = array(
 '#type' => 'textarea',
 '#cols' => 20,
 '#rows' => 5,
 '#resizable' => TRUE,
 '#title' => t('Include the following javascript code on every page you’d like to display comments anywhere on the html page replacing the ALL CAPS elements with your own credentials'),
 '#default_value' => variable_get('realtidbits_comments_js_code', ''),
 );
 break;
 }
 return $form;
}

E) Hook Block Save to save the custom comments block.

/**
 * Implements hook_block_save().
 */
function realtidbits_block_save($delta = '', $edit = array()) {
 switch($delta) {
 case 'realtidbits_js_code' :
 variable_set('realtidbits_comments', $edit['realtidbits_comments_js_code_field']['value']);
 break;
 }
}

F) Hook Block View to show the comments in action.

/**
 * Implements hook_block_view().
 */
function realtidbits_block_view($delta='') {
 $block = array();
 $js_code = 'WILL BE EXPLAINED IN NEXT SECTION ';
 switch($delta) {
 case 'realtidbits_js_code' :
 $block['content'] = $js_code;
 break;
 }
 return $block;
}

Displaying the Realtidbits comments on the site
Integrate the custom block created as above on all the pages where you want to display the comments using any of block integration method as below or add the pages to display the block in block configuration settings.

// @params module, block_view, delta
$block = module_invoke('realtidbits', 'block_view', 'realtidbits_js_code');
print render($block);

Register your site on Realtidbits
Once you register your site with realtidbits, you will get an email with your account credentials and an installation link to grant your login credentials administrator status.. Click on it and then follow the instructions onscreen to grant your login credentials administrator status.

Configure realtidbits account credentials
We will add a settings form to our module to allow administrator easily add the realtidbits account credentials on the site. Add a file as realtidbits.admin.inc add add the below function to define a settings form and save the realtidbits account credentials as drupal variables.

/**
 * Menu callback; Displays the administration settings for Realtidbits.
 */
function realtidbits_admin_settings() {
 $form = array();
 $form['realtidbits_domain'] = array(
 '#type' => 'textfield',
 '#title' => t('Shortname'),
 '#description' => t('The website shortname that you registered Realtidbits with. If you registered http://example.realtidbits.com, you would enter "example" here.'),
 '#default_value' => variable_get('realtidbits_domain', ''),
 );
 $form['settings'] = array(
 '#type' => 'vertical_tabs',
 '#weight' => 50,
 );
 // Advanced settings.
 $form['advanced'] = array(
 '#type' => 'fieldset',
 '#title' => t('Advanced'),
 '#group' => 'settings',
 '#description' => t('Use these settings to configure the more advanced uses of Realtidbits. You can find more information about these in the <a href="@installation">Installation</a> section of Realtidbits. To read the <a href="@documentation">Documentation</a>.', array(
 '@installation' => 'http://documentation.realtidbits.com/Comments_V3/Getting_Started/Installation',
 '@documentation' => 'http://documentation.realtidbits.com/Comments_V3',
 )),
 );
 $form['advanced']['realtidbits_echo_appkey'] = array(
 '#type' => 'textfield',
 '#title' => t('Echo Appkey'),
 '#description' => t('Echo Appkey.'),
 '#default_value' => variable_get('realtidbits_echo_appkey', ''),
 );
 $form['advanced']['realtidbits_echo_api_secret'] = array(
 '#type' => 'textfield',
 '#title' => t('Echo API Secret'),
 '#description' => t('Echo API Secret.'),
 '#default_value' => variable_get('realtidbits_echo_api_secret', ''),
 );
 $form['advanced']['realtidbits_rpx'] = array(
 '#type' => 'textfield',
 '#title' => t('RPX URL'),
 '#description' => t('RPX URL.'),
 '#default_value' => variable_get('realtidbits_rpx', ''),
 );
 $form['advanced']['realtidbits_echo_api_secret'] = array(
 '#type' => 'textfield',
 '#title' => t('Echo API Secret'),
 '#description' => t('Echo API Secret.'),
 '#default_value' => variable_get('realtidbits_echo_api_secret', ''),
 );
 // Behavior settings.
 $form['behavior'] = array(
 '#type' => 'fieldset',
 '#title' => t('Behavior'),
 '#group' => 'settings',
 );
 $form['behavior']['realtidbits_backplane_busname'] = array(
 '#type' => 'textfield',
 '#title' => t('Backplane Busname'),
 '#description' => t('Backplane Busname.'),
 '#default_value' => variable_get('realtidbits_backplane_busname', ''),
 );
 $form['behavior']['realtidbits_backplane_password'] = array(
 '#type' => 'textfield',
 '#title' => t('Backplane Password'),
 '#description' => t('Backplane Password.'),
 '#default_value' => variable_get('realtidbits_backplane_password', ''),
 );
 $form['behavior']['realtidbits_backplane_server'] = array(
 '#type' => 'textfield',
 '#title' => t('Backplane Server'),
 '#description' => t('Backplane Server.'),
 '#default_value' => variable_get('realtidbits_backplane_server', ''),
 );
 return system_settings_form($form);
}

Navigate to admin/config/services/realtidbits to save the realtidbits account credentials received.

Installing Realtidbits comments on the site
I suggest to use version 3 of the comments instead of version 2 which will be deprecated soon.
Replace the $js_code in hook_block_view above with the following code.

 $js_code = ‘
 <div id="comments"></div>
 <script src="http://cdn.echoenabled.com/sdk/v3/loader.js"></script>
 <script>
 Echo.Loader.initApplication({
 "script": "http://cdn.realtidbits.com/libs/v3/comments/comments.min.js",
 "component": "RTB.Apps.Comments",
 "backplane": {
 "serverBaseURL": "https://api.echoenabled.com/v1",
 "busName": "'. variable_get('realtidbits_backplane_busname') .'"
 },
 "config": {
 "appkey": "'. variable_get('realtidbits_echo_appkey') .'",
 "target": document.getElementById("comments"),
 "janrain": {
 "appUrl": "'. variable_get('realtidbits_rpx') .'"
 }
 }
 });
 </script>’;

You can further check the realtidbits developer documentation here for further details and other features of realtidbits comments. The parameter description and other optional parameters are better explained by realtidbits guy Kelly Abbott on developer documentation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s