123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace Drupal\edlp_search\Form;
- use Drupal\Core\Form\FormBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\workflow\Entity\WorkflowManager;
- /**
- * Class EdlpSearchForm.
- */
- class EdlpSearchForm extends FormBase {
- /**
- * {@inheritdoc}
- */
- public function getFormId() {
- return 'edlp_search_form';
- }
- /**
- * {@inheritdoc}
- */
- public function buildForm(array $form, FormStateInterface $form_state) {
- $form['fieldset_search'] = [
- '#type' => 'fieldgroup',
- '#title' => $this->t('Search'),
- 'keys' => [
- '#type' => 'search',
- // '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
- // '#autocomplete_route_parameters' => array('field_name' => 'title'),
- '#maxlength' => 20,
- '#size' => 15,
- '#weight' => '0',
- ]
- ];
- $form['fieldset_langues'] = [
- '#type' => 'fieldgroup',
- '#title' => $this->t('Language'),
- 'language' => [
- '#type' => 'textfield',
- '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
- '#autocomplete_route_parameters' => array('field_name' => 'field_langues'),
- '#maxlength' => 20,
- '#size' => 15,
- '#weight' => '0',
- ]
- ];
- $form['fieldset_genre'] = [
- '#type'=>'fieldgroup',
- '#title' => $this->t('Genres'),
- 'genres' => [
- '#type' => 'textfield',
- '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
- '#autocomplete_route_parameters' => array('field_name' => 'field_genres', 'field_synonyms' => 'field_synonyms'),
- '#maxlength' => 20,
- '#size' => 15,
- '#weight' => '0',
- ]
- ];
- $this->getEntries();
- $form['entries'] = [
- '#type' => 'checkboxes',
- '#title' => $this->t('Entries'),
- '#options' => $this->entries,
- '#weight' => '1',
- ];
- $form['search'] = [
- '#type' => 'submit',
- '#title' => $this->t('Search'),
- '#value' => $this->t('Search'),
- '#weight' => '99',
- ];
- return $form;
- }
- private function getEntries(){
- $query = \Drupal::entityQuery('taxonomy_term')
- ->condition('vid', 'entrees');
- $tids = $query->execute();
- $terms = entity_load_multiple('taxonomy_term', $tids);
- // dsm($terms);
- foreach ($terms as $term) {
- // dpm($term->toArray());
- $tid = $term->id();
- $name = $term->getName();
- // remove masqué
- $sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
- if($sid == 'generique_masque') continue;
- $entries[$tid] = $name;
- }
- asort($entries);
- $this->entries = $entries;
- }
- /**
- * {@inheritdoc}
- */
- public function validateForm(array &$form, FormStateInterface $form_state) {
- parent::validateForm($form, $form_state);
- // if (strlen($form_state->getValue('keys')) < 3) {
- // $form_state->setErrorByName('keys', $this->t('Search string is to short. Please enter more than 3 characters.'));
- // }
- }
- /**
- * {@inheritdoc}
- */
- public function submitForm(array &$form, FormStateInterface $form_state) {
- // Display result.
- foreach ($form_state->getValues() as $key => $value) {
- drupal_set_message($key . ': ' . $value);
- }
- }
- }
|