You are here

function cpn_ctools_render_alter in Code per Node 7

Same name and namespace in other branches
  1. 6 cpn.module \cpn_ctools_render_alter()

Implements hook_ctools_render_alter().

File

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

Code

function cpn_ctools_render_alter(&$info, &$page, &$context) {

  // Only work with node views.
  if ($context['task']['name'] == 'node_view') {

    // Get the node data.
    if (isset($context['contexts']['argument_entity_id:node_1']->data)) {
      $node = $context['contexts']['argument_entity_id:node_1']->data;

      // Verify this view mode was enabled.
      $modes = variable_get('cpn_view_modes_node_' . $node->type, array(
        'full',
        'teaser',
      ));
      if (!in_array('full', $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)) {
          $options = array(
            'type' => 'file',
            'group' => $type == 'css' ? CSS_THEME : JS_THEME,
            'weight' => $weight[$type] - 1,
            'preprocess' => (bool) variable_get('cpn_aggregation_' . $type, FALSE),
          );
          $function = 'drupal_add_' . $type;
          $function($file);
        }
      }

      // Check for CPN.
      if (isset($node->cpn)) {
        foreach (array(
          'css',
          'js',
          'noscript',
        ) as $type) {

          // Check for CSS
          if (isset($node->cpn[$type]) && !empty($node->cpn[$type])) {

            // noscript gets special handling.
            if ($type == 'noscript') {
              $lang = $node->language;
              $info['content'] .= cpn_output_noscript($node->cpn['noscript']);
            }
            else {
              $file = variable_get('cpn_path', 'public://cpn') . '/' . $node->nid . '.' . $type;

              // Make the weight heavier than the per-content type value so it
              // loads last.
              $options = array(
                'type' => 'file',
                'group' => $type == 'css' ? CSS_THEME : JS_THEME,
                'weight' => $weight[$type],
                'preprocess' => (bool) variable_get('cpn_aggregation_' . $type, FALSE),
              );
              $function = 'drupal_add_' . $type;
              $function($file, $options);
            }
          }
        }
      }
    }
  }
}