Hierarchical Select in a block as a taxonomy filter
来源:互联网 发布:python源码下载 编辑:程序博客网 时间:2024/06/05 10:50
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:
- It displays a message mentioning the selected term(s).
- 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
- Hierarchical Select in a block as a taxonomy filter
- A Taxonomy for Performance
- select NULL + '+' + NULL as a
- Bloofi: A Hierarchical Bloom Filter Index with Applications
- Dumping a block in Oracle
- Identifying Hierarchical Structure in Sequences: A linear-time algorithm 1997
- Oracle: Select constant as a column
- A Taxonomy for "Bad Code Smells"
- Retrieve Taxonomy Term by a custom field
- as a
- A Python Script to Disassemble a Block in LLDB
- Securing Android: A Survey, Taxonomy, and Challenges --论文笔记
- Weex This isn't a hierarchical URI.
- Cannot yield a value in the body of a try block with a catch clause
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- Memory as a Programming Concept in C and C++
- SOA security - as a service automatic conversion in distributed infrastructures
- Investing in myself: Realizing my value as a programmer
- 对Verilog 初学者比较有用的整理(转自它处)
- linux无法umount解决方案
- 小白学习html标签归纳
- Android 控件之ImageButton、ImageView
- Ubuntu 安装ruby on rails
- Hierarchical Select in a block as a taxonomy filter
- Controller 规范写法
- Android中dp,sp,mm,in,pt转px值
- Hive 数据抽样
- 导入文件,当Ctrl+A全选时,并不能完全导入的问题。(借鉴网络)
- ubuntu-13.04安装samba共享
- Qt实现网络数据传输(GET)
- sublimeText工具插件和快捷键汇总
- linux下安装mysql数据库