Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions css/localgov_forms_header_block.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.header__form_title h1 {
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 0.5rem;
}

.header__user_description {
font-size: 1.375rem !important;
font-weight: 400 !important;
line-height: 1.5rem;
margin-bottom: 1em;
}

.header__wizard_page_title {
font-size: 2.125rem;
font-weight: bold;
margin-bottom: 1.5rem;
}

.header__form_title_hr {
width: 100%;
margin-top: 0;
margin-bottom: 1.5rem;
border: 0;
border-top: 1px solid #e0e0e0;
border-bottom: 1px solid #e0e0e0;
}
6 changes: 6 additions & 0 deletions localgov_forms.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ localgov_forms.address_change:
- core/drupal
- core/once
- core/jquery

localgov_forms.form_header_block:
version: VERSION
css:
theme:
css/localgov_forms_header_block.css: {}
106 changes: 106 additions & 0 deletions localgov_forms.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;

/**
* Implements hook_theme().
Expand All @@ -20,6 +21,16 @@ function localgov_forms_theme() {
'localgov_forms_uk_address' => [
'render element' => 'element',
],
// Form header block.
'localgov_forms_form_header_block' => [
'variables' => [
'formTitle' => '',
'wizardPageTitle' => '',
'currentPage' => '',
'userDescription' => '',
],
'render element' => 'block',
],
];
}

Expand Down Expand Up @@ -55,3 +66,98 @@ function template_preprocess_localgov_forms_uk_address(array &$variables) {
function localgov_forms_preprocess_webform(array &$variables) {
$variables['#attached']['library'][] = 'localgov_forms/localgov_forms.form_errors';
}

/**
* Implements hook_form_FORM_ID_alter().
*/
function localgov_forms_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
// Check if this is a webform submission form.
if (strpos($form_id, 'webform_submission_') === 0) {
// Get the webform.
$webform = $form_state->getFormObject()->getWebform();
// Check if the form header block configuration is enabled.
if ($webform->getThirdPartySetting('localgov_forms', 'display_header_block', FALSE)) {
// Assuming you have an instance of FormHeaderBlock.
$form_header_block = \Drupal::service('plugin.manager.block')->createInstance('localgov_forms_form_header_block', []);
// Build the block.
$block_build = $form_header_block->build($form_state);

// Check if the block has content to render.
if (!empty($block_build)) {
// Add the block as a prefix to the form.
$form['#prefix'] = \Drupal::service('renderer')->render($block_build);
}
}
}
}

/**
* Implements hook_form_alter().
*/
function localgov_forms_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
// Check if this is a webform edit form.
if (strpos($form_id, 'webform_settings_form') === 0) {
// Get the webform.
$webform = $form_state->getFormObject()->getEntity();

$form['form_settings']['form_header_block_settings'] = [
'#type' => 'details',
'#title' => t('Form header settings'),
'#open' => TRUE,
'#weight' => -9999,
];
// Add the "User Description" field.
$form['form_settings']['form_header_block_settings']['user_description'] = [
'#type' => 'textarea',
'#title' => t('User Description'),
'#default_value' => $webform->getThirdPartySetting('localgov_forms', 'user_description', ''),
'#description' => t('A usasge description for this form that is shown to users.'),
// Adjust weight as needed.
'#weight' => 10,
];

$form['form_settings']['form_header_block_settings']['display_header_block'] = [
'#type' => 'checkbox',
'#title' => t('Display Form Header Block'),
'#default_value' => $webform->getThirdPartySetting('localgov_forms', 'display_header_block', TRUE),
'#description' => t('Enable the form header block to be displayed on this webform.'),
// Adjust weight as needed.
'#weight' => 1,
];

// Add a submit handler to save the setting.
$form['actions']['submit']['#submit'][] = 'localgov_forms_webform_settings_form_submit';
}
// Check if this is a webform submission form.
elseif (strpos($form_id, 'webform_submission_') === 0) {
// Get the webform.
$webform = $form_state->getFormObject()->getWebform();
// Check if the form header block is enabled.
if ($webform->getThirdPartySetting('localgov_forms', 'display_header_block', FALSE)) {
// Store the form state in the form.
$form['#form_state'] = $form_state;
}
}
}

/**
* Submit handler for webform edit form.
*/
function localgov_forms_webform_settings_form_submit(array &$form, FormStateInterface $form_state) {
// Get the webform.
$webform = $form_state->getFormObject()->getEntity();

// Get the checkbox value.
$enable_form_header_block = $form_state->getValue('display_header_block');

// Save the setting to the webform.
$webform->setThirdPartySetting('localgov_forms', 'display_header_block', $enable_form_header_block);
$webform->save();

// Get the user description value.
$user_description = $form_state->getValue('user_description');

// Save the user description setting to the webform.
$webform->setThirdPartySetting('localgov_forms', 'user_description', $user_description);
$webform->save();
}
208 changes: 208 additions & 0 deletions src/Event/FormHeaderDisplayEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

namespace Drupal\localgov_forms\Event;

use Drupal\Component\EventDispatcher\Event;

/**
* Event that is fired when displaying the page header.
*/
class FormHeaderDisplayEvent extends Event {

const EVENT_NAME = 'localgov_forms.form_header_display';

/**
* Entity associated with the current route.
*
* @var \Drupal\Core\Entity\EntityInterface|null
*/
protected $entity = NULL;

/**
* The form title override.
*
* @var array|string|null
*/
protected $formTitle = NULL;

/**
* The wizard page title override.
*
* @var array|string|null
*/
protected $wizardPageTitle = NULL;

/**
* The current page title override.
*
* @var array|string|null
*/
protected $currentPage = NULL;

/**
* The current page title override.
*
* @var array|string|null
*/
protected $pageIndex = NULL;

/**
* The Form user Description override.
*
* @var array|string|null
*/
protected $userDescription = NULL;


/**
* Should the page header block be displayed?
*
* @var bool
*/
protected $visibility = TRUE;

/**
* Cache tags override.
*
* @var array|null
*/
protected $cacheTags = NULL;

/**
* {@inheritdoc}
*/
public function __construct($entity) {
$this->entity = $entity;
}

/**
* Entity getter.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity.
*/
public function getEntity() {
return $this->entity;
}

/**
* Form Title getter.
*
* @return array|string|null
* The form title
*/
public function getFormTitle() {
return $this->formTitle;
}

/**
* Form Title setter.
*
* @param array|string|null $formTitle
* The form title.
*/
public function setFormTitle($formTitle) {
$this->formTitle = $formTitle;
}

/**
* Wizard Page Title getter.
*
* @return array|string|null
* The title.
*/
public function getWizardPageTitle() {
return $this->wizardPageTitle;
}

/**
* Wizard Page Title setter.
*
* @param array|string|null $wizard_page_title
* The title.
*/
public function setWizardPageTitle($wizard_page_title) {
$this->wizardPageTitle = $wizard_page_title;
}

/**
* Current page getter.
*
* @return array|string|null
* The current page title.
*/
public function getCurrentPage() {
return $this->currentPage;
}

/**
* Current page setter.
*
* @param array|string|null $current_page
* The current page title.
*/
public function setCurrentPage($current_page) {
$this->currentPage = $current_page;
}

/**
* Form User Description getter.
*
* @return array|string|null
* The User description title
*/
public function getFormUserDescription() {
return $this->userDescription;
}

/**
* Form Summary setter.
*
* @param array|string|null $form_summary
* The form summary.
*/
public function setFormSummary($form_summary) {
$this->userDescription = $form_summary;
}

/**
* Visibility getter.
*
* @return bool|null
* The title.
*/
public function getVisibility() {
return $this->visibility;
}

/**
* Visibility setter.
*
* @param bool $visibility
* The visibility.
*/
public function setVisibility($visibility) {
$this->visibility = $visibility;
}

/**
* Cache tags getter.
*
* @return array|null
* Cache tags array if set.
*/
public function getCacheTags() {
return $this->cacheTags;
}

/**
* Cache tags setter.
*
* @param array $cacheTags
* The cache tags.
*/
public function setCacheTags(array $cacheTags) {
$this->cacheTags = $cacheTags;
}

}
Loading
Loading