DevelGenerateForm.php in Devel 8.2
File
devel_generate/src/Form/DevelGenerateForm.php
View source
<?php
namespace Drupal\devel_generate\Form;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\devel_generate\DevelGenerateException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DevelGenerateForm extends FormBase {
protected $develGenerateManager;
public function __construct(PluginManagerInterface $devel_generate_manager) {
$this->develGenerateManager = $devel_generate_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.develgenerate'));
}
public function getFormId() {
return 'devel_generate_form_' . $this
->getPluginIdFromRequest();
}
protected function getPluginIdFromRequest() {
$request = $this
->getRequest();
return $request
->get('_plugin_id');
}
public function getPluginInstance($plugin_id) {
$instance = $this->develGenerateManager
->createInstance($plugin_id, []);
return $instance;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$plugin_id = $this
->getPluginIdFromRequest();
$instance = $this
->getPluginInstance($plugin_id);
$form = $instance
->settingsForm($form, $form_state);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Generate'),
'#button_type' => 'primary',
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$plugin_id = $this
->getPluginIdFromRequest();
$instance = $this
->getPluginInstance($plugin_id);
$instance
->settingsFormValidate($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
try {
$plugin_id = $this
->getPluginIdFromRequest();
$instance = $this
->getPluginInstance($plugin_id);
$instance
->generate($form_state
->getValues());
} catch (\Exception $e) {
$this
->logger('DevelGenerate', $this
->t('Failed to generate elements due to "%error".', [
'%error' => $e
->getMessage(),
]));
$this
->messenger()
->addMessage($this
->t('Failed to generate elements due to "%error".', [
'%error' => $e
->getMessage(),
]));
}
}
}