Example #1
0
 /**
  * Add a type selector to the value form
  */
 protected function valueForm(&$form, FormStateInterface $form_state)
 {
     if (!$form_state->get('exposed')) {
         $form['value']['type'] = array('#type' => 'radios', '#title' => $this->t('Value type'), '#options' => array('date' => $this->t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'), 'offset' => $this->t('An offset from the current time such as "@example1" or "@example2"', array('@example1' => '+1 day', '@example2' => '-2 hours -30 minutes'))), '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date');
     }
     parent::valueForm($form, $form_state);
 }
 /**
  * Add a type selector to the value form
  */
 protected function valueForm(&$form, FormStateInterface $form_state)
 {
     parent::valueForm($form, $form_state);
     // Get the value element.
     if (isset($form['value']['#tree'])) {
         $value_element =& $form['value'];
     } else {
         $value_element =& $form;
     }
     // Set the value title to Distance.
     $value_element['value']['#title'] = $this->t('Distance');
     $value_element['value']['#weight'] = 30;
     // Create the units element if not exposed or exposing units is enabled.
     $units = ($exposed = $form_state->get('exposed') && !$this->options['expose_units']) ? [] : ['units' => ['#type' => 'select', '#default_value' => !empty($this->value['units']) ? $this->value['units'] : '', '#weight' => 40, '#options' => ['mile' => $this->t('Miles'), 'km' => $this->t('Kilometers')]]];
     // Add the Latitude and Longitude elements.
     $value_element += ['lat' => ['#type' => 'hidden', '#default_value' => !empty($this->value['lat']) ? $this->value['lat'] : '', '#weight' => 10, '#attributes' => ['class' => ['geolocation-hidden-lat']]], 'lng' => ['#type' => 'hidden', '#default_value' => !empty($this->value['lng']) ? $this->value['lng'] : '', '#weight' => 20, '#attributes' => ['class' => ['geolocation-hidden-lng']]]] + $units;
     // The 'Get my location' button.
     $button_html = '<div class="geolocation-html5-button">';
     $button_html .= '<span class="default">' . $this->t('Get browser location') . '</span>';
     $button_html .= '<span class="location"></span>';
     $button_html .= '<div class="search"></div>';
     $button_html .= '<div class="clear"></div>';
     $button_html .= '</div>';
     $value_element['get_location'] = ['#markup' => $button_html];
     // Attach the html5 library.
     $value_element['#attached'] = ['library' => ['geolocation/geolocation.widgets.html5']];
     if (!$form_state->get('exposed')) {
         $form['expose_units'] = ['#type' => 'checkbox', '#title' => $this->t('Expose Units?'), '#default_value' => !empty($this->options['expose_units']) ? $this->options['expose_units'] : FALSE, '#states' => ['visible' => [['input[name="options[expose_button][checkbox][checkbox]"]' => ['checked' => TRUE]]]], '#weight' => -1000];
     }
 }
 /**
  * Add a type selector to the value form
  */
 protected function valueForm(&$form, FormStateInterface $form_state)
 {
     parent::valueForm($form, $form_state);
     // Get the value element.
     if (isset($form['value']['#tree'])) {
         $value_element =& $form['value'];
     } else {
         $value_element =& $form;
     }
     // Set the value title to Distance.
     $value_element['value']['#title'] = $this->t('Distance');
     $value_element['value']['#weight'] = 30;
     // Create the units element if not exposed or exposing units is enabled.
     $units = ($exposed = $form_state->get('exposed') && !$this->options['expose_units']) ? [] : ['units' => ['#type' => 'select', '#default_value' => !empty($this->value['units']) ? $this->value['units'] : '', '#weight' => 40, '#options' => ['mile' => $this->t('Miles'), 'km' => $this->t('Kilometers')]]];
     // Add the Latitude and Longitude elements.
     $value_element += ['lat' => ['#type' => 'textfield', '#title' => $this->t('Latitude'), '#default_value' => !empty($this->value['lat']) ? $this->value['lat'] : '', '#weight' => 10], 'lng' => ['#type' => 'textfield', '#title' => $this->t('Longitude'), '#default_value' => !empty($this->value['lng']) ? $this->value['lng'] : '', '#weight' => 20]] + $units;
     if (!$form_state->get('exposed')) {
         $form['expose_units'] = ['#type' => 'checkbox', '#title' => $this->t('Expose Units?'), '#default_value' => !empty($this->options['expose_units']) ? $this->options['expose_units'] : FALSE, '#states' => ['visible' => [['input[name="options[expose_button][checkbox][checkbox]"]' => ['checked' => TRUE]]]], '#weight' => -1000];
     }
 }