MaterioSapiSearchForm.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\materio_sapi\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. /**
  6. * Class MaterioSapiSearchForm.
  7. */
  8. class MaterioSapiSearchForm extends FormBase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function getFormId() {
  13. return 'materio_sapi_search_form';
  14. }
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function buildForm(array $form, FormStateInterface $form_state) {
  19. $lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
  20. $form['search'] = [
  21. '#type' => 'textfield',
  22. // '#title' => $this->t('Search'),
  23. '#maxlength' => 64,
  24. '#size' => 25,
  25. '#weight' => '0',
  26. '#attributes' => [
  27. "placeholder" => $this->t('Search'),
  28. // "@keyup" => "keyup",
  29. // "@keyup.enter" => "submit",
  30. "v-model" => "typed",
  31. "v-focus" => "",
  32. // "v-on:select" => "typed",
  33. ],
  34. '#autocomplete_route_name' => 'materio_sapi.search_autocomplete',
  35. ];
  36. $form['searchautocomplete'] = [
  37. '#type' => 'hidden',
  38. '#attributes' => [
  39. "v-model" => "autocomplete"
  40. ],
  41. ];
  42. $form['filters'] = [
  43. '#type' => 'fieldset',
  44. '#title' => t('Assisted Search'),
  45. '#legend_attributes' => [
  46. "class" => ["test-attribute"],
  47. '@click.prevent' => "onClickFilters",
  48. ]
  49. ];
  50. // $form['filters']['switch'] = [
  51. // '#type' => "label",
  52. //
  53. // ]
  54. $query = \Drupal::entityQuery('taxonomy_term');
  55. $query->condition('vid', "assisted_research")
  56. ->sort('field_weight', 'ASC');
  57. $tids = $query->execute();
  58. $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
  59. foreach ($terms as $tid => $term) {
  60. if($term->hasTranslation($lang)){
  61. $term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $lang);
  62. }
  63. $options = array($term->getName());
  64. $childs = $term->get('field_terms')->getValue();
  65. foreach ($childs as $child) {
  66. $child_term = \Drupal\taxonomy\Entity\Term::load($child['target_id']);
  67. if($child_term->hasTranslation($lang)){
  68. $child_term = \Drupal::service('entity.repository')->getTranslationFromContext($child_term, $lang);
  69. }
  70. $options[$child_term->id()] = $child_term->getName();
  71. }
  72. $form['filters']['filter-'.$tid] = array(
  73. '#type' => 'select',
  74. // '#title' => $term->getName(),
  75. // '#multiple' => true,
  76. '#options' => $options
  77. );
  78. }
  79. $form['submit'] = array(
  80. '#type' => 'button',
  81. // '#src' => '',//drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  82. '#value' => t('Search'),
  83. // '#prefix' => '<div class="search-btn-wrapper">',
  84. // '#suffix'=> '</div>',
  85. '#attributes' => [
  86. 'title' => t('Search'),
  87. '@click.prevent' => "submit",
  88. ]
  89. );
  90. return $form;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function validateForm(array &$form, FormStateInterface $form_state) {
  96. parent::validateForm($form, $form_state);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function submitForm(array &$form, FormStateInterface $form_state) {
  102. // Display result.
  103. foreach ($form_state->getValues() as $key => $value) {
  104. drupal_set_message($key . ': ' . $value);
  105. }
  106. }
  107. }