$theme) { $block_regions = system_region_list($key); foreach ($block_regions as $region => $title) { $items['admin/build/block/region_conf/'. $key .'/'. $region] = array( 'title' => "Region configuration of " . check_plain($theme->info['name'] . " - " . $title), 'page callback' => 'drupal_get_form', 'page arguments' => array('region_conf_form', $key, $region), 'type' => MENU_CALLBACK, 'access arguments' => array('administer region_conf'), ); } } return $items; } /** * Implementation of hook_perm(). */ function region_conf_perm() { return array('administer region_conf', 'use PHP for region visibility'); } function region_conf_form(&$form_state, $theme_key, $region) { $form['theme_key'] = array('#type' => 'value', '#value' => $theme_key); $form['region'] = array('#type' => 'value', '#value' => $region); $edit = db_fetch_array(db_query("SELECT roles, pages, visibility FROM {region_conf} WHERE theme_key = '%s' AND region = '%s'", $theme_key, $region)); // Role-based visibility settings $default_role_options = unserialize($edit['roles']); if (!is_array($default_role_options)) { $default_role_options = array(); } $result = db_query('SELECT rid, name FROM {role} ORDER BY name'); $role_options = array(); while ($role = db_fetch_object($result)) { $role_options[$role->rid] = $role->name; } $form['role_vis_settings'] = array( '#type' => 'fieldset', '#title' => t('Role specific visibility settings'), '#collapsible' => TRUE, ); $form['role_vis_settings']['roles'] = array( '#type' => 'checkboxes', '#title' => t('Show region for specific roles'), '#default_value' => $default_role_options, '#options' => $role_options, '#description' => t('Show this region only for the selected role(s). If you select no roles, the region will be visible to all users.'), ); $form['page_vis_settings'] = array( '#type' => 'fieldset', '#title' => t('Page specific visibility settings'), '#collapsible' => TRUE, ); $access = user_access('use PHP for region visibility'); if ($edit['visibility'] == 2 && !$access) { $form['page_vis_settings'] = array(); $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2); $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']); } else { $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')); $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '')); if ($access) { $options[] = t('Show if the following PHP code returns TRUE (PHP-mode, experts only).'); $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '')); } $form['page_vis_settings']['visibility'] = array( '#type' => 'radios', '#title' => t('Show region on specific pages'), '#options' => $options, '#default_value' => $edit['visibility'], ); $form['page_vis_settings']['pages'] = array( '#type' => 'textarea', '#title' => t('Pages'), '#default_value' => $edit['pages'], '#description' => $description, ); } $form['submit'] = array( '#type' => 'submit', '#value' => t('Save region configuration'), ); return $form; } function region_conf_form_submit($form, &$form_state) { // check if register exist to insert or update. if (db_result(db_query("SELECT count(*) FROM {region_conf} WHERE theme_key = '%s' AND region = '%s'", $form_state['values']['theme_key'], $form_state['values']['region']))) { db_query("UPDATE {region_conf} SET roles = '%s', pages = '%s', visibility = %d WHERE theme_key = '%s' AND region = '%s'", serialize($form_state['values']['roles']), $form_state['values']['pages'], $form_state['values']['visibility'], $form_state['values']['theme_key'], $form_state['values']['region']); } else { db_query("INSERT INTO {region_conf} (theme_key, region, roles, pages, visibility) VALUES ('%s', '%s', '%s', '%s', %d)", $form_state['values']['theme_key'], $form_state['values']['region'], serialize($form_state['values']['roles']), $form_state['values']['pages'], $form_state['values']['visibility']); } drupal_set_message(t('The region configuration has been saved.')); cache_clear_all(); $form_state['redirect'] = 'admin/build/block/list/'.$form_state['values']['theme_key']; return; } function region_conf_preprocess_block_admin_display_form (&$vars) { global $theme_key; foreach ($vars['block_regions'] as $region => $title) { if ($region == BLOCK_REGION_NONE) { continue; } $vars['block_regions'][$region] .= " - ". l(t('configure'), 'admin/build/block/region_conf/'.$theme_key.'/'.$region); } } function _region_conf_visibility($theme_key, $region_key) { global $user; // Select. If not exists, return TRUE $rs = db_query("SELECT roles, pages, visibility FROM {region_conf} WHERE theme_key = '%s' AND region = '%s'", $theme_key, $region_key); if (!$region = db_fetch_object($rs)) { return TRUE; } // Check Roles. If false, return FALSE $roles = array_unique(array_values(unserialize($region->roles))); if (count($roles) > 1 && !count(array_intersect(array_keys($user->roles), $roles))) { return FALSE; } // Match path if necessary if ($region->pages) { if ($region->visibility < 2) { $path = drupal_get_path_alias($_GET['q']); // Compare with the internal and path alias (if any). $page_match = drupal_match_path($path, $region->pages); if ($path != $_GET['q']) { $page_match = $page_match || drupal_match_path($_GET['q'], $region->pages); } // When $region->visibility has a value of 0, the region is displayed on // all pages except those listed in $region->pages. When set to 1, it // is displayed only on those pages listed in $region->pages. $page_match = !($region->visibility xor $page_match); } else { $page_match = drupal_eval($region->pages); } } else { $page_match = TRUE; } return $page_match; } function region_conf_theme($existing, $type, $theme, $path) { return array( 'blocks' => array( 'arguments' => array('region' => NULL), 'function' => 'region_conf_blocks', ), ); } /* * Override for theme_blocks * * Completely disables hidden regions */ function region_conf_blocks($region) { global $theme_key; $output = ''; if (_region_conf_visibility($theme_key, $region)) { if ($list = block_list($region)) { foreach ($list as $key => $block) { // $key == module_delta $output .= theme('block', $block); } } // Add any content assigned to this region through drupal_set_content() calls. $output .= drupal_get_content($region); } return $output; }