Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORSLAG-101: Generalized handling of form surveys. Added survey to support form. #359

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

* [PR-359](https://github.com/itk-dev/hoeringsportal/pull/359)
Generalized handling of form surveys. Added survey to support form

## [3.3.0] - 2023-08-25

* [PR-355](https://github.com/itk-dev/hoeringsportal/pull/355)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => $adminFormStateValues['characters_remarks'] ?? '',
];

$this->buildSurveyForm($form['add_form'], ProposalFormAdd::SURVEY_KEY, $adminFormStateValues ?? []);

$form['approve_form'] = [
'#type' => 'details',
'#open' => TRUE,
Expand Down Expand Up @@ -250,6 +252,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => $adminFormStateValues['support_submission_text'] ?? '',
];

$this->buildSurveyForm($form['support_form'], ProposalFormSupport::SURVEY_KEY, $adminFormStateValues ?? []);

$form['sidebar'] = [
'#type' => 'details',
'#open' => TRUE,
Expand All @@ -264,7 +268,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => $adminFormStateValues['sidebar_text']['value'] ?? '',
];

$this->buildSurveyForm($form, $adminFormStateValues ?? []);
$this->buildEmailsForm($form, $adminFormStateValues ?? []);

$form['actions']['#type'] = 'actions';
Expand All @@ -282,44 +285,44 @@ public function buildForm(array $form, FormStateInterface $form_state) {
*
* @param array $form
* The form.
* @param string $surveyKey
* The survey key.
* @param array $adminFormStateValues
* The admin form state values.
*
* @return array
* The form.
*/
private function buildSurveyForm(array &$form, array $adminFormStateValues): array {
$form['survey'] = [
'#type' => 'details',
private function buildSurveyForm(array &$form, string $surveyKey, array $adminFormStateValues): array {
$form[$surveyKey] = [
'#type' => 'container',
'#tree' => TRUE,
'#open' => TRUE,
'#title' => $this
->t('Survey'),
];

$form['survey']['webform'] = [
$form[$surveyKey]['webform'] = [
'#type' => 'select',
'#title' => $this->t('Survey webform'),
'#options' => array_map(
static fn (WebformInterface $webform) => $webform->label(),
$this->webformHelper->loadSurveyWebforms()
),
'#empty_option' => $this->t('Select survey webform'),
'#default_value' => $adminFormStateValues['survey']['webform'] ?? '',
'#description' => $this->t('Select a survey to show as part of the citizen proposal creation form. <a href=":url">Manage surveys</a>.', [
'#default_value' => $adminFormStateValues[$surveyKey]['webform'] ?? '',
'#description' => $this->t('Select a survey to show as part of the form. <a href=":url">Manage surveys</a>.', [
':url' => Url::fromRoute('entity.webform.collection')->toString(TRUE)->getGeneratedUrl(),
]),
];

$form['survey']['description'] = [
$form[$surveyKey]['description'] = [
'#type' => 'text_format',
'#title' => $this->t('Survey description'),
'#format' => $adminFormStateValues['survey']['description']['format'] ?? 'filtered_html',
'#default_value' => $adminFormStateValues['survey']['description']['value'] ?? '',
'#format' => $adminFormStateValues[$surveyKey]['description']['format'] ?? 'filtered_html',
'#default_value' => $adminFormStateValues[$surveyKey]['description']['value'] ?? '',
'#description' => $this->t('Tell a little about why the survey is shown.'),
'#states' => [
'visible' => [
':input[name="survey[webform]"]' => ['filled' => TRUE],
':input[name="' . $surveyKey . '[webform]"]' => ['filled' => TRUE],
],
// Setting "required" with states does not work with text_format fields
// the first time around. Actual validation is performed in
Expand Down Expand Up @@ -415,10 +418,13 @@ public function validateForm(array &$form, FormStateInterface $formState) {
);
}

if (!empty($formState->getValue(['survey', 'webform']))
&& empty($formState->getValue(['survey', 'description', 'value']))) {
$formState->setError($form['survey']['description']['value'],
$this->t('Please enter a survey description.'));
foreach ([ProposalFormAdd::SURVEY_KEY, ProposalFormSupport::SURVEY_KEY] as $key) {
if (!empty($formState->getValue([$key, 'webform']))
&& empty($formState->getValue([$key, 'description', 'value']))) {
$formKey = ProposalFormSupport::SURVEY_KEY === $key ? 'support_form' : 'add_form';
$formState->setError($form[$formKey][$key]['description']['value'],
$this->t('Please enter a survey description.'));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Form for adding proposal.
*/
final class ProposalFormAdd extends ProposalFormBase {
public const SURVEY_KEY = 'create_proposal_survey';

public const ADD_FORM_TITLE_MAXLENGTH = 80;
public const ADD_FORM_PROPOSAL_MAXLENGTH = 2000;
public const ADD_FORM_REMARKS_MAXLENGTH = 10000;
Expand Down Expand Up @@ -214,45 +216,6 @@ private function afterBuildForm(array $form, FormStateInterface $formState) {
return $form;
}

/**
* Build survey form.
*
* @param array $form
* The form.
*
* @return array
* The form.
*/
private function buildSurveyForm(array &$form): array {
$form['survey'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['survey', 'citizen-proposal-survey'],
],
'#tree' => TRUE,
];

try {
$description = $this->getAdminFormStateValue(['survey', 'description']);
if (($webform = $this->loadSurvey()) && isset($description['value'])) {
// We use a numeric index (implicit 0) here to prevent webform fields
// accidentally overwriting the description element.
$form['survey'][] = [
'#type' => 'processed_text',
'#text' => $description['value'],
'#format' => $description['format'] ?? 'filtered_html',
];

$this->webformHelper->renderWebformElements($webform, $form['survey']);
}
}
catch (\Exception $exception) {
throw $exception;
}

return $form;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -299,15 +262,7 @@ public function submitForm(array &$form, FormStateInterface $formState): void {
$formState
->setRedirect('hoeringsportal_citizen_proposal.citizen_proposal.proposal_approve');

// Handle survey.
try {
if ($webform = $this->loadSurvey()) {
$surveyData = (array) $formState->getValue('survey');
$this->webformHelper->setSurveyResponse($webform, $surveyData);
}
}
catch (\Exception) {
}
$this->setSurveyResponse($formState);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Form for approving proposal.
*/
final class ProposalFormApprove extends ProposalFormBase {
public const SURVEY_KEY = ProposalFormAdd::SURVEY_KEY;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -171,14 +172,7 @@ public function submitForm(array &$form, FormStateInterface $formState) {

$this->helper->deleteDraftProposal();

// Handle survey.
try {
if ($webform = $this->loadSurvey()) {
$this->webformHelper->saveSurveyResponse($webform, $entity);
}
}
catch (\Exception) {
}
$this->saveSurveyResponse($entity);

$formState->setRedirectUrl(
$this->deAuthenticateUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Drupal\hoeringsportal_citizen_proposal\Helper\WebformHelper;
use Drupal\hoeringsportal_openid_connect\Controller\OpenIDConnectController;
use Drupal\hoeringsportal_openid_connect\Helper as AuthenticationHelper;
use Drupal\node\NodeInterface;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -21,6 +22,8 @@
* Base form for adding proposal.
*/
abstract class ProposalFormBase extends FormBase {
public const SURVEY_KEY = NULL;

public const CONTENT_TEXT_FORMAT = 'citizen_proposal_content';

/**
Expand Down Expand Up @@ -244,17 +247,91 @@ protected function getUserUuid($allowEditor = TRUE): string {
return sha1($userId);
}

/**
* Build survey form.
*
* @param array $form
* The form.
*/
protected function buildSurveyForm(array &$form) {
$webform = $this->loadSurvey();
if (NULL === $webform) {
return;
}

$form['survey'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['survey', 'citizen-proposal-survey'],
],
'#tree' => TRUE,
];

try {
$description = $this->getAdminFormStateValue([
static::SURVEY_KEY,
'description',
]);
if (isset($description['value'])) {
// We use a numeric index (implicit 0) here to prevent webform fields
// accidentally overwriting the description element.
$form['survey'][] = [
'#type' => 'processed_text',
'#text' => $description['value'],
'#format' => $description['format'] ?? 'filtered_html',
];

$this->webformHelper->renderWebformElements($webform, $form['survey']);
}
}
catch (\Exception $exception) {
throw $exception;
}
}

/**
* Load survey webform.
*
* @return \Drupal\webform\WebformInterface|null
* The webform if any.
*/
protected function loadSurvey(): ?WebformInterface {
if (empty(static::SURVEY_KEY)) {
return NULL;
}

return $this->webformHelper->loadWebform((string) $this->getAdminFormStateValue([
'survey',
static::SURVEY_KEY,
'webform',
]));
}

/**
* Set survey response.
*/
protected function setSurveyResponse(FormStateInterface $formState) {
try {
if ($webform = $this->loadSurvey()) {
$surveyData = (array) $formState->getValue('survey');
$this->webformHelper->setSurveyResponse($webform, $surveyData);
}
}
catch (\Exception) {
}
}

/**
* Save survey response previously set.
*/
protected function saveSurveyResponse(NodeInterface $node) {
try {
if ($webform = $this->loadSurvey()) {
$this->webformHelper->saveSurveyResponse($webform, $node);
}
}
catch (\Exception) {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Form for supporting proposal.
*/
final class ProposalFormSupport extends ProposalFormBase {
public const SURVEY_KEY = 'support_proposal_survey';

/**
* {@inheritdoc}
Expand Down Expand Up @@ -115,6 +116,8 @@ protected function buildProposalForm(array $form, FormStateInterface $formState)
],
];

$this->buildSurveyForm($form);

$form['consent'] = [
'#type' => 'checkbox',
'#title' => $this
Expand Down Expand Up @@ -170,6 +173,9 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
],
);
$this->messenger()->addStatus($this->getAdminFormStateValue('support_submission_text', $this->t('Thank you for your support.')));

$this->setSurveyResponse($form_state);
$this->saveSurveyResponse($node);
}
catch (\Exception $e) {
$this->messenger()->addError($this->t('Something went wrong. Your support was not registered.'));
Expand Down
Loading