class SimpleGMapFormatter in Simple Google Maps 8
Same name and namespace in other branches
- 3.0.x src/Plugin/Field/FieldFormatter/SimpleGMapFormatter.php \Drupal\simple_gmap\Plugin\Field\FieldFormatter\SimpleGMapFormatter
Plugin implementation of the 'simple_gmap' formatter.
Plugin annotation
@FieldFormatter(
id = "simple_gmap",
label = @Translation("Google Map from one-line address"),
field_types = {
"string",
"computed",
"computed_string",
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\simple_gmap\Plugin\Field\FieldFormatter\SimpleGMapFormatter
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of SimpleGMapFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ SimpleGMapFormatter.php, line 22
Namespace
Drupal\simple_gmap\Plugin\Field\FieldFormatterView source
class SimpleGMapFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
"include_map" => TRUE,
"include_static_map" => FALSE,
"include_link" => FALSE,
"include_text" => FALSE,
"iframe_height" => "200",
"iframe_width" => "200",
"iframe_title" => "",
"static_scale" => 1,
"zoom_level" => "14",
"link_text" => "View larger map",
"map_type" => "m",
"langcode" => "en",
"apikey" => "",
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['embedded_label'] = [
'#type' => 'markup',
'#markup' => '<h3>' . $this
->t('Embedded map') . '</h3>',
];
$elements['include_map'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include embedded dynamic map'),
'#default_value' => $this
->getSetting('include_map'),
];
$elements['include_static_map'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include embedded static map'),
'#default_value' => $this
->getSetting('include_static_map'),
];
$elements['apikey'] = [
'#type' => 'textfield',
'#title' => $this
->t('Google Maps API key'),
'#default_value' => $this
->getSetting('apikey'),
'#description' => $this
->t('Static Maps will not work without an API key. See the <a href="https://developers.google.com/maps/documentation/static-maps" target="_blank">Static Maps API page</a> to learn more and obtain a key.'),
'#states' => [
'visible' => [
":input[name*='include_static_map']" => [
'checked' => TRUE,
],
],
'required' => [
':input[name*="include_static_map"]' => [
'checked' => TRUE,
],
],
],
];
$elements['iframe_width'] = [
'#type' => 'textfield',
'#title' => $this
->t('Width of embedded map'),
'#default_value' => $this
->getSetting('iframe_width'),
'#description' => $this
->t('You can set sizes in px or percent (ex: 600px or 100%). Note that static maps only accept sizes in pixels, without the suffix px (ex: 600).'),
'#size' => 10,
];
$elements['iframe_height'] = [
'#type' => 'textfield',
'#title' => $this
->t('Height of embedded map'),
'#default_value' => $this
->getSetting('iframe_height'),
'#description' => $this
->t('You can set sizes in px or percent (ex: 600px or 100%). Note that static maps only accept sizes in pixels, without the suffix px (ex: 600).'),
'#size' => 10,
];
$elements['iframe_title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title of the iframe for embedded map'),
'#default_value' => $this
->getSetting('iframe_title'),
'#description' => $this
->t('The embedded map is in an iframe HTML tag, which should have a title attribute for screen readers (not shown on the page). Use [address] to insert the address text in the title.'),
];
$elements['static_scale'] = [
'#title' => $this
->t('Load Retina sized static image'),
'#type' => 'select',
'#description' => $this
->t('Choose "Yes" to double the width and height of the static image for use on retina displays. (Only applicable to static map)'),
'#options' => [
1 => $this
->t('No'),
2 => $this
->t('Yes'),
],
'#default_value' => (int) $this
->getSetting('static_scale'),
];
$elements['link_label'] = [
'#type' => 'markup',
'#markup' => '<h3>' . $this
->t('Link to map') . '</h3>',
];
$elements['include_link'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include link to map'),
'#default_value' => $this
->getSetting('include_link'),
];
$elements['link_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link text'),
'#default_value' => $this
->getSetting('link_text'),
'#description' => $this
->t("Enter the text to use for the link to the map, or enter 'use_address' (without the quotes) to use the entered address text as the link text"),
];
$elements['generic_label'] = [
'#type' => 'markup',
'#markup' => '<h3>' . $this
->t('General settings') . '</h3>',
];
$elements['zoom_level'] = [
'#type' => 'select',
'#options' => [
1 => $this
->t('1 - Minimum'),
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10,
11 => 11,
12 => 12,
13 => 13,
14 => $this
->t('14 - Default'),
15 => 15,
16 => 16,
17 => 17,
18 => 18,
19 => 19,
20 => $this
->t('20 - Maximum'),
],
'#title' => $this
->t('Zoom level'),
'#default_value' => $this
->getSetting('zoom_level'),
];
$elements['include_text'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include original address text'),
'#default_value' => $this
->getSetting('include_text'),
];
$elements['map_type'] = [
'#type' => 'select',
'#title' => $this
->t('Map type'),
'#description' => $this
->t('Choose a default map type for embedded and linked maps'),
'#options' => [
'm' => $this
->t('Map'),
'k' => $this
->t('Satellite'),
'h' => $this
->t('Hybrid'),
'p' => $this
->t('Terrain'),
],
'#default_value' => $this
->getSetting('map_type'),
];
$elements['langcode'] = [
'#type' => 'textfield',
'#title' => $this
->t('Language'),
'#default_value' => $this
->getSetting('langcode'),
'#description' => $this
->t("Enter a two-letter language code that Google Maps can recognize, or enter 'page' (without the quotes) to use the current page's language code"),
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$map_types = [
'm' => $this
->t('Map'),
'k' => $this
->t('Satellite'),
'h' => $this
->t('Hybrid'),
'p' => $this
->t('Terrain'),
];
$map_type = $this
->getSetting('map_type') ? $this
->getSetting('map_type') : 'm';
$map_type = isset($map_types[$map_type]) ? $map_types[$map_type] : $map_types['m'];
$include_map = $this
->getSetting('include_map');
if ($include_map) {
$summary[] = $this
->t('Dynamic map: @width x @height', [
'@width' => $this
->getSetting('iframe_width'),
'@height' => $this
->getSetting('iframe_height'),
]);
$summary[] = $this
->t('Title of the iframe: @title', [
'@title' => $this
->getSetting('iframe_title'),
]);
}
$include_static_map = $this
->getSetting('include_static_map');
if ($include_static_map) {
$summary[] = $this
->t('Static map: @width x @height, Scale: @static_scale', [
'@width' => $this
->getSetting('iframe_width'),
'@height' => $this
->getSetting('iframe_height'),
'@static_scale' => $this
->getSetting('static_scale'),
]);
}
$include_link = $this
->getSetting('include_link');
if ($include_link) {
$summary[] = $this
->t('Map link: @link_text', [
'@link_text' => $this
->getSetting('link_text'),
]);
}
if ($include_link || $include_map || $include_static_map) {
$summary[] = $this
->t('Map Type: @map_type', [
'@map_type' => $map_type,
]);
$summary[] = $this
->t('Zoom Level: @zoom_level', [
'@zoom_level' => $this
->getSetting('zoom_level'),
]);
$summary[] = $this
->t('Language: @language', [
'@language' => $this
->getSetting('langcode'),
]);
}
$include_text = $this
->getSetting('include_text');
if ($include_text) {
$summary[] = $this
->t('Original text displayed');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$settings = $this
->getSettings();
$include_text = $settings['include_text'];
$zoom_level = (int) $settings['zoom_level'];
// For some reason, static gmaps accepts a different value for map type.
$static_map_types = [
'm' => 'roadmap',
'k' => 'satellite',
'h' => 'hybrid',
'p' => 'terrain',
];
$map_type = $settings['map_type'];
// Figure out a language code to use. Google cannot recognize 'und'.
if ($settings['langcode'] == 'page') {
$lang_to_use = $langcode;
}
else {
$lang_to_use = [
'#plain_text' => $settings['langcode'],
];
}
foreach ($items as $delta => $item) {
$url_value = urlencode($item->value);
$address_value = $item->value;
$address = $include_text ? $address_value : '';
$text_for_link = $settings['link_text'] == 'use_address' ? $address_value : $settings['link_text'];
$link_text = [
'#plain_text' => $text_for_link,
];
$iframe_title = $settings['iframe_title'];
$iframe_title_value = [
'#plain_text' => str_replace('[address]', $address_value, $iframe_title),
];
$element[$delta] = [
'#theme' => 'simple_gmap_output',
'#include_map' => $settings['include_map'],
'#include_static_map' => $settings['include_static_map'],
'#include_link' => $settings['include_link'],
'#include_text' => $settings['include_text'],
'#width' => [
'#plain_text' => $settings['iframe_width'],
],
'#height' => [
'#plain_text' => $settings['iframe_height'],
],
'#iframe_title' => $iframe_title_value,
'#static_scale' => (int) $settings['static_scale'],
'#url_suffix' => $url_value,
'#zoom' => $zoom_level,
'#link_text' => $link_text,
'#address_text' => $address,
'#map_type' => $map_type,
'#langcode' => $lang_to_use,
'#static_map_type' => $static_map_types[$map_type],
'#apikey' => $settings['apikey'],
];
}
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The label display setting. | |
FormatterBase:: |
protected | property |
The formatter settings. Overrides PluginSettingsBase:: |
|
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
11 |
FormatterBase:: |
protected | function | Returns the value of a field setting. | |
FormatterBase:: |
protected | function | Returns the array of field settings. | |
FormatterBase:: |
public static | function |
Returns if the formatter can be used for the provided field. Overrides FormatterInterface:: |
14 |
FormatterBase:: |
public | function |
Allows formatters to load information for field values being displayed. Overrides FormatterInterface:: |
2 |
FormatterBase:: |
public | function |
Builds a renderable array for a fully themed field. Overrides FormatterInterface:: |
1 |
FormatterBase:: |
public | function |
Constructs a FormatterBase object. Overrides PluginBase:: |
11 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
SimpleGMapFormatter:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsBase:: |
|
SimpleGMapFormatter:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FormatterBase:: |
|
SimpleGMapFormatter:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FormatterBase:: |
|
SimpleGMapFormatter:: |
public | function |
Builds a renderable array for a field value. Overrides FormatterInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |