Advertisement
Guest User

Untitled

a guest
May 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. <?php
  2. namespace Drupal\drupalup_simple_form\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Ajax\AjaxResponse;
  6. use Drupal\Core\Ajax\HtmlCommand;
  7.  
  8. /**
  9.  * Our simple form class.
  10.  */
  11. class SimpleForm extends FormBase {
  12.   /**
  13.    * {@inheritdoc}
  14.    */
  15.   public function getFormId() {
  16.     return 'drupalup_simple_form';
  17.   }
  18.  
  19.  
  20.   /**
  21.    * {@inheritdoc}
  22.    */
  23.  
  24.  public function buildForm(array $form, FormStateInterface $form_state) {
  25.   $form['search'] = array(
  26.     '#type' => 'textfield',
  27.     '#title' => t('Search:'),
  28.   );
  29.   $form['submit'] = array(
  30.     '#type' => 'submit',
  31.     '#value' => $this->t('Submit'),
  32.     '#button_type' => 'primary',
  33.     '#ajax' => array(
  34.       'callback' => [$this, 'submitForm'],
  35.       'event' => 'click',
  36.     ),
  37.   );
  38.   return $form;
  39. }
  40.  
  41.  
  42.  
  43.   /**
  44.    * {@inheritdoc}
  45.    */
  46.  
  47.   //public function submitForm(array &$form, FormStateInterface $form_state) {
  48.     // drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('candidate_name'))));
  49.     // foreach ($form_state->getValues($results) as $result) {
  50.       // print_r($json_string= (string) $results->getBody());
  51.     // }
  52.    // }
  53.  
  54.   /* public function submitForm(array &$form, FormStateInterface $form_state) {
  55.     * drupal_set_message(
  56.      *   $form_state->getValue($results)
  57.       *  );
  58.   * }
  59. */
  60.   public function submitForm(array &$form, FormStateInterface $form_state) {
  61.     $url = 'http://systembevakningsagenten.se/api/json/1.0/searchProduct.json?query=';
  62.     $term = $form_state->getValue('search');
  63.     $query_url = $url. $term;
  64.     $client =  \Drupal::httpClient();
  65.     $request = $client->get($query_url);
  66.     $data = json_decode($request->getBody(), TRUE);
  67.     $results = [];
  68.     if (!empty($data) && !empty($data['items'])) {
  69.       foreach ($data['items'] as $item ) {
  70.         $results[] = $item['name'];
  71.       }
  72.     }
  73.      }
  74.  
  75. }
  76. ?>
  77.  
  78. Message: <input type="text" id="message">
  79. <input type="submit" onclick='PostMessage()'>
  80. <script>
  81. function PostMessage() {
  82. var comment = document.getElementById('message').value;
  83.     window.location.assign('http://systembevakningsagenten.se/api/json/1.0/searchProduct.json?query='+comment)
  84. }
  85. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement