Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACMS-653: Changes in dashboard google API form is breaking installation #655

Merged
merged 3 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions modules/acquia_cms_tour/src/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\acquia_cms_tour\Controller;

use Drupal\acquia_cms_tour\Form\AcquiaConnectorForm;
use Drupal\acquia_cms_tour\Form\AcquiaGoogleMapsAPIForm;
use Drupal\acquia_cms_tour\Form\AcquiaGoogleMapsApiDashboardForm;
use Drupal\acquia_cms_tour\Form\AcquiaSearchSolrForm;
use Drupal\acquia_cms_tour\Form\AcquiaTelemetryForm;
use Drupal\acquia_cms_tour\Form\GoogleAnalyticsForm;
Expand All @@ -12,6 +12,7 @@
use Drupal\acquia_cms_tour\Form\SiteStudioCoreForm;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -39,6 +40,13 @@ final class DashboardController extends ControllerBase {
*/
protected $state;

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* The sub-controllers to invoke in order to build the tour page.
*
Expand All @@ -49,7 +57,7 @@ final class DashboardController extends ControllerBase {
'acquia_connector_form' => AcquiaConnectorForm::class,
'acquia_solr_search_form' => AcquiaSearchSolrForm::class,
'google_analytics_form' => GoogleAnalyticsForm::class,
'acquia_google_maps_api' => AcquiaGoogleMapsAPIForm::class,
'acquia_google_maps_api' => AcquiaGoogleMapsApiDashboardForm::class,
'recaptcha_form' => RecaptchaForm::class,
'google_tag_manager_form' => GoogleTagManagerForm::class,
'acquia_telemetry' => AcquiaTelemetryForm::class,
Expand All @@ -60,11 +68,14 @@ final class DashboardController extends ControllerBase {
*
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
* The class resolver.
*/
public function __construct(StateInterface $state, ClassResolverInterface $class_resolver) {
public function __construct(StateInterface $state, ModuleHandlerInterface $module_handler, ClassResolverInterface $class_resolver) {
$this->state = $state;
$this->module_handler = $module_handler;
$this->classResolver = $class_resolver;
}

Expand All @@ -91,6 +102,7 @@ private function getSectionOutput(string $key, string $controller_class) {
public static function create(ContainerInterface $container) {
return new static(
$container->get('state'),
$container->get('module_handler'),
$container->get('class_resolver')
);
}
Expand Down Expand Up @@ -124,10 +136,12 @@ public function content() {
$count = 0;
$item_count = 0;
foreach (static::SECTIONS as $key => $controller) {
$count++;
$build[$key] = $this->getSectionOutput($key, $controller);
$state_var = $this->classResolver->getInstanceFromDefinition($controller)->getProgressState();
if ($state_var) {
if (isset($state_var['total'])) {
saurabhtripathics marked this conversation as resolved.
Show resolved Hide resolved
$count++;
}
if (isset($state_var['count']) && $state_var['count']) {
$item_count++;
}
}
Expand Down
8 changes: 6 additions & 2 deletions modules/acquia_cms_tour/src/Form/AcquiaConnectorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,12 @@ public function ignoreConfig(array &$form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function getProgressState() {
$acquia_connector_progress = $this->state->get('acquia_connector_progress');
return $acquia_connector_progress;
if ($this->module_handler->moduleExists('acquia_connector')) {
return [
'total' => 1,
'count' => $this->state->get('acquia_connector_progress'),
];
}
}

}
185 changes: 37 additions & 148 deletions modules/acquia_cms_tour/src/Form/AcquiaGoogleMapsAPIForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

namespace Drupal\acquia_cms_tour\Form;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\InfoParserInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\geocoder\GeocoderProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -18,88 +12,25 @@
*/
final class AcquiaGoogleMapsAPIForm extends ConfigFormBase {

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;


/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;


/**
* The info file parser.
*
* @var \Drupal\Core\Extension\InfoParserInterface
*/
protected $infoParser;

/**
* The link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected $linkGenerator;

/**
* The Geocoder provider entity storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
private $geocoderProviderStorage;

/**
* AcquiaTelemetryForm constructor.
*
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Extension\InfoParserInterface $info_parser
* The info file parser.
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generator.
*/
public function __construct(StateInterface $state, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, InfoParserInterface $info_parser, LinkGeneratorInterface $link_generator) {
$this->state = $state;
$this->entityTypeManager = $entity_type_manager;
$this->module_handler = $module_handler;
$this->infoParser = $info_parser;
$this->linkGenerator = $link_generator;
if ($entity_type_manager->hasDefinition('geocoder_provider')) {
$this->geocoderProviderStorage = $entity_type_manager->getStorage('geocoder_provider');
}

}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('state'),
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('info_parser'),
$container->get('link_generator')

);
/** @var static $instance */
$instance = parent::create($container);

$entity_type_manager = $container->get('entity_type.manager');
if ($entity_type_manager->hasDefinition('geocoder_provider')) {
$instance->geocoderProviderStorage = $entity_type_manager->getStorage('geocoder_provider');
}
return $instance;
}

/**
Expand All @@ -123,62 +54,34 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// Text input for Google Maps. ACMS can use the Gmaps API in two totally
// different features (Site Studio and Place nodes). Site Studio is always
// enabled in ACMS, but Place may not.
$module = 'geocoder';
if ($this->getProgressState()) {
$form['acquia_telemetry']['check_icon'] = [
'#prefix' => '<span class= "dashboard-check-icon">',
'#suffix' => "</span>",
];
}
if ($this->module_handler->moduleExists($module)) {
$module_path = $this->module_handler->getModule($module)->getPathname();
$module_info = $this->infoParser->parse($module_path);
$maps_api_key = $this->config('cohesion.settings')
->get('google_map_api_key');
$provider = $this->loadProvider();
if ($provider) {
$configuration = $provider->get('configuration');
$maps_api_key = $configuration['apiKey'];
}
$form['acquia_google_maps_api_wrapper'] = [
'#type' => 'details',
'#title' => $module_info['name'],
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['acquia_google_maps_api_wrapper']['acquia_google_maps_api'] = [
'maps_api_key' => [
'#type' => 'textfield',
'#title' => $this->t('Maps API key'),
'#placeholder' => '1234abcd',
'#description' => $this->t('Enter your Google Maps API Key to automatically generate maps for Place content in Acquia CMS.'),
'#default_value' => $maps_api_key,
'#required' => TRUE,
'#prefix' => '<div class= "dashboard-fields-wrapper">' . $module_info['description'],
'#suffix' => "</div>",
],
];
$form['acquia_google_maps_api_wrapper']['actions']['submit'] = [
'#type' => 'submit',
'#value' => 'Save',
'#submit' => ['::saveConfig'],
'#prefix' => '<div class= "dashboard-buttons-wrapper">',
];
$form['acquia_google_maps_api_wrapper']['actions']['ignore'] = [
'#type' => 'submit',
'#value' => 'Ignore',
'#submit' => ['::ignoreConfig'],
];
$form['acquia_google_maps_api_wrapper']['actions']['advanced'] = [
'#markup' => $this->linkGenerator->generate(
'Advanced',
Url::fromRoute('entity.geocoder_provider.collection')
),
'#suffix' => "</div>",
];

return $form;
$maps_api_key = $this->config('cohesion.settings')
->get('google_map_api_key');

$provider = $this->loadProvider();
if ($provider) {
$configuration = $provider->get('configuration');
$maps_api_key = $configuration['apiKey'];
}

$form['acquia_google_maps_api'] = [
'maps_api_key' => [
'#type' => 'textfield',
'#title' => $this->t('Maps API key'),
'#description' => $this->t('Enter your Google Maps API Key to automatically generate maps for Place content in Acquia CMS.'),
'#default_value' => $maps_api_key,
'#required' => TRUE,
],
'#type' => 'fieldset',
'#open' => TRUE,
'#title' => $this->t('Google Maps'),
];

$form['acquia_google_maps_api']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#button_type' => 'primary',
];
return $form;
}

/**
Expand All @@ -197,15 +100,15 @@ private function loadProvider() : ?GeocoderProviderInterface {
/**
* {@inheritdoc}
*/
public function saveConfig(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$maps_api_key = $form_state->getValue('maps_api_key');

// Configure Google Maps API Key for both Site Studio and
// Geocoder module.
$this->config('cohesion.settings')
->set('google_map_api_key', $maps_api_key)
->save(TRUE);
$this->state->set('acquia_google_maps_progress', TRUE);

$provider = $this->loadProvider();
if ($provider) {
$configuration = $provider->get('configuration');
Expand All @@ -217,18 +120,4 @@ public function saveConfig(array &$form, FormStateInterface $form_state) {
$this->messenger()->addStatus('The Google Maps API key has been set.');
}

/**
* {@inheritdoc}
*/
public function ignoreConfig(array &$form, FormStateInterface $form_state) {
$this->state->set('acquia_google_maps_progress', TRUE);
}

/**
* {@inheritdoc}
*/
public function getProgressState() {
return($this->state->get('acquia_google_maps_progress'));
}

}
Loading