Advertisement
Guest User

example_hs

a guest
Oct 13th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.35 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Implements hook_menu()
  5.  */
  6. function example_hs_menu() {
  7.   $items['example-hs'] = array(
  8.     'title' => 'Example Hierarchical Select',
  9.     'page callback' => 'drupal_get_form',
  10.     'page arguments' => array('example_hs_example_form'),
  11.     'access arguments' => array('access content'),
  12.   );
  13.   return $items;
  14. }
  15.  
  16. /**
  17.  * Form builder
  18.  */
  19. function example_hs_example_form($form, $form_state) {
  20.   // Враппер для селектов
  21.   $form['terms'] = array(
  22.     '#tree' => TRUE,
  23.     '#prefix' => '<div id="terms">',
  24.     '#suffix' => '</div>',
  25.   );
  26.  
  27.   // Если сработает AJAX, то в $form_state['values']['terms'] будут выбранные значения селектов
  28.   $terms_values = isset($form_state['values']['terms']) ? $form_state['values']['terms'] : array();
  29.   // Селектов будет генерится на 1 больше чем есть в $form_state['values']['terms']
  30.   $terms_values += array('term' . (count($terms_values) + 1) => 'none');
  31.   // В первом селекте будут выводится термины без родителя
  32.   $parent_tid = 0;
  33.  
  34.   foreach ($terms_values as $input_name => $tid) {
  35.     // Завершаем цикл если в предыдущем селекте ничего не выбрано
  36.     // или у термина нет дочерних терминов
  37.     if ($parent_tid === 'none' || !($terms = taxonomy_get_tree(6, $parent_tid, 1))) {
  38.       break;
  39.     }
  40.  
  41.     // Формируем данные для селекта
  42.     $options = array('none' => t('- Any -'));
  43.     foreach ($terms as $term) {
  44.       $options[$term->tid] = $term->name;
  45.     }
  46.  
  47.     // Создаём сам селект
  48.     $form['terms'][$input_name] = array(
  49.       '#title' => $parent_tid == 'none' ? t('Region') : t('Town'), 
  50.       '#type' => 'select',
  51.       '#options' => $options,
  52.       '#default_value' => $tid,
  53.       '#ajax' => array(
  54.         'callback' => 'example_hs_example_form_ajax_callback',
  55.         'wrapper' => 'terms',
  56.       ),
  57.     );
  58.    
  59.    
  60.     $form['terms']['term2'] = array(
  61.       '#title' => t('Town'),   
  62.       '#type' => 'select',
  63.       '#options' => $options,
  64.       '#default_value' => $tid,
  65.       '#attributes' => $parent_tid != 'none' ? '' : array('disabled' => array('disabled')),
  66.     );
  67.        
  68.    $parent_tid = isset($options[$tid]) ? $tid : 'none';
  69.   }
  70.  
  71.   $form['submit'] = array(
  72.     '#type' => 'submit',
  73.     '#value' => 'Submit',
  74.   );
  75.  
  76.   return $form;
  77. }
  78.  
  79. /**
  80.  * AJAX callback
  81.  */
  82. function example_hs_example_form_ajax_callback($form, $form_state) {
  83.   return $form['terms'];
  84. }
  85.  
  86. /**
  87.  * Submit callback
  88.  */
  89. function example_hs_example_form_submit($form, $form_state) {
  90.   dsm($form_state);
  91. }
  92.  
  93.  
  94. /**
  95.  * Реализация hook_block_info()
  96.  * Информация о блоке и дефолтные настройки
  97.  */
  98. function example_hs_block_info() {
  99.   $blocks['hello-message'] = array('info' => t('Message'));
  100.   return $blocks;
  101. }
  102.  
  103. /**
  104.  * Реализация hook_block_view()
  105.  * Генерация заголовка блока и его контента
  106.  */
  107. function example_hs_block_view($delta = '') {
  108.   $block = array();
  109.  
  110.   if ($delta == 'hello-message') {
  111.     $block['subject'] = t('Message');
  112.     $block['content'] = drupal_render(drupal_get_form('example_hs_example_form'));
  113.   }
  114.   return $block;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement