View source
<?php
define('ND_FIELD_THEME', 1);
define('ND_FIELD_FUNCTION', 2);
define('ND_FIELD_PREPROCESS', 3);
define('ND_FIELD_IGNORE', 4);
define('ND_FIELD_CUSTOM', 5);
define('ND_FIELD_OVERRIDABLE', 6);
define('ND_FIELD_OVERRIDDEN', 7);
define('ND_FIELD_BLOCK', 8);
define('ND_DEFAULT_REGION', 'disabled');
define('ND_DEFAULT_FORMAT', 'default');
define('ND_DEFAULT_LABEL_FORMAT', 'hidden');
define('ND_DEFAULT_WEIGHT', -19);
define('BLOCK_TEMPLATE', 1);
define('BLOCK_TITLE_CONTENT', 2);
define('BLOCK_CONTENT', 3);
function nd_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'load':
$node->has_body = node_get_types('type', $node->type)->has_body;
break;
case 'view':
if ($node->build_mode == NODE_BUILD_RSS) {
_nd_nodeapi($node);
}
elseif ($node->build_mode == NODE_BUILD_PREVIEW) {
$node->build_mode = $teaser == TRUE ? 'teaser' : 'full';
$node->has_body = node_get_types('type', $node->type)->has_body;
}
elseif ($node->build_mode === NODE_BUILD_NORMAL) {
$build_mode = $page ? 'full' : 'teaser';
$node->build_mode = $build_mode;
}
if ($node->build_mode == 'teaser' && $node->sticky == 1) {
$node->build_mode = 'nd_sticky';
}
break;
case 'alter':
if ($node->build_mode == NODE_BUILD_RSS) {
return;
}
_nd_nodeapi($node);
break;
}
}
function _nd_nodeapi(&$node) {
$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());
$fields = nd_get_fields($node->type, $node->has_body, $node->build_mode);
if (!empty($fields)) {
foreach ($fields as $key => $field) {
$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 (isset($field['storage'])) {
continue;
}
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;
}
}
}
}
$node->nd_fields = $nd_fields;
$node->regions = $regions;
if (empty($regions)) {
$node->render_by_nd = FALSE;
}
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]);
}
}
}
}
function nd_preprocess_node(&$vars, $hook) {
if (!variable_get('nd_preprocess_override', FALSE)) {
_nd_preprocess_node($vars, $hook);
}
}
function _nd_preprocess_node(&$vars, $hook) {
$node = $vars['node'];
$vars['template_files'][] = 'node-' . $node->type . '-' . $node->build_mode;
$vars['template_files'][] = 'node-' . $node->type . '-' . $node->build_mode . '-' . $node->nid;
if (!$node->render_by_nd) {
return;
}
$all_regions = nd_regions('anything', TRUE);
$regions = $vars['regions'];
$themed_regions = array();
$nd_display_settings = variable_get('nd_display_settings_' . $node->type, array());
$fields = nd_get_fields($node->type, $node->has_body, $node->build_mode);
if (!empty($fields)) {
foreach ($fields as $key => $field) {
if ($field['type'] == ND_FIELD_PREPROCESS && isset($node->nd_fields[$key])) {
$field_key = $key . '_rendered';
$vars[$field_key] = $vars[$key];
unset($vars[$key]);
}
if ($field['type'] == ND_FIELD_IGNORE) {
$field_key = $key . '_rendered';
$vars[$field_key] = $node->content[$key]['#value'];
}
}
}
$region_classes = array();
foreach ($all_regions as $region_name => $region_title) {
if (isset($regions[$region_name])) {
$region_content = '';
$region = $regions[$region_name];
asort($region);
foreach ($region as $key => $weight) {
$field_content = isset($vars[$key . '_rendered']) ? $vars[$key . '_rendered'] : '';
if (!empty($field_content)) {
$field_key = strtr($key, '_', '-');
$region_content .= theme('nd_field', $field_content, $field_key, $node->nd_fields[$key]);
}
unset($node->content[$key]);
}
if (!empty($region_content)) {
if ($region_name == 'left' || $region_name == 'right') {
$region_classes[$region_name] = $region_name;
}
$themed_regions[$region_name] = array(
'content' => $region_content,
);
}
}
}
$node_display = new stdClass();
$node_display->all_regions = $all_regions;
$node_display->themed_regions = $themed_regions;
$node_display->region_classes = $region_classes;
nd_plugins_process($vars, $node_display, $nd_display_settings);
$nd_middle_classes = 'no-sidebars';
if (isset($node_display->region_classes['left']) && isset($node_display->region_classes['right'])) {
$nd_middle_classes = 'two-sidebars';
}
elseif (isset($node_display->region_classes['left'])) {
$nd_middle_classes = 'one-sidebar sidebar-left';
}
elseif (isset($node_display->region_classes['right'])) {
$nd_middle_classes = 'one-sidebar sidebar-right';
}
$content = '';
foreach ($node_display->themed_regions as $region_name => $region_data) {
$content .= '<div class="nd-region-' . $region_name;
if ($region_name == 'middle') {
$content .= ' ' . $nd_middle_classes;
}
if (isset($region_data['extra_class'])) {
$content .= $region_data['extra_class'];
}
$content .= '" ';
if (isset($region_data['inline_css'])) {
$content .= $region_data['inline_css'];
}
$content .= '><div class="nd-region-inner">' . $region_data['content'] . '</div></div>';
}
$vars['content'] = $content;
}
function nd_plugins_process(&$vars, &$node_display, $nd_display_settings) {
$plugins = variable_get('nd_plugin_settings', array());
if (!empty($plugins)) {
foreach ($plugins as $key => $plugin) {
if (isset($plugin['filename'])) {
require_once $plugin['filename'];
}
$function = $plugin['process_callback'];
$function($vars, $node_display, $nd_display_settings);
}
}
}
function nd_default_value($settings, $build_mode, $type, $key, $search_key, $default) {
return isset($settings[$build_mode][$type][$key][$search_key]) ? $settings[$build_mode][$type][$key][$search_key] : $default;
}
function nd_eval_code($key, $field, &$node) {
if (isset($field['code'])) {
$node_key = $key . '_rendered';
$value = nd_eval($field['code'], $node);
if (module_exists('token')) {
$value = token_replace($value, 'node', $node);
}
$node->{$node_key} = $value;
}
}
function nd_eval_block($key, $field, &$node) {
if (isset($field['code'])) {
list($module, $delta) = explode('|', $field['code']);
$block = module_invoke($module, 'block', 'view', $delta);
if (!empty($block)) {
switch ($field['render']) {
case BLOCK_TEMPLATE:
global $theme_key;
$block = (object) $block;
$block->module = $module;
$block->delta = $delta;
$block_title = db_result(db_query("SELECT title FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme_key));
if (!empty($block_title)) {
$block->subject = $block_title == '<none>' ? '' : check_plain($block_title);
}
$content = theme('block', $block);
break;
case BLOCK_TITLE_CONTENT:
$content = '<h2 class="block-title">' . $block['subject'] . '</h2>';
$content .= $block['content'];
break;
case BLOCK_CONTENT:
$content = $block['content'];
break;
}
$node_key = $key . '_rendered';
$node->{$node_key} = $content;
}
}
}
function nd_eval($code, $node) {
global $theme_path, $theme_info, $conf;
$old_theme_path = $theme_path;
if (!isset($theme_info)) {
$theme_path = drupal_get_path('theme', $conf['theme_default']);
}
else {
$theme_path = dirname($theme_info->filename);
}
ob_start();
print eval('?>' . $code);
$output = ob_get_contents();
ob_end_clean();
$theme_path = $old_theme_path;
return $output;
}
function nd_get_fields($node_type, $has_body, $build_mode) {
static $static_fields = array();
if (!isset($static_fields[$node_type][$build_mode])) {
$fields = module_invoke_all('nd_fields', $node_type, $has_body, $build_mode);
$db_fields = variable_get('nd_fields', array());
if (!empty($db_fields)) {
foreach ($db_fields as $key => $field) {
$fields[$key] = array(
'title' => check_plain($field['title']),
'code' => isset($field['block']) ? $field['block'] : $field['code'],
'type' => $field['type'],
'render' => isset($field['render']) ? $field['render'] : '',
);
$exclude = isset($field['exclude'][$node_type]) && $field['exclude'][$node_type] === $node_type ? TRUE : FALSE;
if ($exclude) {
unset($fields[$key]);
}
}
}
drupal_alter('nd_fields', $fields);
$static_fields[$node_type][$build_mode] = $fields;
}
return $static_fields[$node_type][$build_mode];
}
function nd_get_build_modes($selector = NULL, $reset = FALSE) {
$build_modes = variable_get('nd_all_build_modes', array());
if (empty($build_modes) || $reset) {
require_once 'includes/nd.registry.inc';
$build_modes = _nd_register_build_modes();
}
if ($selector != NULL) {
return $build_modes[$selector]['build modes'];
}
else {
return $build_modes;
}
}
function nd_regions($build_mode = 'anything', $render = FALSE) {
if ($build_mode != NODE_BUILD_RSS) {
if ($render == FALSE) {
return array(
'header' => t('Header'),
'left' => t('Left'),
'middle' => t('Middle'),
'right' => t('Right'),
'footer' => t('Footer'),
'disabled' => t('Disabled'),
);
}
else {
return array(
'header' => t('Header'),
'left' => t('Left'),
'right' => t('Right'),
'middle' => t('Middle'),
'footer' => t('Footer'),
'disabled' => t('Disabled'),
);
}
}
else {
return array(
'middle' => t('Enabled'),
'disabled' => t('Disabled'),
);
}
}
function nd_init() {
if (variable_get('nd_regions_css', TRUE)) {
drupal_add_css(drupal_get_path('module', 'nd') . '/css/regions.css');
}
}
function nd_menu() {
require_once 'includes/nd.registry.inc';
return _nd_menu();
}
function nd_theme() {
require_once 'includes/nd.registry.inc';
return _nd_theme();
}
function nd_content_build_modes() {
require_once 'includes/nd.registry.inc';
return _nd_content_build_modes();
}
function nd_nd_plugins() {
require_once 'includes/nd.registry.inc';
return _nd_plugins();
}
function nd_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'nd') . '/views',
);
}
function nd_nd_fields($node_type, $has_body, $build_mode) {
$fields = array(
'title' => array(
'title' => t('Title'),
'formatters' => array(
'nd_title_h1_nolink' => t('H1 title'),
'nd_title_h1_link' => t('H1 title, linked to node'),
'nd_title_h2_nolink' => t('H2 title'),
'nd_title_h2_link' => t('H2 title, linked to node'),
'nd_title_h2_block_nolink' => t('H2 block title'),
'nd_title_h2_block_link' => t('H2 block title, linked to node'),
'nd_title_p_nolink' => t('Paragraph title'),
'nd_title_p_link' => t('Paragraph title, linked to node'),
),
'type' => ND_FIELD_THEME,
),
'author' => array(
'title' => t('Author'),
'formatters' => array(
'nd_author_nolink' => t('Author'),
'nd_author_link' => t('Author linked to profile'),
),
'type' => ND_FIELD_THEME,
),
'links' => array(
'title' => t('Links'),
'type' => ND_FIELD_PREPROCESS,
),
'read_more' => array(
'title' => t('Read more'),
'code' => '<?php echo l(t("Read more"), "node/$node->nid"); ?>',
'type' => ND_FIELD_OVERRIDABLE,
),
'post_date' => array(
'title' => t('Post date'),
'code' => '<?php echo format_date($node->created, "custom", "d/m/Y"); ?>',
'type' => ND_FIELD_OVERRIDABLE,
),
);
if ($has_body == TRUE) {
$fields['body'] = array(
'title' => t('Core body'),
'formatters' => array(
'nd_bodyfield' => t('Body'),
),
'type' => ND_FIELD_THEME,
);
}
if (module_exists('taxonomy')) {
$fields['terms'] = array(
'title' => t('Taxonomy'),
'type' => ND_FIELD_PREPROCESS,
);
}
if (module_exists('upload') && $build_mode != 'teaser' && variable_get("upload_{$node_type}", 1)) {
$fields['files'] = array(
'title' => t('Core upload'),
'type' => ND_FIELD_IGNORE,
);
}
return $fields;
}