View source
<?php
namespace Drupal\yamlform;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class YamlFormThirdPartySettingsManager implements YamlFormThirdPartySettingsManagerInterface {
use StringTranslationTrait;
protected $configFactory;
protected $moduleHandler;
protected $pathValidator;
protected $addonsManager;
protected $config;
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, PathValidatorInterface $path_validator, YamlFormAddonsManagerInterface $addons_manager) {
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->pathValidator = $path_validator;
$this->addonsManager = $addons_manager;
$this->config = $this->configFactory
->getEditable('yamlform.settings');
$this
->loadIncludes();
}
protected function loadIncludes() {
$modules = array_keys($this->moduleHandler
->getModuleList());
foreach ($modules as $module) {
$this->moduleHandler
->loadInclude($module, 'yamlform.inc');
$this->moduleHandler
->loadInclude($module, 'yamlform.inc', "yamlform/{$module}");
$this->moduleHandler
->loadInclude('yamlform', "inc", "third_party_settings/yamlform.{$module}");
}
}
public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL) {
$this->moduleHandler
->alter($type, $data, $context1, $context2);
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['third_party_settings'] = [
'#tree' => TRUE,
];
$form['#after_build'] = [
[
$this,
'afterBuild',
],
];
return $form;
}
public function afterBuild(array $form, FormStateInterface $form_state) {
if (!isset($form['third_party_settings']) || !Element::children($form['third_party_settings'])) {
$form['actions']['#access'] = FALSE;
drupal_set_message($this
->t('There are no third party settings available. Please install a contributed module that integrates with the YAML Form module.'), 'warning');
$form['supported'] = [
'title' => [
'#markup' => $this
->t('Supported modules.'),
'#prefix' => '<h3>',
'#suffix' => '</h3>',
],
'modules' => [
'#theme' => 'admin_block_content',
'#content' => $this->addonsManager
->getThirdPartySettings(),
],
];
}
else {
ksort($form['third_party_settings']);
}
return $form;
}
public function setThirdPartySetting($module, $key, $value) {
$this->config
->set("third_party_settings.{$module}.{$key}", $value);
return $this;
}
public function getThirdPartySetting($module, $key, $default = NULL) {
$value = $this->config
->get("third_party_settings.{$module}.{$key}");
return isset($value) ? $value : $default;
}
public function getThirdPartySettings($module) {
$this->config
->get("third_party_settings.{$module}") ?: [];
}
public function unsetThirdPartySetting($module, $key) {
$this->config
->clear("third_party_settings.{$module}.{$key}");
if (!$this->config
->get("third_party_settings.{$module}")) {
$this->config
->clear("third_party_settings.{$module}");
}
return $this;
}
public function getThirdPartyProviders() {
$third_party_settings = $this->config
->get('third_party_settings') ?: [];
return array_keys($third_party_settings);
}
}