You are here

GeoCodeAddressBlock.php in IP Geolocation Views & Maps 8

File

src/Plugin/Block/GeoCodeAddressBlock.php
View source
<?php

namespace Drupal\ip_geoloc\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\ip_geoloc\Form\SetLocationForm;
use Drupal\Component\Utility\Xss;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;

/**
 * IPGV&M: Set my location .
 *
 * @Block(
 *   id = "geocode_address",
 *   admin_label = @Translation("IPGV&M: Set my location"),
 *   category = @Translation("IP Geoloc"),
 * )
 */
class GeoCodeAddressBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {

    // Calling the set location form in block.
    $form = $this->formBuilder
      ->getForm(SetLocationForm::class);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return 0;
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface &$form_state) {
    $form = parent::blockForm($form, $form_state);

    // Retrieve existing configuration for this block.
    $config = $this
      ->getConfiguration();

    // Add fields for block configuration.
    $form['address'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Visitor location'),
      '#description' => $this
        ->t('This panel offers various ways for the visitor to set the focus of maps created through this module.'),
    ];
    $note = $this
      ->t('As soon as the newly updated visitor location comes in, this is reflected on the page through an automatic refresh, unless the auto-refresh has been switched off under <a href="!url">Advanced options</a>.', [
      '!url' => url('admin/config/system/ip_geoloc'),
    ]);
    $form['address']['find_visitor'] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#title' => $this
        ->t('"Find me" button'),
      '#description' => $this
        ->t("Displays a button for the visitor to put themselves on the map. If they confirm the browser prompt, this locates the visitor via their device's WiFi and GPS capabilities. This typcially takes 3 seconds.") . ' ' . $note,
    ];
    $form['address']['find_visitor']['ip_geoloc_visitor_find'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Find visitor using Wifi/GPS location detection'),
      '#default_value' => $config
        ->get('ip_geoloc_visitor_find') ? $config
        ->get('ip_geoloc_visitor_find') : TRUE,
    ];
    $form['address']['find_visitor']['ip_geoloc_visitor_reverse_geocode'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Reverse-geocode visitor location to a street address'),
      '#default_value' => $config
        ->get('ip_geoloc_visitor_reverse_geocode') ? $config
        ->get('ip_geoloc_visitor_reverse_geocode') : TRUE,
      '#description' => $this
        ->t('If ticked, reverse-geocoding is executed immeditaley after the visitor position is detected, as part of the same request. It typically adds another 0.3 seconds of waiting time.'),
      '#states' => [
        'visible' => [
          'input[name="ip_geoloc_visitor_find"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['address']['find_visitor']['ip_geoloc_visitor_find_label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label for "Find me" button'),
      '#size' => 30,
      '#default_value' => Xss::filterAdmin($config
        ->get('ip_geoloc_visitor_find_label') ? $config
        ->get('ip_geoloc_visitor_find_label') : ''),
      '#description' => $this
        ->t('Defaults to %default <br/>You may use most HTML tags.', [
        '%default' => IP_GEOLOC_VISITOR_DEFAULT_FIND_LABEL,
      ]),
      '#states' => [
        'visible' => [
          'input[name="ip_geoloc_visitor_find"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['address']['find_visitor']['ip_geoloc_visitor_find_position'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Position of "Find me" button'),
      // Keys represent form element weights.
      '#options' => [
        0 => $this
          ->t('First'),
        7 => $this
          ->t('Second'),
        12 => $this
          ->t('2nd Last'),
        17 => $this
          ->t('Last'),
      ],
      '#default_value' => $config
        ->get('ip_geoloc_visitor_find_position') ? $config
        ->get('ip_geoloc_visitor_find_position') : 0,
      '#description' => $this
        ->t('Relative to the other buttons on the "Set my location" form, if present. These buttons can be configured below.'),
      '#states' => [
        'visible' => [
          'input[name="ip_geoloc_visitor_find"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $throbber_text = $config
      ->get('ip_geoloc_throbber_text') ? $config
      ->get('ip_geoloc_throbber_text') : '';
    $form['address']['find_visitor']['ip_geoloc_throbber_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text displayed while visitor is being geolocated'),
      '#size' => 30,
      '#default_value' => $throbber_text == '<none>' ? $throbber_text : Xss::filterAdmin($throbber_text),
      '#description' => $this
        ->t('A text shown together with an animated image, aka throbber, to show the visitor that geolocation is in progress. </br/>Defaults to %default <br/>You may use most HTML tags. Use <em>&lt;none&gt;</em> to have no throbber and no text.', [
        '%default' => IP_GEOLOC_THROBBER_DEFAULT_TEXT,
      ]),
      '#states' => [
        'visible' => [
          'input[name="ip_geoloc_visitor_find"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['address']['edit'] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => $this
        ->t('"Move to" option with editable location'),
    ];
    $form['address']['edit']['ip_geoloc_visitor_address_editable'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Make address editable'),
      '#default_value' => $config
        ->get('ip_geoloc_visitor_address_editable') ? $config
        ->get('ip_geoloc_visitor_address_editable') : TRUE,
      '#description' => $this
        ->t('If ticked, the visitor may enter a new address or tweak their reverse-geocoded address. It will be geocoded to latitude & longitude to become their new location.<br/>If not ticked, then this field simply displays the current reverse-geocoded address, without a submit button.'),
    ];
    $label = $config
      ->get('ip_geoloc_visitor_address_label') ? $config
      ->get('ip_geoloc_visitor_address_label') : '';
    $form['address']['edit']['ip_geoloc_visitor_address_label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label for editable address'),
      '#size' => 30,
      '#default_value' => $label == '<none>' ? $label : Xss::filterAdmin($label),
      '#description' => $this
        ->t('Defaults to %default. You may use most HTML tags. Use <em>&lt;none&gt;</em> for no label.', [
        '%default' => IP_GEOLOC_VISITOR_DEFAULT_ADDRESS_LABEL,
      ]),
      '#states' => [
        'visible' => [
          'input[name="ip_geoloc_visitor_address_editable"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['address']['display'] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => $this
        ->t('Form element to enter and display address'),
    ];
    $form['address']['display']['ip_geoloc_address_element'] = [
      '#type' => 'radios',
      '#options' => [
        0 => $this
          ->t('text field'),
        1 => $this
          ->t('text area, 1 line'),
        2 => $this
          ->t('text area, 2 lines'),
        3 => $this
          ->t('text area, 3 lines'),
      ],
      '#default_value' => $config
        ->get('ip_geoloc_address_element') ? $config
        ->get('ip_geoloc_address_element') : 2,
      '#description' => $this
        ->t('Text areas may be resized by the visitor, text fields cannot.'),
    ];
    $form['address']['display']['ip_geoloc_visitor_address_default'] = [
      '#type' => 'textarea',
      '#rows' => 2,
      '#title' => $this
        ->t('Default partial address'),
      '#default_value' => Html::escape($config
        ->get('ip_geoloc_visitor_address_default') ? $config
        ->get('ip_geoloc_visitor_address_default') : ''),
      '#description' => $this
        ->t("You may use this to pre-populate the visitor address box with, say, a country, so that the visitor and the geocoding service have a context to the partial address entered. You may use comma's, newlines and blank lines if you wish."),
    ];
    $vocabularies = [];
    foreach (Vocabulary::loadMultiple() as $vid => $vocabulary) {

      // TODO find replacement field_info_instances for D8

      /*
            foreach (field_info_instances('taxonomy_term', $vocabulary->machine_name) as $field_name => $field) {
            $field_info = field_info_field($field_name);
            if ($field_info['type'] == 'geofield') {
            $vocabularies[$vid] = $vocabulary->label();
            break;
            }
            }
      */
    }
    $description = $this
      ->t('The region selector allows the visitor to zoom in on a geographical area, such as a province or wine region, picked from a taxonomy vocabulary. The vocabulary must have a <a target="geofield" href="@url">Geofield</a> attached, providing the latitude & longitude of the centre of the region.', [
      '@url' => url('http://drupal.org/project/geofield'),
    ]);
    $form['address']['region'] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => $this
        ->t('Geographical region selector'),
      '#description' => $description,
    ];
    $form['address']['region']['ip_geoloc_geo_vocabulary_id'] = [
      '#type' => 'select',
      '#options' => [
        0 => $this
          ->t('None'),
      ] + $vocabularies,
      '#default_value' => $config
        ->get('ip_geoloc_geo_vocabulary_id') ? $config
        ->get('ip_geoloc_geo_vocabulary_id') : 0,
    ];
    if (empty($vocabularies)) {
      $form['address']['region']['ip_geoloc_geo_vocabulary_id']['#description'] = $this
        ->t('You do not appear to have any vocabularies on which a Geofield is defined. You cannot enable this feature until you do.');
    }
    $form['address']['region']['ip_geoloc_region_autocomplete'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Widget to select geographical region'),
      '#options' => [
        '0' => $this
          ->t('Select (drop-down)'),
        '1' => $this
          ->t('Autocomplete textfield'),
      ],
      '#default_value' => $config
        ->get('ip_geoloc_region_autocomplete') ? $config
        ->get('ip_geoloc_region_autocomplete') : '0',
      '#states' => [
        'invisible' => [
          'select[name="ip_geoloc_geo_vocabulary_id"]' => [
            'value' => '0',
          ],
        ],
      ],
    ];
    $label = $config
      ->get('ip_geoloc_visitor_region_label') ? $config
      ->get('ip_geoloc_visitor_region_label') : '';
    $form['address']['region']['ip_geoloc_visitor_region_label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label for region selector'),
      '#size' => 30,
      '#default_value' => $label == '<none>' ? $label : Xss::filterAdmin($label),
      '#description' => $this
        ->t('Defaults to %default. You may use most HTML tags. Use <em>&lt;none&gt;</em> for no label.', [
        '%default' => IP_GEOLOC_VISITOR_DEFAULT_REGION_LABEL,
      ]),
      '#states' => [
        'invisible' => [
          'select[name="ip_geoloc_geo_vocabulary_id"]' => [
            'value' => 0,
          ],
        ],
      ],
    ];
    $form['address']['region']['ip_geoloc_region_parent'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Center to use'),
      '#options' => [
        '0' => $this
          ->t('Selected region'),
        '1' => $this
          ->t('Parent of selected region'),
        '2' => $this
          ->t('Grandparent of selected region'),
        '3' => $this
          ->t('Great-grandparent of selected region'),
      ],
      '#default_value' => $config
        ->get('ip_geoloc_region_parent') ? $config
        ->get('ip_geoloc_region_parent') : 0,
      '#description' => $this
        ->t('If the selected parent does not exist the next parent up will be used instead.'),
      '#states' => [
        'invisible' => [
          'select[name="ip_geoloc_geo_vocabulary_id"]' => [
            'value' => '0',
          ],
        ],
      ],
    ];
    $form['redirection'] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => $this
        ->t('Redirection'),
      '#description' => $this
        ->t('Redirection'),
    ];
    global $base_url;
    $form['redirection']['ip_geoloc_address_redirect'] = [
      '#type' => 'textfield',
      '#field_prefix' => "{$base_url}/",
      '#title' => $this
        ->t('Redirect upon submit'),
      '#default_value' => $config
        ->get('ip_geoloc_address_redirect') ? $config
        ->get('ip_geoloc_address_redirect') : '',
      '#description' => $this
        ->t('The page to redirect to after the user has pressed "Go". Use <em>&lt;front&gt;</em> for the front page.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface &$form_state) {

    // Migration comment:  Part of ip_geoloc_block_save for geocode_address delta.
    foreach ($form_state
      ->getValues() as $variable => $value) {
      if (strpos($variable, 'ip_geoloc') === 0) {
        $this->configuration[$variable] = $value;
      }
    }
  }

}

Classes

Namesort descending Description
GeoCodeAddressBlock IPGV&M: Set my location .