View source
<?php
namespace Drupal\cloudflare\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\cloudflare\CloudFlareZoneInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use CloudFlarePhpSdk\Exceptions\CloudFlareTimeoutException;
use Drupal\Core\Link;
use Drupal\Core\Url;
class ZoneSelectionForm extends FormBase implements ContainerInjectionInterface {
protected $configFactory;
protected $zoneApi;
protected $logger;
protected $zones;
protected $cloudFlareComposerDependenciesMet;
protected $hasMultipleZones;
public static function create(ContainerInterface $container) {
$has_zone_mock = $container
->has('cloudflare.zonemock');
return new static($container
->get('config.factory'), $has_zone_mock ? $container
->get('cloudflare.zonemock') : $container
->get('cloudflare.zone'), $container
->get('logger.factory')
->get('cloudflare'), $container
->get('cloudflare.composer_dependency_check')
->check());
}
public function __construct(ConfigFactoryInterface $config_factory, CloudFlareZoneInterface $zone_api, LoggerInterface $logger, $composer_dependencies_met) {
$this->configFactory = $config_factory;
$this->config = $config_factory
->getEditable('cloudflare.settings');
$this->zoneApi = $zone_api;
$this->logger = $logger;
$this->cloudFlareComposerDependenciesMet = $composer_dependencies_met;
$this->hasZoneId = !empty($this->config
->get('zone_id'));
$this->hasValidCredentials = $this->config
->get('valid_credentials') === TRUE;
if ($this->hasValidCredentials && $this->cloudFlareComposerDependenciesMet) {
try {
$this->zones = $this->zoneApi
->listZones();
$this->hasMultipleZones = count($this->zones) > 1;
} catch (CloudFlareTimeoutException $e) {
$this
->messenger()
->addError($this
->t('Unable to connect to CloudFlare. You will not be able to change the selected Zone.'));
}
}
}
protected function getEditableConfigNames() {
return [
'cloudflare.zoneselection',
];
}
public function getFormId() {
return 'cloudflare_zone_selection';
}
public function buildForm(array $form, FormStateInterface $form_state) {
return $this
->buildZoneSelectSection();
}
protected function buildZoneSelectSection() {
$section = [];
$section['zone_selection_fieldset'] = [
'#type' => 'fieldset',
'#weight' => 0,
];
if (!$this->hasMultipleZones && $this->hasValidCredentials) {
if (empty($this->zones)) {
$add_site_link = Link::fromTextAndUrl($this
->t('add a site'), Url::fromUri('https://www.cloudflare.com/a/setup'));
$section['zone_selection_fieldset']['zone_selection'] = [
'#markup' => $this
->t('<p>Your CloudFlare account does not have any zones configured. Verify your API details or !add_site_link via the console.</p>', [
'!add_site_link' => $add_site_link
->toString(),
]),
];
return $section;
}
$zone_id = $this->zones[0]
->getZoneId();
$this->config
->set('zone_id', $zone_id)
->save();
$section['zone_selection_fieldset']['zone_selection'] = [
'#markup' => $this
->t('<p>Your CloudFlare account has a single zone which has been automatically selected for you. Simply click "Finish" to save your settings.</p>'),
];
return $section;
}
$listing = $this
->buildZoneListing();
$section['zone_selection_fieldset']['zone_selection'] = $listing;
return $section;
}
public function buildZoneListing() {
$form_select_field = [];
$zone_select = [];
foreach ($this->zones as $zone) {
$zone_select[$zone
->getZoneId()] = $zone
->getName();
}
$form_select_field = [
'#type' => 'textfield',
'#title' => $this
->t('Zone'),
'#disabled' => FALSE,
'#options' => $zone_select,
'#description' => $this
->t('Use the autocomplete to select your zone (top level domain for the site). The zone ID corresponding to the domain will then be saved in the field.'),
'#default_value' => $this->config
->get('zone_id'),
'#empty_option' => '- None -',
'#empty_value' => '0',
'#autocomplete_route_name' => 'cloudflare.zone_autocomplete',
];
return $form_select_field;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->hasMultipleZones) {
$zone_id = $form_state
->getValue('zone_selection');
$this->config
->set('zone_id', $zone_id)
->save();
}
$form_state
->setRedirect('cloudflare.admin_settings_form');
}
public function autocompleteZone(Request $request) {
$zone_autocomplete_text = $request->query
->get('q');
$matches = [];
foreach ($this->zoneApi
->listZones() as $zone) {
if (stripos($zone
->getName(), $zone_autocomplete_text) === 0) {
$matches[] = [
'value' => $zone
->getZoneId(),
'label' => $zone
->getName(),
];
}
}
return new JsonResponse($matches);
}
}