public function ComponentGenerateForm::form in Module Builder 8.3
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ ComponentGenerateForm.php, line 21
Class
- ComponentGenerateForm
- Form showing generated component code.
Namespace
Drupal\module_builder\FormCode
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$component_data = $this
->getComponentDataObject();
// Get the generation config.
$config = \Drupal::config('module_builder.settings');
$module_config = $config
->get('generator_settings.module');
$config_data = \Drupal::service('module_builder.drupal_code_builder')
->getTask('Configuration')
->getConfigurationData('module');
$config_data
->import($module_config ?? []);
// Get the files.
try {
$files = $this->codeBuilderTaskHandlerGenerate
->generateComponent($component_data, [], $config_data);
} catch (InvalidInputException $e) {
$this
->messenger()
->addError(t("Invalid input for code generator: @message", [
'@message' => $e
->getMessage(),
]));
return $form;
}
// Get the path to the module if it's previously been written.
$existing_module_path = $this
->getExistingModule();
if ($existing_module_path) {
$this
->messenger()
->addWarning(t("This module already exists at @path. It is recommended you use version control to prevent losing any existing code.", [
'@path' => $existing_module_path,
]));
}
$module_name = $this->entity
->id();
if (\Drupal::moduleHandler()
->moduleExists($module_name)) {
$this
->messenger()
->addWarning(t("This module is currently ENABLED on this site. Writing files MAY CAUSE YOUR SITE TO CRASH."));
}
ksort($files);
$statuses = [];
foreach ($files as $filename => $code) {
// We don't actually use the value from the form, as the POST process
// seems to be turning unix line endings into Windows line endings! Store
// it in the form state instead.
$form_state
->set([
'files',
$filename,
], $code);
// Warn the user if the file already exists, and is not committed to git.
$filepath = $existing_module_path . '/' . $filename;
if (file_exists($filepath)) {
// TODO: if the file is clean in git, we can maybe skip this, or say
// it's ok to overwrite?
// Perform the 'git status' command in the module folder, to allow for
// the case where the module has its own git repository.
$git_status = shell_exec("cd {$existing_module_path} && git status {$filename} --porcelain");
if (!empty($git_status)) {
$git_status_code = substr($git_status, 0, 2);
// TODO: These don't take into account that changes might be staged.
switch ($git_status_code) {
case '??':
$statuses[$filename] = t("WARNING: file already exists and is not under git version control! Writing this will lose the existing version.");
break;
case ' M':
$statuses[$filename] = t("WARNING: file already exists and has uncommitted changes! Writing this will lose these.");
break;
}
}
else {
$statuses[$filename] = t("(File already exists)");
}
}
else {
$statuses[$filename] = '';
}
}
$form['files'] = [
'#type' => 'module_builder_generated_files',
'#files' => $files,
'#statuses' => $statuses,
];
return $form;
}