View source
<?php
namespace BackupMigrate\Drupal\Config;
use BackupMigrate\Core\Config\ConfigInterface;
use BackupMigrate\Core\Main\BackupMigrateInterface;
use BackupMigrate\Core\Plugin\PluginManagerInterface;
use Drupal\backup_migrate\Entity\SettingsProfile;
use Drupal\Core\Form\FormStateInterface;
class DrupalConfigHelper {
public static function buildAllPluginsForm(PluginManagerInterface $plugins, $operation, $parents = []) {
$form = [];
foreach ($plugins
->getAll() as $plugin_key => $plugin) {
$schema = $plugin
->configSchema([
'operation' => $operation,
]);
$config = $plugin
->config();
DrupalConfigHelper::addFieldsFromSchema($form, $schema, $config, array_merge($parents, [
$plugin_key,
]));
}
return $form;
}
public static function buildPluginForm($plugin, $operation, $parents = [
'config',
]) {
$schema = $plugin
->configSchema([
'operation' => $operation,
]);
$config = $plugin
->config();
return DrupalConfigHelper::buildFormFromSchema($schema, $config, $parents);
}
public static function buildFormFromSchema($schema, ConfigInterface $config, $parents = [], $form = []) {
$form = [];
DrupalConfigHelper::addFieldsFromSchema($form, $schema, $config, $parents);
return $form;
}
public static function addFieldsFromSchema(&$form, $schema, ConfigInterface $config, $parents = []) {
if (isset($schema['groups'])) {
foreach ($schema['groups'] as $group_key => $item) {
if ($group_key == 'default' && $parents) {
$group_key = end($parents);
}
if (!isset($form[$group_key])) {
$form[$group_key] = [
'#type' => 'fieldset',
'#title' => $item['title'],
'#tree' => FALSE,
];
}
}
}
if (isset($schema['fields'])) {
foreach ($schema['fields'] as $field_key => $item) {
$form_item = [];
$value = $config
->get($field_key);
switch ($item['type']) {
case 'text':
$form_item['#type'] = 'textfield';
if (!empty($item['multiple'])) {
$form_item['#type'] = 'textarea';
if (!isset($form_item['#description'])) {
$form_item['#description'] = '';
}
$form_item['#description'] .= ' ' . t('Add one item per line.');
$form_item['#element_validate'] = [
'BackupMigrate\\Drupal\\Config\\DrupalConfigHelper::validateMultiText',
];
$value = implode("\n", $value);
}
if (!empty($item['multiline'])) {
$form_item['#type'] = 'textarea';
}
break;
case 'password':
$form_item['#type'] = 'password';
$form_item['#value_callback'] = 'BackupMigrate\\Drupal\\Config\\DrupalConfigHelper::valueCallbackSecret';
break;
case 'number':
$form_item['#type'] = 'textfield';
$form_item['#size'] = 5;
if (!empty($item['max'])) {
$form_item['#size'] = strlen((string) $item['max']) + 3;
}
break;
case 'boolean':
$form_item['#type'] = 'checkbox';
break;
case 'enum':
$form_item['#type'] = 'select';
$form_item['#multiple'] = !empty($item['multiple']);
if (empty($item['#required']) && empty($item['multiple'])) {
$item['options'] = [
'' => '--' . t('None') . '--',
] + $item['options'];
}
$form_item['#options'] = $item['options'];
break;
}
if ($form_item) {
$form_item['#title'] = $item['title'];
$form_item['#parents'] = array_merge($parents, [
$field_key,
]);
$form_item['#required'] = !empty($item['required']);
$form_item['#default_value'] = $value;
if (!empty($item['description'])) {
$form_item['#description'] = $item['description'];
}
if (!empty($item['group'])) {
$group_key = $item['group'];
if ($group_key == 'default' && $parents) {
$group_key = end($parents);
}
$form[$group_key][$field_key] = $form_item;
}
else {
$form[$field_key] = $form_item;
}
}
}
}
}
public static function validateMultiText(&$element, FormStateInterface &$form_state) {
$form_state
->setValueForElement($element, array_map('trim', explode("\n", $element['#value'])));
}
public static function valueCallbackSecret(&$element, $input, FormStateInterface $form_state) {
if (empty($input)) {
return $element['#default_value'];
}
return $input;
}
public static function getPluginSelector(PluginManagerInterface $plugins, $title, $default_value = NULL) {
$options = [];
foreach ($plugins
->getAll() as $key => $plugin) {
$options[$key] = $plugin
->confGet('name', $key);
}
return [
'#type' => 'select',
'#title' => $title,
'#options' => $options,
'#default_value' => $default_value,
];
}
public static function getSourceSelector(BackupMigrateInterface $bam, $title, $default_value = NULL) {
return DrupalConfigHelper::getPluginSelector($bam
->sources(), $title, $default_value);
}
public static function getDestinationSelector(BackupMigrateInterface $bam, $title, $default_value = NULL) {
return DrupalConfigHelper::getPluginSelector($bam
->destinations(), $title, $default_value);
}
public static function getSettingsProfileSelector($title, $default_value = NULL) {
$options = [];
foreach (SettingsProfile::loadMultiple() as $key => $profile) {
$options[$key] = $profile
->get('label');
}
if ($options) {
return [
'#type' => 'select',
'#title' => $title,
'#options' => $options,
'#default_value' => $default_value,
];
}
}
}