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.
  );


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章