You are here

function nodejs_send_content_channel_token in Node.js integration 6

Same name and namespace in other branches
  1. 8 nodejs.module \nodejs_send_content_channel_token()
  2. 7 nodejs.module \nodejs_send_content_channel_token()

Send a content channel token to Node.js.

Parameters

mixed $channel:

mixed $anonymous_only:

File

./nodejs.module, line 23

Code

function nodejs_send_content_channel_token($channel, $anonymous_only = FALSE) {
  $message = (object) array(
    'token' => nodejs_generate_content_token(),
    'anonymousOnly' => $anonymous_only,
    'channel' => $channel,
  );
  $response = Nodejs::sendContentToken($message);
  if (isset($response->error)) {
    $args = array(
      '%token' => $message->token,
      '%code' => $response->code,
      '%error' => $response->error,
    );
    watchdog('nodejs', t('Error sending content token "%token" to the Node.js server: [%code] %error', $args));
    return FALSE;
  }

  // We always set this in drupal.settings, even though Ajax requests will not
  // see it. It's a bit ugly, but it means that setting the tokens for full
  // page requests will just work.
  drupal_add_js(array(
    'nodejs' => array(
      'contentTokens' => array(
        $channel => $message->token,
      ),
    ),
  ), array(
    'type' => 'setting',
  ));

  // We return the message, so that calling code can use it for Ajax requests.
  return $message->token;
}