Skip to content

Commit

Permalink
Add missing form functions
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenoh committed Nov 2, 2023
1 parent 056bb85 commit 612275a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
13 changes: 4 additions & 9 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ parameters:
path: src/functions/file.php

-
message: "#^Cannot access offset mixed on mixed\\.$#"
message: "#^Call to an undefined method object\\:\\:setFormId\\(\\)\\.$#"
count: 2
path: src/functions/form.php

Expand All @@ -866,22 +866,17 @@ parameters:
path: src/functions/form.php

-
message: "#^Function form_set_error\\(\\) has no return type specified\\.$#"
count: 1
path: src/functions/form.php

-
message: "#^Function form_set_error\\(\\) has parameter \\$limit_validation_errors with no type specified\\.$#"
message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(mixed\\)\\: mixed\\)\\|null, 'strval' given\\.$#"
count: 1
path: src/functions/form.php

-
message: "#^Function form_set_error\\(\\) has parameter \\$message with no type specified\\.$#"
message: "#^Parameter \\#1 \\$form_arg of method Drupal\\\\Core\\\\Form\\\\FormBuilderInterface\\:\\:buildForm\\(\\) expects Drupal\\\\Core\\\\Form\\\\FormInterface\\|string, object given\\.$#"
count: 1
path: src/functions/form.php

-
message: "#^Function form_set_error\\(\\) has parameter \\$name with no type specified\\.$#"
message: "#^Parameter \\#1 \\$form_arg of method Drupal\\\\Core\\\\Form\\\\FormBuilderInterface\\:\\:getForm\\(\\) expects Drupal\\\\Core\\\\Form\\\\FormInterface\\|string, object given\\.$#"
count: 1
path: src/functions/form.php

Expand Down
83 changes: 78 additions & 5 deletions src/functions/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,80 @@

declare(strict_types=1);

function form_set_error($name = null, $message = '', $limit_validation_errors = null)
use Drupal\Core\Form\FormStateInterface;
use Retrofit\Drupal\Form\DrupalGetForm;
use Retrofit\Drupal\Form\ArrayAccessFormState;

/**
* @param mixed[] $form_state
* @return mixed[]
*/
function drupal_build_form(string $form_id, array &$form_state): array
{
$form_object = \Drupal::classResolver(DrupalGetForm::class);
$form_object->setFormId($form_id);
$original_form_state = $form_state;
$form_state = new ArrayAccessFormState();
foreach ($original_form_state as $offset => $value) {
$form_state[$offset] = $value;
}
return \Drupal::formBuilder()->buildForm($form_object, $form_state);
}

function drupal_form_submit(string $form_id, FormStateInterface $form_state): void
{
\Drupal::formBuilder()->submitForm($form_id, $form_state);
}

/**
* @return mixed[]
*/
function drupal_get_form(string $form_id): array
{
$form_object = \Drupal::classResolver(DrupalGetForm::class);
$form_object->setFormId($form_id);
return \Drupal::formBuilder()->getForm($form_object);
}

/**
* @param mixed[] $element
*/
function form_error(array &$element, string $message = ''): void
{
$form = &drupal_static(__FUNCTION__, array());
form_set_error(implode('][', (array) $element['#parents']), $message);
}

function form_load_include(
FormStateInterface &$form_state,
string $type,
string $module,
?string $name = null
): string|false {
return $form_state->loadInclude($module, $type, $name);
}

/**
* @param ?string[] $limit_validation_errors
* @return mixed[]
*/
function form_set_error(?string $name = null, string $message = '', ?array $limit_validation_errors = null): array
{
// @todo Find a way to get form state to this really works.
$form = &drupal_static(__FUNCTION__, []);
$form = (array) $form;
$sections = &drupal_static(__FUNCTION__ . ':limit_validation_errors');
if (isset($limit_validation_errors)) {
$sections = $limit_validation_errors;
}
if (isset($name) && !isset($form[$name])) {
$record = true;
if (isset($sections)) {
if (is_array($sections)) {
$record = false;
foreach ($sections as $section) {
if (array_slice(explode('][', $name), 0, count($section)) === array_map('strval', $section)) {
if (
array_slice(explode('][', $name), 0, count((array) $section))
=== array_map('strval', (array) $section)
) {
$record = true;
break;
}
Expand All @@ -24,10 +85,22 @@ function form_set_error($name = null, $message = '', $limit_validation_errors =
$form[$name] = $message;
}
}

return $form;
}

/**
* @param mixed[] $element
*/
function form_set_value(array $element, mixed $value, FormStateInterface &$form_state): void
{
$form_state->setValueForElement($element, $value);
}

function form_state_values_clean(FormStateInterface $form_state): void
{
$form_state->cleanValues();
}

function form_get_errors()
{
$form = form_set_error();
Expand Down

0 comments on commit 612275a

Please sign in to comment.