Hierarchical Select in a block as a taxonomy filter

I've created a sample module that creates a new block which allows you to filter by Taxonomy term. The code is very clearly documented, to make it easier for you to adapt it to your needs.

The block is defined by a hook_block() implementation:

/**
 * Implementation of hook_block().
 */
function hs_taxonomy_filter_block_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks['hs-taxonomy-filter'] = array(
      'info'   => t('Hierarchical Select term filter'),
      'weight' => 0,
      'region' => 'left',
      'cache'  => BLOCK_NO_CACHE, // Drupal Forms may never be cached.
    );
    return $blocks;
  }
  else if ($op == 'view') {
    switch($delta) {
      case 'hs-taxonomy-filter':
        $block['subject'] = t('Filter by term');
        $block['content'] = drupal_get_form('hs_taxonomy_filter_block_form');
        return $block;
    }
  }
}

Besides that, there are a form definition function and a form submit callback:

/**
 * Form definition function; Hierarchical Select Taxonomy Filter Block form.
 */
function hs_taxonomy_filter_block_form($form_state) {
  $form['filter'] = array(
    '#type' => 'hierarchical_select',
    '#title' => t('Filter by term'),
    '#description' => t('This is a pointless description.'),
    // Consult API.txt for an explanation of all these settings.
    '#config' => array(
      'module' => 'hs_taxonomy',
      'params' => array(
        'vid' => 1, // Enter your vocabulary ID here.
        'root_term' => NULL, // Enter a term ID here if you want to display only terms below the term with that ID.
      ),
      'save_lineage'    => 0,
      'enforce_deepest' => 1,
      'entity_count'    => 0,
      'require_entity'  => 0,
      'resizable'       => 1,
      'level_labels' => array(
        'status' => 1,
        'labels' => array(
          0 => t('Continent'),
          1 => t('Country'),
          2 => t('State or province'),
          2 => t('City'),
        ),
      ),
      'dropbox' => array(
        // Only allow for a single term or single lineage to be selected.
        'status' => 0,
      ),
      'editability' => array(
        // Creating new terms from within a form to filter by existing terms
        // doesn't make sense, hence it is disabled.
        'status' => 0,
      ),
    ),
    '#default_value' => array(), // To set a default value, fill this with one or more term IDs.
  );

  $form['apply'] = array(
    '#type' => 'submit', 
    '#value' => t('Apply'),
  );

  return $form;
}

/**
 * Form submit callback; Hierarchical Select Taxonomy Filter Block form.
 */
function hs_taxonomy_filter_block_form_submit($form_id, &$form_state) {
  $value = $form_state['values']['filter'];

  // Create an array containing all the term names that have been selected.
  // Even when only a single term has been submitted (i.e. when the dropbox is
  // disabled).
  $terms = array();
  if (is_array($value)) {
    foreach ($value as $term_id) {
      $term = taxonomy_get_term($term_id);
      $terms[] = $term->name;
    }
  }
  else {
    $term = taxonomy_get_term($value);
    $terms[] = $term->name;
  }

  // Maybe you want to display a message?
  $output = t('The following term(s) have been selected: !terms.', array('!terms' => implode(', ', $terms)));
  drupal_set_message($output);

  // Or redirect the user?
  $tid = (is_array($value)) ? reset($value) : $value; // If multiple values are selected, use the first.
  drupal_goto("taxonomy/term/$tid");
}

That's it :)

The sample submit callback does two things:

  1. It displays a message mentioning the selected term(s).
  2. It redirects the user to the term page

These are just examples of what you can do in the submit callback, of course. You can do anything you want.

A .zip file containing this module is attached. Happy Hierarchical Selecting!

http://download.csdn.net/detail/e_zhiwen/9705558


下面是在项目中的应用:
 $select=$form_state['selection'];
   $tid=0;
   foreach(array_keys($select) as $selectid){
	   $tid=$form_state['build_info']['args'][0]->result[$selectid]->field_field_localtion[0]['raw']['tid'];
	   break;
   }
  	$form=array();
	$form['vbo']['localtionID']=  array(
	'#type' => 'hierarchical_select',
    '#title' => t('选择组别'),
    
    // Consult API.txt for an explanation of all these settings.
    '#config' => array(
      'module' => 'hs_taxonomy',
      'params' => array(
        'vid' => 12, // Enter your vocabulary ID here.所在工厂即组别的词汇ID
        'root_term' => 1111, // Enter a term ID here if you want to display only terms below the term with that ID.TSG id=1111
      ),
      'save_lineage'    => 0,
      'enforce_deepest' => 1,
      'entity_count'    => 0,
      'require_entity'  => 0,
      'resizable'       => 1,
      'level_labels' => array(
        'status' => 1,
        'labels' => array(
          0 => t('集团'),
          1 => t('工厂'),
          2 => t('区域'),
          2 => t('组'),
        ),
      ),
      'dropbox' => array(
        // Only allow for a single term or single lineage to be selected.
        'status' => 0,
      ),
      'editability' => array(
        // Creating new terms from within a form to filter by existing terms
        // doesn't make sense, hence it is disabled.
        'status' => 0,
      ),
	  'render_flat_select'=>0,
    ),
    '#default_value' => $tid, // To set a default value, fill this with one or more term IDs.
  );


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值