You are here

function _nd_nodeapi in Node displays 6

Same name and namespace in other branches
  1. 6.3 nd.module \_nd_nodeapi()
  2. 6.2 nd.module \_nd_nodeapi()
  3. 7 nd.module \_nd_nodeapi()

Helper function to alter node properties

Parameters

stdClass $node The complete node object.:

1 call to _nd_nodeapi()
nd_nodeapi in ./nd.module
Implementation of hook_nodeapi().

File

./nd.module, line 82
Main node displays file.

Code

function _nd_nodeapi(&$node) {

  // See if rendering is needed later on by the preprocess hook.
  // There are two ways of excluding: global exclude on content type
  // or per build mode per content type.
  // We don't return here, because we want to add all fields on the node object
  // so themers can use it in their template.
  $exclude_build_modes = variable_get('nd_buildmodes_exclude', array());
  $node->render_by_nd = isset($exclude_build_modes[$node->type][$node->build_mode]) && $exclude_build_modes[$node->type][$node->build_mode] == TRUE || variable_get('nd_contenttype_' . $node->type, FALSE) == TRUE ? FALSE : TRUE;
  $regions = array();
  $nd_fields = array();
  $nd_display_settings = variable_get('nd_display_settings_' . $node->type, array());

  // Get all fields.
  $fields = nd_get_fields($node->type, $node->has_body, $node->build_mode);
  if (!empty($fields)) {
    foreach ($fields as $key => $field) {

      // Exclude now depends on the region, previous it was an exclude property.
      $region = nd_default_value($nd_display_settings, $node->build_mode, 'fields', $key, 'region', ND_DEFAULT_REGION);
      if ($region != 'disabled') {
        $weight = nd_default_value($nd_display_settings, $node->build_mode, 'fields', $key, 'weight', ND_DEFAULT_WEIGHT);
        $node->content[$key]['#weight'] = $weight;
        $regions[$region][$key] = $weight;
        $nd_fields[$key] = array(
          'key' => $key,
          'title' => $field['title'],
          'labelformat' => nd_default_value($nd_display_settings, $node->build_mode, 'fields', $key, 'labelformat', ND_DEFAULT_LABEL_FORMAT),
          'type' => isset($field['display_settings']) ? 'cck' : 'nd',
        );

        // If the field has the storage key, it means the theming is done in that module.
        if (isset($field['storage'])) {
          continue;
        }

        // Theming can either be done in preprocess, or with a custom funtion or an
        // existing formatter theming function. For a custom function we'll pass
        // on the $node object, the field name and the complete field array.
        switch ($field['type']) {
          case ND_FIELD_PREPROCESS:
          case ND_FIELD_IGNORE:
            break;
          case ND_FIELD_CUSTOM:
          case ND_FIELD_OVERRIDABLE:
          case ND_FIELD_OVERRIDDEN:
            nd_eval_code($key, $field, $node);
            break;
          case ND_FIELD_BLOCK:
            nd_eval_block($key, $field, $node);
            break;
          case ND_FIELD_FUNCTION:
            if (isset($field['file'])) {
              include_once $field['file'];
            }
            $function = nd_default_value($nd_display_settings, $node->build_mode, 'fields', $key, 'format', key($field['formatters']));
            $function($node, $key, $field);
            break;
          case ND_FIELD_THEME:
            $format = nd_default_value($nd_display_settings, $node->build_mode, 'fields', $key, 'format', key($field['formatters']));
            theme($format, $node);
            break;
        }
      }
    }
  }

  // Add fields and regions to node object.
  // Also reset render_by_nd property if needed.
  $node->nd_fields = $nd_fields;
  $node->regions = $regions;
  if (empty($regions)) {
    $node->render_by_nd = FALSE;
  }

  // Special support for RSS.
  if ($node->build_mode == NODE_BUILD_RSS && $node->render_by_nd == TRUE) {
    $node->content['body']['#access'] = FALSE;
    foreach (element_children($node->content) as $key => $field) {
      if (!isset($nd_fields[$field])) {
        $node->content[$field]['#access'] = FALSE;
      }
      elseif (isset($nd_fields[$field]) && $nd_fields[$field]['type'] == 'nd') {
        $key = $field . '_rendered';
        $field_key = strtr($key, '_', '-');
        $node->content[$field]['#value'] = theme('nd_field', $node->{$key}, $field, $nd_fields[$field]);
      }
    }
  }
}