Skip to content

4240: Added handler and element usage overview module #418

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
!/web/modules/custom/os2forms_email_handler
!/web/modules/custom/webform_migrate
!/web/modules/custom/os2forms_queued_email
!/web/modules/custom/os2forms_selvbetjening_deprecations

# Ignore site specific modules.
/web/sites/*/modules
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.

## [Under udvikling]

* Tilføjede handler og element brug oversigt.

## [4.2.1] 2025-05-06

* Sikrede at OS2Forms attachment elementer bliver detekteret korrekt
Expand Down
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ module:
os2forms_rest_api: 0
os2forms_sbsys: 0
os2forms_selvbetjening: 0
os2forms_selvbetjening_deprecations: 0
os2forms_sync: 0
os2forms_user_field_lookup: 0
os2forms_user_menu: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'OS2Forms deprecations'
type: module
description: Handler and element usage overview
package: 'OS2Forms'
core_version_requirement: ^9 || ^10
dependencies:
- webform:webform
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
os2forms_selvbetjening_deprecations.elements:
path: '/admin/os2forms_selvbetjening_deprecations/elements'
defaults:
_form: '\Drupal\os2forms_selvbetjening_deprecations\Form\ElementsForm'
_title: 'Elements'
requirements:
_permission: 'access webform overview'
options:
no_cache: TRUE

os2forms_selvbetjening_deprecations.handlers:
path: '/admin/os2forms_selvbetjening_deprecations/handlers'
defaults:
_form: '\Drupal\os2forms_selvbetjening_deprecations\Form\HandlersForm'
_title: 'Handlers'
requirements:
_permission: 'access webform overview'
options:
no_cache: TRUE
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace Drupal\os2forms_selvbetjening_deprecations\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\webform\Plugin\WebformElementManagerInterface;
use Drupal\webform\WebformEntityStorageInterface;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* Elements form.
*/
final class ElementsForm extends FormBase {
use StringTranslationTrait;

/**
* Constructor.
*/
public function __construct(
private readonly WebformElementManagerInterface $webformElementManager,
private readonly WebformEntityStorageInterface $webformEntityStorage,
) {
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
$container->get('plugin.manager.webform.element'),
$container->get('entity_type.manager')->getStorage('webform')
);
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'os2forms_selvbetjening_deprecations_elements';
}

/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $form
* @phpstan-return array<string, mixed>
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form_state->setMethod(Request::METHOD_GET);
$form['#after_build'] = [$this->afterBuild(...)];

$elementOptions = array_map(
$this->getWebformElementLabel(...),
$this->getWebformElementDefinitions()
);

$selectedElements = $this->getRequest()->get('elements');
if (!is_array($selectedElements)) {
$selectedElements = [];
}

$form['filters'] = [
'#type' => 'details',
'#title' => $this->t('Filters'),
];

$form['filters']['elements'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Elements'),
'#required' => TRUE,
'#options' => $elementOptions,
'#default_value' => $selectedElements,
'#multiple' => TRUE,
];

$form['filters']['actions']['#type'] = 'actions';

$form['filters']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Show webforms with selected elements'),
'#attributes' => ['name' => ''],
];

if (!empty($selectedElements)) {
foreach ($selectedElements as $type) {
$webforms[$type] = array_filter(
$this->webformEntityStorage->loadMultiple(),
static fn(WebformInterface $webform) => !empty(array_filter(
$webform->getElementsDecodedAndFlattened(),
fn(array $element) => $type === ($element['#type'] ?? NULL))
)
);
}

$form['webforms'] = [
'#type' => 'container',
'#weight' => 9999,
];

$form['webforms']['refresh'] = Link::fromTextAndUrl(
$this->t('Refresh'),
Url::fromRoute('<current>', $this->getRequest()->query->all())
)->toRenderable()
+ [
'#attributes' => [
'class' => ['button', 'btn'],
],
];

foreach ($webforms as $elementType => $forms) {
$element = $this->getWebformElementDefinitions()[$elementType];
$form['webforms'][$elementType] = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#title' => $this->getWebformElementLabel($element),
'#items' => $forms
? array_map(
static fn(WebformInterface $webform) => Link::fromTextAndUrl($webform->label(), $webform->toUrl('edit-form')),
$forms
)
: [$this->t('No forms')],
];
}
}

return $form;
}

/**
* Remove elements from being submitted as GET variables.
*/
public function afterBuild(array $element, FormStateInterface $form_state) {
unset($element['form_token'], $element['form_build_id'], $element['form_id']);

return $element;
}

/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $form
*/
public function submitForm(array &$form, FormStateInterface $formState): void {
}

/**
* Webform element definitions.
*
* @var array|null
*/
private ?array $webformElementDefinitions = NULL;

/**
* Get webform element definitions.
*
* @return array[]
* The definitions.
*/
private function getWebformElementDefinitions(): array {
if (NULL === $this->webformElementDefinitions) {
$this->webformElementDefinitions = $this->webformElementManager->getSortedDefinitions();
uasort($this->webformElementDefinitions, fn (array $a, array $b) => $this->isExcluded($a) <=> $this->isExcluded($b));
}

return $this->webformElementDefinitions;
}

/**
* Decide if a webform element is excluded.
*
* @see WebformElementManagerInterface::isExcluded()
*/
private function isExcluded(array $definition): bool {
return $this->webformElementManager->isExcluded($definition['id']);
}

/**
* Get webform element label.
*/
private function getWebformElementLabel(array $definition): string {
return sprintf('%s %s (%s)', $this->isExcluded($definition) ? '[excluded]' : '', $definition['label'], $definition['id']);
}

}
Loading
Loading