You are here

function cpn_node_view in Code per Node 7

Implements hook_node_view().

File

./cpn.module, line 459
Primary hook implementations.

Code

function cpn_node_view($node, $view_mode, $langcode) {

  // This variable ensures that CSS and JS don't get added twice, which is a
  // problem especially for JS.
  static $previewed = FALSE;

  // Verify this view mode was enabled.
  $modes = variable_get('cpn_view_modes_node_' . $node->type, array(
    'full',
    'teaser',
  ));
  if (!in_array($view_mode, $modes)) {
    return;
  }

  // Optional weights.
  $weight = array(
    'css' => variable_get('cpn_weight_css', CSS_THEME),
    'js' => variable_get('cpn_weight_js', JS_THEME),
  );

  // Attach the content type CSS/JS, lighter than the per-page files.
  foreach (array(
    'css',
    'js',
  ) as $type) {
    $file = variable_get('cpn_path', 'public://cpn') . '/' . $node->type . '.' . $type;
    if (is_file($file)) {
      $node->content['#attached'][$type]['cpn_type_' . $node->type] = array(
        'type' => 'file',
        'group' => $type == 'css' ? CSS_THEME : JS_THEME,
        'weight' => $weight[$type] - 1,
        'data' => $file,
      );
    }
  }

  // Previewing: add CSS and/or JS to the page, inline.
  if (!empty($node->in_preview)) {
    if (!$previewed) {
      foreach (array(
        'css',
        'js',
      ) as $type) {
        if (isset($node->cpn[$type]) && drupal_strlen(trim($node->cpn[$type]))) {

          // Wrap the output with the global wrapper.
          $output = cpn_wrap_output($node->cpn[$type], 'node', $type);

          // Replace the token strings using any available global & node
          // tokens.
          $output = token_replace($output, array(
            'node' => $node,
          ));

          // Output the code.
          if (!empty($output)) {
            $node->content['#attached'][$type]['cpn_node_' . $node->nid] = array(
              'type' => 'inline',
              'group' => $type == 'css' ? CSS_THEME : JS_THEME,
              'weight' => $weight[$type],
              'data' => $output,
            );
          }
        }
      }
    }
    $previewed = TRUE;
  }
  else {
    foreach (array(
      'css',
      'js',
    ) as $type) {
      $file = variable_get('cpn_path', 'public://cpn') . '/' . $node->nid . '.' . $type;
      if (is_file($file)) {
        $node->content['#attached'][$type]['cpn_node_' . $node->nid] = array(
          'type' => 'file',
          'group' => $type == 'css' ? CSS_THEME : JS_THEME,
          'weight' => $weight[$type],
          'data' => $file,
          'preprocess' => variable_get('cpn_aggregation_' . $type, FALSE),
        );

        // Optionally load as an external file.
        if ((bool) variable_get('cpn_external_' . $type, FALSE)) {
          $node->content['#attached'][$type]['cpn_node_' . $node->nid]['type'] = 'external';
          $node->content['#attached'][$type]['cpn_node_' . $node->nid]['data'] = file_create_url($file) . '?' . $node->changed;
        }
      }
    }
  }

  // 'noscript' code needs to be handled specially.
  if (!empty($node->cpn['noscript'])) {
    $node->content['body'][0]['#prefix'] = cpn_output_noscript($node->cpn['noscript']);
  }
}