Skip to content

Commit

Permalink
Merge branch 'main' into add-node-type-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mglaman authored Dec 9, 2023
2 parents eba643d + 8137753 commit ddd12b3
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 77 deletions.
70 changes: 0 additions & 70 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,6 @@ parameters:
count: 1
path: src/Plugin/Field/FieldType/DecoratedFieldItem.php

-
message: "#^Method Retrofit\\\\Drupal\\\\Plugin\\\\Field\\\\FieldType\\\\DecoratedFieldItem\\:\\:applyDefaultValue\\(\\) return type has no value type specified in iterable type Drupal\\\\Core\\\\Field\\\\FieldItemInterface\\.$#"
count: 1
path: src/Plugin/Field/FieldType/DecoratedFieldItem.php

-
message: "#^Method Retrofit\\\\Drupal\\\\Plugin\\\\Field\\\\FieldType\\\\DecoratedFieldItem\\:\\:calculateDependencies\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -955,26 +950,6 @@ parameters:
count: 1
path: src/functions/node.php

-
message: "#^Call to method loadParents\\(\\) on an unknown class Drupal\\\\taxonomy\\\\TermStorageInterface\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Call to method loadTree\\(\\) on an unknown class Drupal\\\\taxonomy\\\\TermStorageInterface\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Call to static method loadMultiple\\(\\) on an unknown class Drupal\\\\taxonomy\\\\Entity\\\\Vocabulary\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Class Drupal\\\\taxonomy\\\\TermStorageInterface not found\\.$#"
count: 2
path: src/functions/taxonomy.php

-
message: "#^Function taxonomy_get_parents\\(\\) has no return type specified\\.$#"
count: 1
Expand All @@ -985,56 +960,11 @@ parameters:
count: 1
path: src/functions/taxonomy.php

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

-
message: "#^Function taxonomy_get_tree\\(\\) has parameter \\$load_entities with no type specified\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Function taxonomy_get_tree\\(\\) has parameter \\$max_depth with no type specified\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Function taxonomy_get_tree\\(\\) has parameter \\$parent with no type specified\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Function taxonomy_get_tree\\(\\) has parameter \\$vid with no type specified\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Function taxonomy_get_vocabularies\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Parameter \\#1 \\$entity of class Retrofit\\\\Drupal\\\\Entity\\\\WrappedConfigEntity constructor expects Drupal\\\\Core\\\\Config\\\\Entity\\\\ConfigEntityInterface, Drupal\\\\taxonomy\\\\VocabularyInterface given\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Parameter \\#1 \\$entity of class Retrofit\\\\Drupal\\\\Entity\\\\WrappedContentEntity constructor expects Drupal\\\\Core\\\\Entity\\\\ContentEntityInterface, Drupal\\\\taxonomy\\\\TermInterface given\\.$#"
count: 2
path: src/functions/taxonomy.php

-
message: "#^Parameter \\$term of anonymous function has invalid type Drupal\\\\taxonomy\\\\TermInterface\\.$#"
count: 2
path: src/functions/taxonomy.php

-
message: "#^Parameter \\$vocabulary of anonymous function has invalid type Drupal\\\\taxonomy\\\\VocabularyInterface\\.$#"
count: 1
path: src/functions/taxonomy.php

-
message: "#^Function theme\\(\\) never returns string so it can be removed from the return type\\.$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions src/Form/ArrayAccessFormState.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

final class ArrayAccessFormState extends FormState implements \ArrayAccess
{
public function __construct(bool $errors = false)
{
static::setAnyErrors($errors);
}

public function offsetExists(mixed $offset): bool
{
return isset($this->$offset);
Expand Down
45 changes: 41 additions & 4 deletions src/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,47 @@ class FormBuilder extends CoreFormBuilder
*/
public function buildForm(mixed $form_arg, FormStateInterface|array &$form_state): array
{
$original_form_state = $form_state;
$form_state = new ArrayAccessFormState();
if ($original_form_state instanceof FormStateInterface) {
$original_form_state = $original_form_state->getCacheableArray() + get_object_vars($original_form_state);
if ($form_state instanceof FormStateInterface) {
$original_form_state = $form_state->getCacheableArray() + get_object_vars($form_state) + [
'always_process' => $form_state->getAlwaysProcess(),
'buttons' => $form_state->getButtons(),
'cleanValueKeys' => $form_state->getCleanValueKeys(),
'complete_form' => $form_state->getCompleteForm(),
'errors' => $form_state->getErrors(),
'executed' => $form_state->isExecuted(),
'groups' => $form_state->getGroups(),
'input' => $form_state->getUserInput(),
'invalidToken' => $form_state->hasInvalidToken(),
'limit_validation_errors' => $form_state->getLimitValidationErrors(),
'method' => match (true) {
$form_state->isMethodType('POST') => 'POST',
$form_state->isMethodType('GET') => 'GET',
$form_state->isMethodType('HEAD') => 'HEAD',
$form_state->isMethodType('OPTIONS') => 'OPTIONS',
$form_state->isMethodType('PUT') => 'PUT',
$form_state->isMethodType('DELETE') => 'DELETE',
$form_state->isMethodType('TRACE') => 'TRACE',
$form_state->isMethodType('CONNECT') => 'CONNECT',
$form_state->isMethodType('PATCH') => 'PATCH',
default => 'POST',
},
'must_validate' => $form_state->isValidationEnforced(),
'no_redirect' => $form_state->isRedirectDisabled(),
'rebuild' => $form_state->isRebuilding(),
'rebuild_info' => $form_state->getRebuildInfo(),
'redirect' => $form_state->getRedirect(),
'submitted' => $form_state->isSubmitted(),
'submit_handlers' => $form_state->getSubmitHandlers(),
'temporary' => $form_state->getTemporary(),
'triggering_element' => $form_state->getTriggeringElement(),
'validate_handlers' => $form_state->getValidateHandlers(),
'validation_complete' => $form_state->isValidationComplete(),
'values' => $form_state->getValues(),
];
$form_state = new ArrayAccessFormState(call_user_func([get_class($form_state), 'hasAnyErrors']));
} else {
$original_form_state = $form_state;
$form_state = new ArrayAccessFormState();
}
foreach ($original_form_state as $offset => $value) {
$form_state[$offset] = $value;
Expand Down
3 changes: 3 additions & 0 deletions src/constants/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

declare(strict_types=1);

use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Logger\RfcLogLevel;

const CHECK_PLAIN = 0;

const LANGUAGE_NONE = LanguageInterface::LANGCODE_NOT_SPECIFIED;

const PASS_THROUGH = -1;

const WATCHDOG_EMERGENCY = RfcLogLevel::EMERGENCY;
Expand Down
6 changes: 6 additions & 0 deletions src/functions/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Extension\ExtensionPathResolver;
use Retrofit\Drupal\Controller\RetrofitTitleResolver;
Expand Down Expand Up @@ -67,6 +68,11 @@ function drupal_get_schema(?string $table = null, ?bool $rebuild = false): array
}
}

function drupal_hash_base64(string $data): string
{
return Crypt::hashBase64($data);
}

function drupal_is_https(): bool
{
return \Drupal::request()->isSecure();
Expand Down
23 changes: 23 additions & 0 deletions src/functions/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,26 @@ function db_table_exists(string $table): bool
{
return DB::get()->schema()->tableExists($table);
}

/**
* @param mixed[] $spec
* @param mixed[] $keys_new
*/
function db_add_field(string $table, string $field, array $spec, array $keys_new = array()): void
{
DB::get()->schema()->addField($table, $field, $spec, $keys_new);
}

/**
* @param mixed[] $spec
* @param mixed[] $keys_new
*/
function db_change_field(string $table, string $field, string $field_new, array $spec, array $keys_new = array()): void
{
DB::get()->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
}

function db_field_exists(string $table, string $field): bool
{
return DB::get()->schema()->fieldExists($table, $field);
}
1 change: 0 additions & 1 deletion src/functions/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Drupal\Core\Form\FormStateInterface;
use Retrofit\Drupal\Form\DrupalGetForm;
use Retrofit\Drupal\Form\ArrayAccessFormState;

/**
* @param mixed[] $form_state
Expand Down
1 change: 1 addition & 0 deletions src/functions/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function drupal_alter(
mixed &$context3 = null
): void {
\Drupal::moduleHandler()->alter($type, $data, $context1, $context2);
\Drupal::theme()->alter($type, $data, $context1, $context2);
}

function module_exists(string $module): bool
Expand Down
21 changes: 19 additions & 2 deletions src/functions/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Drupal\Core\Entity\EntityInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\TermStorageInterface;
Expand All @@ -27,12 +28,28 @@ function taxonomy_get_parents($tid)
);
}

function taxonomy_get_tree($vid, $parent = 0, $max_depth = null, $load_entities = false)
/**
* @return EntityInterface[]
*/
function taxonomy_get_term_by_name(string $name): array
{
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
assert($storage instanceof TermStorageInterface);
return $storage->loadByProperties(['name' => $name]);
}

/**
* @return WrappedContentEntity[]
*/
function taxonomy_get_tree(string $vid, int $parent = 0, ?int $max_depth = null, bool $load_entities = false): array
{
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
assert($storage instanceof TermStorageInterface);
return array_map(
static fn (TermInterface $term) => new WrappedContentEntity($term),
static function (object $term) {
assert($term instanceof TermInterface);
return new WrappedContentEntity($term);
},
$storage->loadTree($vid, $parent, $max_depth, $load_entities)
);
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
$loader->addPsr4('Drupal\\field\\', __DIR__ . '/../vendor/drupal/core/modules/field/src');
$loader->addPsr4('Drupal\\node\\', __DIR__ . '/../vendor/drupal/core/modules/node/src');
$loader->addPsr4('Drupal\\sqlite\\', __DIR__ . '/../vendor/drupal/core/modules/sqlite/src');
$loader->addPsr4('Drupal\\taxonomy\\', __DIR__ . '/../vendor/drupal/core/modules/taxonomy/src');
$loader->addPsr4('Drupal\\user\\', __DIR__ . '/../vendor/drupal/core/modules/user/src');
$loader->addPsr4('Drupal\\Tests\\user\\Traits\\', __DIR__ . '/../vendor/drupal/core/modules/user/tests/src/Traits');

Expand Down

0 comments on commit ddd12b3

Please sign in to comment.