Skip to content

Fix crash when editing taxonomy blueprints#48

Merged
tdwesten merged 5 commits intotdwesten:mainfrom
tylerdak:fix/taxonomies-blueprint-parent-edit
Apr 2, 2025
Merged

Fix crash when editing taxonomy blueprints#48
tdwesten merged 5 commits intotdwesten:mainfrom
tylerdak:fix/taxonomies-blueprint-parent-edit

Conversation

@tylerdak
Copy link
Contributor

Hi!

I've noticed Statamic Builder causes a crash when trying to edit Taxonomy Blueprints.

In the TaxonomyBlueprintsController provided by Statamic-Builder, the blueprint parameter passed into ::edit gets overridden and then potentially passed into parent::edit despite the malformed parameter:

// $blueprint is the string handle for the blueprint
public function edit($taxonomy, $blueprint)
{
    $blueprint = $taxonomy->termBlueprint($blueprint);
    // ...
    // parent::edit gets an object instead of the string passed into this method
    return parent::edit($taxonomy, $blueprint);

This results in the parent::edit method calling $taxonomy->termBlueprint again except with an object instead of the expected string. Here's the resulting error and stack trace:

[2025-03-15 01:43:13] local.ERROR: array_key_exists(): Argument #1 ($key) must be a valid array offset type {"exception":"[object] (TypeError(code: 0): array_key_exists(): Argument #1 ($key) must be a valid array offset type at ...path-to-app/vendor/laravel/framework/src/Illuminate/Collections/Collection.php:468)
[stacktrace]
#0 ...path-to-app/vendor/laravel/framework/src/Illuminate/Collections/Collection.php(468): array_key_exists()
#1 ...path-to-app/vendor/statamic/cms/src/Taxonomies/Taxonomy.php(151): Illuminate\\Support\\Collection->get()
#2 ...path-to-app/vendor/statamic/cms/src/Taxonomies/Taxonomy.php(125): Statamic\\Taxonomies\\Taxonomy->getBaseTermBlueprint()
#3 ...path-to-app/vendor/statamic/cms/src/Http/Controllers/CP/Taxonomies/TaxonomyBlueprintsController.php(39): Statamic\\Taxonomies\\Taxonomy->termBlueprint()
#4 ...path-to-app/vendor/tdwesten/statamic-builder/src/Http/Controllers/TaxonomyBlueprintsController.php(26): Statamic\\Http\\Controllers\\CP\\Taxonomies\\TaxonomyBlueprintsController->edit()
#5 ...path-to-app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): Tdwesten\\StatamicBuilder\\Http\\Controllers\\TaxonomyBlueprintsController->edit()

To fix this, I've stored the original blueprint handle passed into the method in a temporary variable. That temporary variable then gets passed to parent::edit instead of the Blueprint object:

$blueprint = $taxonomy->termBlueprint($givenBlueprint = $blueprint);
// ...
return parent::edit($taxonomy, $givenBlueprint);

This bug is technically present in the CollectionBlueprintsController too. However, Statamic uses Blink to get the result for the $collection->entryBlueprint call. So, when the parent calls entryBlueprint again (after the first call being by Statamic-Builder's implementation), Blink returns the first result so no error gets thrown. I've implemented a similar patch to the CollectionBlueprintsController too, though.

Let me know if there's anything else I can do to help get this merged! Thanks!

@tdwesten
Copy link
Owner

Hi @tylerdak,

Could you provide me with a test case to evaluate your code?

@tylerdak tylerdak force-pushed the fix/taxonomies-blueprint-parent-edit branch from 9b19585 to 842ae7c Compare March 25, 2025 23:24
@tylerdak tylerdak force-pushed the fix/taxonomies-blueprint-parent-edit branch from 842ae7c to 1985cbe Compare March 25, 2025 23:26
@tylerdak
Copy link
Contributor Author

Hi,

I've added a test case. It's in this branch's history now. It's also in tylerdak:test/taxonomies-blueprint-parent-edit which might be more convenient for testing against the current version of statamic-builder.

I did have to make some changes to TestCase.php and add an autoload-dev entry to get Statamic to load properly in the test.

@tdwesten
Copy link
Owner

tdwesten commented Apr 2, 2025

@tylerdak Thanks a lot!

@tdwesten tdwesten merged commit 95df80d into tdwesten:main Apr 2, 2025
3 checks passed
@tylerdak tylerdak deleted the fix/taxonomies-blueprint-parent-edit branch April 2, 2025 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants