EdlpSearchForm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Drupal\edlp_search\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\workflow\Entity\WorkflowManager;
  6. /**
  7. * Class EdlpSearchForm.
  8. */
  9. class EdlpSearchForm extends FormBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function getFormId() {
  14. return 'edlp_search_form';
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function buildForm(array $form, FormStateInterface $form_state) {
  20. $form['fieldset_search'] = [
  21. '#type' => 'fieldgroup',
  22. '#title' => $this->t('Search'),
  23. 'keys' => [
  24. '#type' => 'search',
  25. // '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
  26. // '#autocomplete_route_parameters' => array('field_name' => 'title'),
  27. '#maxlength' => 20,
  28. '#size' => 15,
  29. '#weight' => '0',
  30. ]
  31. ];
  32. $form['fieldset_langues'] = [
  33. '#type' => 'fieldgroup',
  34. '#title' => $this->t('Language'),
  35. 'language' => [
  36. '#type' => 'textfield',
  37. '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
  38. '#autocomplete_route_parameters' => array('field_name' => 'field_langues'),
  39. '#maxlength' => 20,
  40. '#size' => 15,
  41. '#weight' => '0',
  42. ]
  43. ];
  44. $form['fieldset_genre'] = [
  45. '#type'=>'fieldgroup',
  46. '#title' => $this->t('Genres'),
  47. 'genres' => [
  48. '#type' => 'textfield',
  49. '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
  50. '#autocomplete_route_parameters' => array('field_name' => 'field_genres', 'field_synonyms' => 'field_synonyms'),
  51. '#maxlength' => 20,
  52. '#size' => 15,
  53. '#weight' => '0',
  54. ]
  55. ];
  56. $this->getEntries();
  57. $form['entries'] = [
  58. '#type' => 'checkboxes',
  59. '#title' => $this->t('Entries'),
  60. '#options' => $this->entries,
  61. '#weight' => '1',
  62. ];
  63. $form['search'] = [
  64. '#type' => 'submit',
  65. '#title' => $this->t('Search'),
  66. '#value' => $this->t('Search'),
  67. '#weight' => '99',
  68. ];
  69. return $form;
  70. }
  71. private function getEntries(){
  72. $query = \Drupal::entityQuery('taxonomy_term')
  73. ->condition('vid', 'entrees');
  74. $tids = $query->execute();
  75. $terms = entity_load_multiple('taxonomy_term', $tids);
  76. // dsm($terms);
  77. foreach ($terms as $term) {
  78. // dpm($term->toArray());
  79. $tid = $term->id();
  80. $name = $term->getName();
  81. // remove masqué
  82. $sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
  83. if($sid == 'generique_masque') continue;
  84. $entries[$tid] = $name;
  85. }
  86. asort($entries);
  87. $this->entries = $entries;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function validateForm(array &$form, FormStateInterface $form_state) {
  93. parent::validateForm($form, $form_state);
  94. // if (strlen($form_state->getValue('keys')) < 3) {
  95. // $form_state->setErrorByName('keys', $this->t('Search string is to short. Please enter more than 3 characters.'));
  96. // }
  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. }