--- nodeblock.module 2009-03-31 23:12:10.000000000 -0400 +++ nodeblock.module 2009-05-04 16:46:37.000000000 -0400 @@ -7,6 +7,25 @@ */ /** + * Implementation of hook_menu(). + */ +function nodeblock_menu() { + $items = array(); + + if (module_exists('content')) { + $items['admin/settings/nodeblock'] = array( + 'title' => 'Nodeblock', + 'description' => 'Enable regions for CCK build modes.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('nodeblock_cck_settings_form'), + 'access arguments' => array('administer content types'), + ); + } + + return $items; +} + +/** * Utility function to tell whether a type is enabled as a node block */ function nodeblock_type_enabled($type) { @@ -100,8 +119,18 @@ function nodeblock_block($op = 'list', $ return $blocks; } elseif ($op == 'view') { + global $theme_key; $node = node_load($delta); + // If CCK is enabled, set the build mode to the region. + $settings = variable_get('nodeblock_regions', array()); + if (module_exists('content') && isset($settings[$theme_key])) { + $region = _nodeblock_block_region($delta); + if ($settings[$theme_key][$region]) { + $node->build_mode = $theme_key .'_'. $region; + } + } + // if the node type is translatable, try to load the node with the appropriate // language from the translation set. if (module_exists('translation') && translation_supported_type($node->type)) { @@ -186,4 +215,111 @@ function nodeblock_preprocess_node(&$var */ function nodeblock_theme_registry_alter(&$registry) { array_unshift($registry['node']['theme paths'], drupal_get_path('module', 'nodeblock')); -} \ No newline at end of file +} + +/** + * Region settings form. + */ +function nodeblock_cck_settings_form() { + $form = array(); + + drupal_clear_css_cache(); + $themes = system_theme_data(); + $settings = variable_get('nodeblock_regions', array()); + + module_load_include('inc', 'system', 'system.admin'); + uasort($themes, 'system_sort_modules_by_info_name'); + + $form['nodeblock_regions'] = array( + '#type' => 'fieldset', + '#title' => t('CCK Regions'), + '#description' => t('Please select which regions should be available as build modes for CCK.'), + '#tree' => TRUE, + ); + + foreach($themes as $theme) { + if ($theme->status) { + $form['nodeblock_regions'][$theme->name] = array( + '#type' => 'fieldset', + '#title' => $theme->info['name'], + '#tree' => TRUE, + '#collapsible' => TRUE, + ); + + foreach (system_region_list($theme->name) as $region => $title) { + $form['nodeblock_regions'][$theme->name][$region] = array( + '#type' => 'checkbox', + '#title' => $title, + '#default_value' => $settings[$theme->name][$region], + ); + } + } + } + + $form = system_settings_form($form); + $form['#submit'][] = 'nodeblock_cck_settings_form_submit'; + + return $form; +} + +/** + * Submit handler for the nodeblock_cck_settings_form. + */ +function nodeblock_cck_settings_form_submit($form, &$form_state) { + menu_cache_clear_all(); +} + +/** + * Implementations of hook_content_build_modes + * on behalf of core modules. + * + * @see node_content_build_modes() in content.module + * + * @return + * An array describing the build modes used by the module. + * They are grouped by secondary tabs on CCK's 'Display fields' screens. + */ +function nodeblock_content_build_modes() { + $settings = variable_get('nodeblock_regions', array()); + + if (!empty($settings)) { + $build_modes = array(); + + foreach($settings as $theme_key => $regions) { + foreach (system_region_list($theme_key) as $region => $title) { + if ($settings[$theme_key][$region]) { + // Delimit the build mode with an underscore like so: themekey_region + $build_mode = $theme_key .'_'. $region; + // Create the title for the tab. + $tab_title = t('@title (@theme)', array('@title' => $title, '@theme' => $theme_key)); + // Set the build mode, leaving views_style as FALSE for now. + $build_modes[$build_mode] = array('title' => $tab_title, 'views_style' => FALSE); + } + } + } + + if (!empty($build_modes)) { + return array( + 'nodeblock' => array( + 'title' => t('Block'), + 'build modes' => $build_modes, + ), + ); + } + } +} + +/** + * Returns the region the block is being viewed in. + * + * @param $delta + * The $delta (or) $nid of the block + * + * @return + * The region for the block. + */ +function _nodeblock_block_region($delta) { + global $theme_key; + + return db_result(db_query("SELECT region FROM {blocks} WHERE module = 'nodeblock' AND delta = '%s' AND theme ='%s'", $delta, $theme_key)); + }