Repository URL to install this package:
|
Version:
2.0.2 ▾
|
<?php
namespace Drupal\custom_forms\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\custom_forms\CustomFormsSubmissionHandlerManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for the custom form handler edit forms.
*
* This is the backend add/edit form.
*/
class CustomFormHandlerForm extends ContentEntityForm {
/**
* The entity being used by this form.
*
* @var \Drupal\custom_forms\Entity\CustomFormSubmissionHandlerInterface
*/
protected $entity;
/**
* @var \Drupal\custom_forms\CustomFormsSubmissionHandlerManager
* The plugin manager for submission handler plugins.
*/
protected $pluginManager;
/**
* @inheritDoc
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('plugin.manager.custom_forms_submission_handlers'),
$container->get('entity_type.manager'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time')
);
}
/**
* {@inheritdoc}
*/
public function __construct(
EntityRepositoryInterface $entity_repository,
CustomFormsSubmissionHandlerManager $pluginManager,
EntityTypeManagerInterface $entityTypeManager,
EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL,
TimeInterface $time = NULL
) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->pluginManager = $pluginManager;
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$request = \Drupal::request();
$attributes = $request->attributes;
/** @var \Drupal\custom_forms\CustomFormInterface $customForm */
$customForm = $attributes->get('custom_form');
$form = parent::buildForm($form, $form_state);
$form['#theme'] = 'system_config_form';
$plugins = [];
foreach ($this->pluginManager->getDefinitions() as $plugin_id => $plugin_definition) {
$plugins[$plugin_id] = $plugin_definition['label'];
}
$form['form'] = [
'#type' => 'value',
'#value' => $customForm
];
$form['plugin'] = [
'#type' => 'select',
'#title' => $this->t('Type'),
'#description' => $this->t('Choose what type of handler it should be.'),
'#required' => TRUE,
'#options' => $plugins,
'#empty_option' => '- ' . $this->t('Select type') . ' -',
];
// If we only have one plugin available, disable the field and add select
// the only option.
if (count($plugins) === 1) {
$form['plugin']['#disabled'] = TRUE;
$form['plugin']['#value'] = key($plugins);
$form['plugin']['#default_value'] = key($plugins);
}
// If the entity isn't new (not on add-form) and the plugin exists, show
// the settings.
if (!$this->entity->isNew() && !$this->entity->get('plugin')->isEmpty()) {
$plugin_id = $this->entity->get('plugin')->value;
$definition = $this->pluginManager->getDefinition($plugin_id);
$settings = $this->entity->getPluginSettings();
/** @var \Drupal\custom_forms\Plugin\CustomForms\SubmissionHandler\CustomFormsSubmissionHandlerInterface $plugin */
$plugin = $this->entity->getPlugin();
$form['plugin_entity'] = [
'#type' => 'value',
'#value' => $plugin,
];
if ($definition['has_settings']) {
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this->t('Settings'),
'#tree' => TRUE,
];
$form['settings'] += $plugin->buildSettingsForm($customForm, $form, $form_state);
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->cleanValues()->getValues();
/** @var \Drupal\custom_forms\Plugin\CustomForms\SubmissionHandler\CustomFormsSubmissionHandlerInterface $plugin */
$plugin = $form_state->getValue('plugin_entity');
unset($values['plugin_entity']);
$form_state->setValues($values);
if ($plugin !== NULL) {
$plugin->submitSettingsForm($this->entity->getCustomForm(), $form, $form_state);
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\custom_forms\Entity\CustomFormSubmissionHandler $entity */
$entity = $this->getEntity();
$entity->setOwnerId($this->currentUser()->id());
$result = $entity->save();
$arguments = ['%label' => $this->entity->label(), '%form' => $entity->getCustomForm()->label()];
if ($result == SAVED_NEW) {
$this->messenger()->addMessage($this->t('New handler %label for %form has been created.', $arguments));
$form_state->setRedirect('entity.custom_form_submission_handler.edit_form', ['custom_form' => $entity->getCustomForm()->id(), 'custom_form_submission_handler' => $entity->id()]);
}
else {
$this->messenger()->addMessage($this->t('The handler %label for %form has been updated.', $arguments));
$form_state->setRedirect('entity.custom_form_submission_handler.collection', ['custom_form' => $entity->getCustomForm()->id()]);
}
}
/**
* @inheritDoc
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#submit' => ['::submitForm', '::save'],
];
if (!$this->entity->isNew() && $this->entity->hasLinkTemplate('delete-form')) {
$route_info = $this->entity->toUrl('delete-form');
$route_info->setRouteParameter('custom_form', $this->entity->getCustomForm()->id());
if ($this->getRequest()->query->has('destination')) {
$query = $route_info->getOption('query');
$query['destination'] = $this->getRequest()->query->get('destination');
$route_info->setOption('query', $query);
}
$actions['delete'] = [
'#type' => 'link',
'#title' => $this->t('Delete'),
'#access' => $this->entity->access('delete'),
'#attributes' => [
'class' => ['button', 'button--danger'],
],
];
$actions['delete']['#url'] = $route_info;
}
return $actions;
}
}