diff --git a/README.txt b/README.txt index bdccf70..8179f26 100644 --- a/README.txt +++ b/README.txt @@ -1,29 +1,18 @@ == Description == BlockTerm lets you specify taxonomy terms to show this block on. - When you visit a page where the node is tagged with one of these terms, the block will show, otherwise it won't. + When you visit a page where the node is tagged with one of these terms, + or a child term af one of these terms, the block will show, otherwise it won't. == Installation == 1. Enable the module -2. add the following to [yourtheme]_block($block) in template.php: +2. Copy the block-ignore.tpl.php file to your theme directory - -if (module_exists('blockterm')) { - if (!blockterm_can_show($block) ) { - return; - } -} - -return phptemplate_block($block); - - == Usage == -1. Edit any Block, open up the fieldset for Block Taxonomy, select some terms - -2. add *node/* to the list of allowed URLs (else this module will do nothing) +1. Edit or Create any Block, open up the fieldset for Block Taxonomy, select some terms -3. Save it and try it out! +2. Save it and try it out! diff --git a/blockterm.info b/blockterm.info index 93e3ac5..52b8a15 100644 --- a/blockterm.info +++ b/blockterm.info @@ -1,5 +1,10 @@ -; $Id: -name = "Blocks by term" -description = "Let's the user choose a term when a given block will show" -package = "Other" -dependencies = "helpers_node helpers_form" +; $Id: blockterm.info bwinett Exp $ +name = Blocks by term +description = Let's the user select a taxonomy term in the settings of a block. The block will only show on nodes that have the required taxonomy term selected. +dependencies[] = taxonomy + +; Information added manually +version = "6.x-0.5" +core = "6.x" + + diff --git a/blockterm.module b/blockterm.module index 71497d8..98b4270 100644 --- a/blockterm.module +++ b/blockterm.module @@ -1,139 +1,151 @@ 'admin/settings/blockterm', - 'title' => t('Block Theme'), - 'description' => t('Allows the admin to define re-usable block templates that can be configured from block config screen'), - 'callback' => 'drupal_get_form', - 'callback arguments' => array('blockterm_admin_settings'), - ); - } - return $items; -} +/** + * @file + * The module file for the blockterm module + */ -function blockterm_help($section) { +function blockterm_help($path, $arg) { global $base_url; - switch ($section) { + switch ($path) { case 'admin/help#blockterm': return t('Define block visibility by term'); break; - -// case 'admin/settings/blockterm': -// return t('blockterm allows an admin to define tpl files for standard block templates and provides a select box on the block configure form so the user can select a tpl file to use as opposed to having to override the templates by block ID.'); -// break; } } -function blockterm_form_alter($form_id,&$form) { - if ($form_id == 'block_admin_configure'){ - $module = $form['module']['#value']; - $delta = $form['delta']['#value']; - $var_name = $module . '-' .$delta; - - -// $blockterms = variable_get('blockterm_visibility',''); -// $_sets = explode("\n",$blockterms); -// $options[] = t('<-- None -->'); -// -// foreach ($_sets as $key => $value) { -// $set = explode('|',$value); -// $options[$set[0]] = $set[1]; -// } +/** + * Implementation of hook_perm(). + */ +function blockterm_perm() { + return array('select block visibility terms'); +} + +/* form for updating a block */ +function blockterm_form_block_admin_configure_alter(&$form, &$form_state) { + $module = $form['module']['#value']; + $delta = $form['delta']['#value']; + $var_name = $module .'-'. $delta; + _blockterm_form_alter($form, $form_state, $var_name); +} +/* form for adding a new block */ +function blockterm_form_block_add_block_form_alter(&$form, &$form_state) { + _blockterm_form_alter($form, $form_state); +} +/** + shared local function to alter forms + $default-value: default option + */ +function _blockterm_form_alter(&$form, &$form_state, $var_name=NULL) { + if ( user_access('select block visibility terms') ) { + $form['block_settings']['blockterm'] = array( + '#type' => 'fieldset', + '#title' => t("Block Taxonomy Settings"), + '#description' => t('In addition to the rules below, blocks will show on any nodes which are tagged with the following terms.'), + '#collapsed' => TRUE, + '#collapsible' => TRUE, + '#tree' => TRUE, + ); $blockterm = blockterm_get(); - - $form['block_settings']['blockterm'] = array ( - '#type' => 'fieldset', - '#title' => t("Block Taxonomy Settings"), - '#description' => t('In addition to the rules below, blocks will show on any nodes which are tagged with the following terms.
Note: If you want this to work, you will want to add *node/* to the URLs to include in the path settings below'), - '#collapsed' => true, - '#collapsible' => true, - '#tree' => true, + $form['block_settings']['blockterm']['enable_block_term'] = array( + '#type' => 'checkbox', + '#title' => t('Enable block taxonomy'), + '#description' => t('Check to activate this feature'), + '#default_value' => (isset($blockterm[$var_name]['enable_block_term']))?$blockterm[$var_name]['enable_block_term']:FALSE, + '#weight' => -10, ); - -foreach (taxonomy_get_vocabularies() as $vid => $vocab) { - - $vocab_select = taxonomy_form($vid); - $vocab_select['#multiple'] = true; - $vocab_select['#options'] = is_string($vocab_select['#options']["0"]) ? $vocab_select['#options'] : array(0 => theme('none_option')) + $vocab_select['#options']; - $vocab_select['#default_value'] = null; - if ($blockterm[$var_name][$vid]) { - $vocab_select['#default_value'] = $blockterm[$var_name][$vid]; - } - $form['block_settings']['blockterm'][$vid] = $vocab_select; -} - + foreach (taxonomy_get_vocabularies() as $vid => $vocab) { + $vocab_select = taxonomy_form($vid); + $vocab_select['#multiple'] = TRUE; + $vocab_select['#options'] = is_string($vocab_select['#options']['']) ? $vocab_select['#options'] : array(0 => theme('none_option')) + $vocab_select['#options']; + if ($blockterm[$var_name][$vid]) { + $default_value = $blockterm[$var_name][$vid]; + } - -// $form['block_settings']['blockterm'] = array ( -// '#type' => 'select', -// '#title' => t('Block Term Settings'), -// '#default_value' => $blockterm[$var_name], -// '#options' => $options, -// ); - - $form['#submit']['blockterm_save'] = array(); + $vocab_select['#default_value'] = $default_value; + $vocab_select['#weight'] = 0; + $form['block_settings']['blockterm'][$vid] = $vocab_select; + } + $form['#submit'][] = 'blockterm_save'; } } function blockterm_set($new_val) { if ($new_val) { - variable_set('blockterm',$new_val); + variable_set('blockterm', $new_val); } } - function blockterm_get() { static $blockterm; if (empty($blockterm)) { - $blockterm = variable_get('blockterm',array()); + $blockterm = variable_get('blockterm', array()); } return $blockterm; } - -function blockterm_save($form_id,$form_values) { - $var_name = $form_values['module'] . '-' . $form_values['delta']; +function blockterm_save($form, &$form_state) { + $delta = $form_state['values']['delta']; + if (is_NULL($delta)) { + // this is a new block. First get the new delta value + $result = db_query("SELECT delta FROM {blocks} WHERE bid = '%s'", db_last_insert_id('boxes', 'bid')); + if ($delta_result = db_fetch_object($result)) { + $delta = $delta_result->delta; + } + else return; + } + $var_name = $form_state['values']['module'] .'-'. $delta; $blockterm = blockterm_get(); - - if (!$form_values['blockterm']) { + + if (!$form_state['values']['blockterm']) { unset($blockterm[$var_name]); - } else { - $blockterm[$var_name] = $form_values['blockterm']; + } + else { + $blockterm[$var_name] = $form_state['values']['blockterm']; } blockterm_set($blockterm); } -function blockterm_can_show(&$block) { - if (is_node_page()) { - $node = node_load_current(); - $blockterm = blockterm_get(); - $var_name = $block->module . '-' . $block->delta; +function blockterm_preprocess_block(&$variables) { + $var_name = $variables['block']->module .'-'. $variables['block']->delta; + $blockterm = blockterm_get(); + if(isset($blockterm[$var_name]['enable_block_term']) && $blockterm[$var_name]['enable_block_term'] == 1) { if (!isset($blockterm[$var_name])) { - return true; + //Nothing to do: This block has no blockterm variables set + return; } - foreach ($node->taxonomy as $tid => $tax) { - if (isset($blockterm[$var_name][$tax->vid][$tid])) { - return true; + $hide = TRUE; + if ( arg(0) == 'node' && is_numeric(arg(1)) ) { + // Yes, we're viewing a node. + $node = node_load(arg(1)); + foreach ($node->taxonomy as &$tax) { + if (_block_has_term($blockterm[$var_name], $tax)) { + $hide = FALSE; + } } } - - return false; + if($hide) { + //$variables['template_files'][] = 'block-ignore'; // Use template block-ignore.tpl.php to hide blocks + unset($variables['block']); // Unset block variable to hide block + } } - - return true; } -//Is it better to unserialize a huge array, or to use a hacky string based namespace? - +// Check if this block is enabled for the given taxonomy term or one of its child terms. +function _block_has_term(&$blockterm, &$tax) { + if (isset($blockterm[$tax->vid][$tax->tid])) { + return TRUE; + } + $children = taxonomy_get_tree($tax->vid, $tax->tid); + foreach ($children as &$child_tax) { + return _block_has_term($blockterm, $child_tax); + } + return FALSE; +} -?> \ No newline at end of file +//Is it better to unserialize a huge array, or to use a hacky string based namespace?