--- nodeblock.module +++ nodeblock.module @@ -6,6 +6,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) { @@ -116,12 +135,23 @@ function nodeblock_block($op = 'list', $delta = 0, $edit = array()) { variable_set('nodeblock_block_' . $delta, array('teaser' => $edit['teaser'], 'links' => $edit['links'])); } elseif ($op == 'view') { + global $theme_key; $node = node_load($delta); if (!node_access('view', $node)) { return; } $nodeblock_display = variable_get('nodeblock_block_' . $delta, array('teaser' => 0, 'links' => 1)); + // If CCK is enabled, set the build mode to the region. + $settings = variable_get('nodeblock_regions', array()); + if (module_exists('content')) { + $node->build_mode = 'block'; + $region = _nodeblock_block_region($delta); + if (isset($settings[$theme_key]) && $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)) { @@ -207,3 +237,114 @@ function nodeblock_preprocess_node(&$variables) { function nodeblock_theme_registry_alter(&$registry) { array_unshift($registry['node']['theme paths'], drupal_get_path('module', 'nodeblock')); } + +/** + * 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); + } + } + } + + $build_modes['block'] = array('title' => 'Block', '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)); +} + +