View source
<?php
namespace Drupal\module_builder\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
class GeneratedFiles extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#process' => [
[
$class,
'processFiles',
],
],
'#theme' => 'generated_files',
'#theme_wrappers' => [
'form_element',
],
];
}
public static function processFiles(&$element, FormStateInterface $form_state, &$complete_form) {
$element['filename_list'] = [
'#tree' => TRUE,
];
foreach ($element['#files'] as $filename => $code) {
$element['filename_list'][$filename] = [
'#type' => 'checkbox',
'#title' => $filename . ' ' . ($element['#statuses'][$filename] ?? ''),
'#attributes' => [
'data-generated-file' => $filename,
],
];
$element['code'][$filename] = [
'#type' => 'textarea',
'#title' => t("@filename code", [
'@filename' => $filename,
]) . ' ' . ($element['#statuses'][$filename] ?? ''),
'#rows' => count(explode("\n", $code)),
'#default_value' => $code,
'#parents' => [
'file_code',
$filename,
],
'#attributes' => [
'data-generated-file' => $filename,
],
];
}
$element['#attached'] = [
'library' => [
'module_builder/generated_files',
],
];
return $element;
}
}