Advertisement
Guest User

Parse PHP Values to JS file when the form is submitted

a guest
Mar 9th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Contains \Drupal\livechat\Form\lcForm.
  6.  */
  7.  
  8. namespace Drupal\livechat\Form;
  9.  
  10. use Drupal\Core\Form\ConfigFormBase;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Drupal\Core\Form\FormStateInterface;
  13.  
  14. class lcForm extends ConfigFormBase {
  15.  
  16.   /**
  17.    * {@inheritdoc}
  18.    */
  19.   public function getFormId() {
  20.     return 'livechat.admin_settings';
  21.   }
  22.  
  23.   /**
  24.    * {@inheritdoc}
  25.    */
  26.   protected function getEditableConfigNames() {
  27.     return [
  28.       'livechat.settings',
  29.     ];
  30.   }
  31.  
  32.   /**
  33.    * {@inheritdoc}
  34.    */
  35.   public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  36.  
  37.     $config = $this->config('livechat.settings');
  38.  
  39.     $form['url'] = array(
  40.       '#type' => 'url',
  41.       '#title' => $this->t('Your server address:'),
  42.       '#required' => true,
  43.       '#attributes' => array(
  44.           'placeholder' => 'saved server: ' . $config->get('server'),
  45.           'autofocus' => TRUE
  46.         )
  47.     );
  48.  
  49.     $form['ip_port'] = array(
  50.       '#type' => 'number',
  51.       '#title' => $this->t('Port:'),
  52.       '#required' => true,
  53.       '#attributes' => array(
  54.           'placeholder' => 'saved port: ' . $config->get('port'),
  55.           'autofocus' => TRUE
  56.         )
  57.     );
  58.  
  59.     return parent::buildForm($form, $form_state);  
  60.  
  61.   }
  62.  
  63.   /**
  64.    * {@inheritdoc}
  65.    */
  66.   public function validateForm(array &$form, FormStateInterface $form_state) {
  67.    
  68.     if(!empty($form_state->getValue('url'))
  69.         && !empty($form_state->getValue('ip_port'))
  70.       ){ // fields are all submitted.
  71.      
  72.          if (5 < strlen($form_state->getValue('ip_port')))
  73.             $form_state->setErrorByName('url', $this->t('Are you sure that is a Port?'));
  74.  
  75.     }
  76.  
  77.   }
  78.  
  79.   /**
  80.    * {@inheritdoc}
  81.    */
  82.   public function submitForm(array &$form, FormStateInterface $form_state) {
  83.  
  84.     $values = $form_state->getValues();
  85.  
  86.     drupal_set_message($this->t('Your server address is @url', array('@url' => $values['url'])));
  87.  
  88.     drupal_set_message($this->t('Listening on @ip_port', array('@ip_port' => $values['ip_port'])));
  89.  
  90.     $this->config('livechat.settings')
  91.       ->clear('server')
  92.       ->set('server', $values['url'])
  93.       ->save();
  94.  
  95.     $this->config('livechat.settings')
  96.       ->clear('port')
  97.       ->set('port', $values['ip_port'])
  98.       ->save();
  99.  
  100.   }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement