You are here

function webform_localization_node_view in Webform Localization 7

Same name and namespace in other branches
  1. 7.4 webform_localization.module \webform_localization_node_view()

Implements hook_node_view().

File

./webform_localization.module, line 207
Webform localization module.

Code

function webform_localization_node_view($node, $view_mode) {
  if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
    return;
  }

  // Select all webforms that match the localization configuration.
  $query = db_select('webform', 'w');
  $query
    ->innerJoin('webform_localization', 'wl', 'w.nid = wl.nid');
  $query
    ->fields('w', array(
    'nid',
  ));
  $query
    ->condition('wl.single_webform', 0, '<>');
  $query
    ->condition('wl.single_webform', $node->tnid, '=');
  $query
    ->condition('w.nid', $node->nid, '<>');
  $result = $query
    ->execute()
    ->fetchField();
  if ($result) {

    /**
     * NOTE:
     * Perhaps not most eficient way.. a possible alternative
     * @todo rewrite the webform load and view process as a
     * independent function to reuse.
     */
    $source_node = node_load($result);

    // We replace the webform with the node translation source.
    $node->webform = $source_node->webform;

    // This fool webform_node_view to avoid errors with drafts in between pages.
    $translation_nid = $node->nid;
    if (($node->webform['allow_draft'] || $node->webform['auto_save']) && $user->uid != 0) {
      $node->nid = $source_node->nid;
    }

    // Call node view implementation to update the $node->content.
    webform_node_view($node, $view_mode);

    // Reset the nid if we used the drafts fix.
    $node->nid = $translation_nid;
  }
}