You are here

class GeoCodeAddressBlock in IP Geolocation Views & Maps 8

IPGV&M: Set my location .

Plugin annotation


@Block(
  id = "geocode_address",
  admin_label = @Translation("IPGV&M: Set my location"),
  category = @Translation("IP Geoloc"),
)

Hierarchy

Expanded class hierarchy of GeoCodeAddressBlock

File

src/Plugin/Block/GeoCodeAddressBlock.php, line 21

Namespace

Drupal\ip_geoloc\Plugin\Block
View source
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;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockAccess protected function Indicates whether the block should be shown. 16
BlockPluginTrait::blockValidate public function 3
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. 2
BlockPluginTrait::calculateDependencies public function
BlockPluginTrait::defaultConfiguration public function 19
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit().
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). 1
BlockPluginTrait::__construct public function 22
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GeoCodeAddressBlock::blockForm public function Overrides BlockPluginTrait::blockForm
GeoCodeAddressBlock::blockSubmit public function Overrides BlockPluginTrait::blockSubmit
GeoCodeAddressBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
GeoCodeAddressBlock::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides ContextAwarePluginBase::getCacheMaxAge
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2