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:

  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