Skip to content

Instantly share code, notes, and snippets.

@Didier-Vaerman
Created April 7, 2016 08:27
Show Gist options
  • Save Didier-Vaerman/6a8af8a877bbd8c7629f5af346134547 to your computer and use it in GitHub Desktop.
Save Didier-Vaerman/6a8af8a877bbd8c7629f5af346134547 to your computer and use it in GitHub Desktop.
Create
var map;
window.initMap = function(){
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
google.maps:
version: VERSION
js:
https://maps.googleapis.com/maps/api/js?key=YourAPIkey&callback=initMap:
type: external
attributes:
async: true
defer: true
<?php
/**
* @file
* Contains \Drupal\tilt_display\Plugin\Block\LocationBlock.
*/
namespace Drupal\tilt_display\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Provides a 'LocationBlock' block.
*
* @Block(
* id = "location_block",
* admin_label = @Translation("Location block"),
* )
*/
class LocationBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['google_maps_api_key'] = array(
'#type' => 'textfield',
'#title' => $this->t('Google Maps API key'),
'#description' => $this->t('Goto the Google Developer Console to generate the API key https://console.developers.google.com.'),
'#default_value' => isset($this->configuration['google_maps_api_key']) ? $this->configuration['google_maps_api_key'] : '',
'#maxlength' => 255,
'#size' => 64,
'#weight' => '0',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['google_maps_api_key'] = $form_state->getValue('google_maps_api_key');
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$url = Url::fromRoute('contact.site_page');
$link = Link::fromTextAndUrl(t("Contact us"), $url);
$link = $link->toRenderable();
$link['#attributes'] = array('class' => array('contact button'));
$contactTitle = t('Meet us !');
$address = t('255 Chaussée de Waterloo, 1060 Brussels, Belgium');
$output = [
'#theme' => 'tilt_display_location',
'#google_maps_api_key' => $this->configuration['google_maps_api_key'],
'#contact_link' => render($link),
'#contact_title' => $contactTitle,
'#address' => $address,
// @todo review pros and cons between attached libraries and attached jss
// @todo review how to pass api key to tilt_display.libraries.yml
/*
'#attached' => [
'js' => [
'https://maps.googleapis.com/maps/api/js?key='.$this->configuration['google_maps_api_key'],
drupal_get_path('module', 'tilt_display') . '/js/tilt_display.js',
]
],
*/
'#attached' => array(
'library' => array(
'tilt_display/tilt_display',
'tilt_display/google.maps',
),
),
];
$build['location_block_google_maps_api_key']['#markup'] = render($output);
return $build;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment