Skip to content

Commit 453c1b1

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-88225-google-map-api-configuration' into cms-team-2-sprint-8
2 parents b27b900 + 2783853 commit 453c1b1

File tree

24 files changed

+1196
-38
lines changed

24 files changed

+1196
-38
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Block\Adminhtml\System\Config;
9+
10+
class GoogleMapsApiValidator extends \Magento\Config\Block\System\Config\Form\Field
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
protected function _prepareLayout()
16+
{
17+
parent::_prepareLayout();
18+
$this->setTemplate('Magento_PageBuilder::system/config/google_maps_api_validator.phtml');
19+
return $this;
20+
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
26+
{
27+
$element = clone $element;
28+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
29+
return parent::render($element);
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
36+
{
37+
$originalData = $element->getOriginalData();
38+
$this->addData(
39+
[
40+
'button_label' => __($originalData['button_label']),
41+
'valid_label' => __($originalData['valid_label']),
42+
'invalid_label' => __($originalData['invalid_label']),
43+
'source_field' => $originalData['source_field'],
44+
'html_id' => $element->getHtmlId(),
45+
'validate_url' => $this->_urlBuilder->getUrl('pagebuilder/googlemaps/validateapi')
46+
]
47+
);
48+
49+
return $this->_toHtml();
50+
}
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Block;
9+
10+
class GoogleMapsApiBlock extends \Magento\Framework\View\Element\Template
11+
{
12+
const GOOGLE_MAPS_API_KEY_PATH = 'cms/pagebuilder/google_maps_api_key';
13+
const GOOGLE_MAPS_LIBRARY_URL = 'https://maps.googleapis.com/maps/api/js?v=3&key=%s';
14+
15+
/**
16+
* Generate URL for retrieving Google Maps Javascript API
17+
*
18+
* @return string
19+
*/
20+
public function getGoogleMapsApiPath(): string
21+
{
22+
$apiKey = $this->_scopeConfig->getValue(self::GOOGLE_MAPS_API_KEY_PATH);
23+
$libraryUrlWithKey = sprintf(self::GOOGLE_MAPS_LIBRARY_URL, $apiKey);
24+
return $libraryUrlWithKey;
25+
}
26+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Component;
10+
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\UrlInterface;
13+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
14+
use Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator;
15+
16+
class MapContainer extends \Magento\Ui\Component\Container
17+
{
18+
const GOOGLE_MAPS_API_KEY_PATH = 'cms/pagebuilder/google_maps_api_key';
19+
20+
/**
21+
* @var UrlInterface $url
22+
*/
23+
private $url;
24+
25+
/**
26+
* @var ScopeConfigInterface $scopeConfig
27+
*/
28+
private $scopeConfig;
29+
30+
/**
31+
* @var ApiKeyValidator $ApiKeyValidator
32+
*/
33+
private $ApiKeyValidator;
34+
35+
/**
36+
* Constructor
37+
*
38+
* @param ContextInterface $context
39+
* @param UrlInterface $url
40+
* @param ScopeConfigInterface $scopeConfig
41+
* @param ApiKeyValidator $ApiKeyValidator
42+
* @param array $components
43+
* @param array $data
44+
*/
45+
public function __construct(
46+
ContextInterface $context,
47+
UrlInterface $url,
48+
ScopeConfigInterface $scopeConfig,
49+
ApiKeyValidator $ApiKeyValidator,
50+
array $components = [],
51+
array $data = []
52+
) {
53+
parent::__construct(
54+
$context,
55+
$components,
56+
$data
57+
);
58+
$this->url = $url;
59+
$this->scopeConfig = $scopeConfig;
60+
$this->ApiKeyValidator = $ApiKeyValidator;
61+
}
62+
63+
/**
64+
* Prepare component configuration
65+
*
66+
* @return void
67+
*/
68+
public function prepare()
69+
{
70+
parent::prepare();
71+
$config = $this->getData('config');
72+
$apiKey = $this->scopeConfig->getValue(self::GOOGLE_MAPS_API_KEY_PATH) ?: "";
73+
$response = $this->ApiKeyValidator->validate($apiKey);
74+
if (!$response['success']) {
75+
$config['visible'] = true;
76+
}
77+
78+
if (isset($config['map_configuration_url'])) {
79+
$config['map_configuration_url'] = $this->url->getUrl($config['map_configuration_url']);
80+
}
81+
if (isset($config['content'])) {
82+
$config['content'] = sprintf($config['content'], $config['map_configuration_url']);
83+
}
84+
85+
$this->setData('config', (array) $config);
86+
}
87+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Controller\Adminhtml\GoogleMaps;
9+
10+
use Magento\Framework\Controller\ResultFactory;
11+
12+
class ValidateApi extends \Magento\Backend\App\Action
13+
{
14+
const ADMIN_RESOURCE = 'Magento_Backend::content';
15+
16+
/**
17+
* @var \Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator
18+
*/
19+
private $ApiKeyValidator;
20+
21+
/**
22+
* Constructor
23+
*
24+
* @param \Magento\Backend\App\Action\Context $context
25+
* @param \Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator $ApiKeyValidator
26+
*/
27+
public function __construct(
28+
\Magento\Backend\App\Action\Context $context,
29+
\Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator $ApiKeyValidator
30+
) {
31+
parent::__construct($context);
32+
$this->ApiKeyValidator = $ApiKeyValidator;
33+
}
34+
35+
/**
36+
* Send test request to Google Maps and return response
37+
*
38+
* @return Json
39+
*/
40+
public function execute()
41+
{
42+
$apiKey = $this->getRequest()->getParam('key');
43+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($this->ApiKeyValidator->validate($apiKey));
44+
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Model\GoogleMaps;
9+
10+
class ApiKeyValidator
11+
{
12+
const GOOGLE_MAPS_EMBED_URL = 'https://www.google.com/maps/embed/v1/place?key=%s&q=Austin+TX';
13+
14+
/**
15+
* Send test request to Google Maps and return response
16+
*
17+
* @param string $apiKey
18+
* @return array
19+
*/
20+
public function validate(string $apiKey): array
21+
{
22+
$testUrl = sprintf(self::GOOGLE_MAPS_EMBED_URL, $apiKey);
23+
$curl = curl_init($testUrl);
24+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
25+
$result = curl_exec($curl);
26+
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
27+
$returnArray = [
28+
'responseMessage' => $responseCode !== 200 ? $result : '',
29+
'success' => $responseCode === 200 ? true : false
30+
];
31+
32+
return $returnArray;
33+
}
34+
}

app/code/Magento/PageBuilder/etc/adminhtml/system.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
<![CDATA[Includes advanced tools to design page layouts, add Magento content (Product and Category data, CMS content and Blocks), and format text.<br /><br />For Product Attributes — Page Builder can be selected as the “input type” to design layouts and create content.]]>
1919
</comment>
2020
</field>
21+
<field id="google_maps_api_key" translate="label comment" type="text" sortOrder="2010" showInDefault="1" showInWebsite="0" showInStore="0">
22+
<label>Google Maps API Key</label>
23+
<comment>
24+
<![CDATA[Enter API key to use the Map content block. <a href="https://developers.google.com/maps/documentation/javascript/get-api-key" target="_blank">Get API Key</a>.]]>
25+
</comment>
26+
</field>
27+
<field id="google_maps_api_key_validator" translate="button_label valid_label invalid_label" sortOrder="2011" showInDefault="1" showInWebsite="0" showInStore="0">
28+
<attribute type="button_label">Test Key</attribute>
29+
<attribute type="valid_label">Key is valid</attribute>
30+
<attribute type="invalid_label">Key is invalid. Try different key.</attribute>
31+
<attribute type="source_field">cms_pagebuilder_google_maps_api_key</attribute>
32+
<frontend_model>Magento\PageBuilder\Block\Adminhtml\System\Config\GoogleMapsApiValidator</frontend_model>
33+
</field>
2134
</group>
2235
</section>
2336
</system>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* @var \Magento\PageBuilder\Block\Adminhtml\System\Config\GoogleMapsApiValidator $block
9+
*/
10+
?>
11+
<button class="scalable"
12+
type="button"
13+
id="<?= $block->escapeHtmlAttr($block->getHtmlId()) ?>"
14+
data-mage-init='{
15+
"Magento_PageBuilder/js/system/config/google-maps-api-validator": {
16+
"elementId": "<?= $block->escapeJs($block->getHtmlId()) ?>",
17+
"validateUrl": "<?= $block->escapeJs($block->getValidateUrl()) ?>",
18+
"buttonLabel": "<?= $block->escapeJs($block->getButtonLabel()) ?>",
19+
"validLabel": "<?= $block->escapeJs($block->getValidLabel()) ?>",
20+
"invalidLabel": "<?= $block->escapeJs($block->getInvalidLabel()) ?>",
21+
"sourceField": "<?= $block->escapeJs($block->getSourceField()) ?>"
22+
}
23+
}'
24+
disabled="disabled"
25+
><span class="result"><?= $block->escapeHtml($block->getButtonLabel()) ?></span></button>

app/code/Magento/PageBuilder/view/adminhtml/ui_component/pagebuilder_map_form.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@
6262
</formElements>
6363
</field>
6464
</fieldset>
65-
<fieldset name="general" sortOrder="20">
65+
<container sortOrder="20" name="google_map_api_key_check" component="Magento_Ui/js/form/components/html" class="Magento\PageBuilder\Component\MapContainer">
66+
<argument name="data" xsi:type="array">
67+
<item name="config" xsi:type="array">
68+
<item name="additionalClasses" xsi:type="string">message message-warning</item>
69+
<item name="visible" xsi:type="boolean">false</item>
70+
<item name="map_configuration_url" xsi:type="string">admin/system_config/edit/section/cms</item>
71+
<item name="content" xsi:type="string" translate="true"><![CDATA[You must provide the <a href="%s">Google Maps API key</a> in order to use the map]]></item>
72+
</item>
73+
</argument>
74+
</container>
75+
<fieldset name="general" sortOrder="30">
6676
<settings>
6777
<label/>
6878
<collapsible>true</collapsible>

app/code/Magento/PageBuilder/view/adminhtml/web/css/source/_icons.less

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,19 @@
171171
content: @icon-systems__content;
172172
}
173173
}
174+
175+
.icon-admin-pagebuilder-success {
176+
.abs-icon;
177+
&:before {
178+
color: @success__color;
179+
content: @icon-check-mage__content;
180+
}
181+
}
182+
183+
.icon-admin-pagebuilder-error {
184+
.abs-icon;
185+
&:before {
186+
color: @error__color;
187+
content: @icon-error__content;
188+
}
189+
}

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
define([
99
'Magento_Ui/js/form/element/abstract',
10-
'https://maps.googleapis.com/maps/api/js?key=AIzaSyCw10cOO31cpxb2bcwnHPHKtxov8oUbxJw'
10+
'googleMaps'
1111
], function (AbstractField) {
1212
'use strict';
1313

0 commit comments

Comments
 (0)