ConfigInspectorItemForm.php in Configuration Inspector 8
File
src/Form/ConfigInspectorItemForm.php
View source
<?php
namespace Drupal\config_inspector\Form;
use Drupal\Core\Config\Schema\ArrayElement;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class ConfigInspectorItemForm extends FormBase {
public function getFormId() {
return 'config_inspector_item_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $schema = NULL) {
$form['structure'] = $this
->buildFormConfigElement($schema);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
protected function buildFormConfigElement($schema, $collapsed = FALSE) {
$build = [];
foreach ($schema as $key => $element) {
$definition = $element
->getDataDefinition();
$label = $definition['label'] ?: $this
->t('N/A');
if ($element instanceof ArrayElement) {
$build[$key] = [
'#type' => 'details',
'#title' => $label,
'#open' => !$collapsed,
] + $this
->buildFormConfigElement($element, TRUE);
}
else {
$type = $definition['type'];
switch ($type) {
case 'boolean':
$type = 'checkbox';
break;
case 'string':
case 'color_hex':
case 'path':
case 'label':
$type = 'textfield';
break;
case 'text':
$type = 'textarea';
break;
case 'integer':
$type = 'number';
break;
}
$value = $element
->getString();
$build[$key] = [
'#type' => $type,
'#title' => $label,
'#default_value' => $value,
];
}
}
return $build;
}
}