Skip to content
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

Update for craft 5 #186

Merged
merged 5 commits into from Mar 12, 2024
Merged

Update for craft 5 #186

merged 5 commits into from Mar 12, 2024

Conversation

kristoffeys
Copy link
Contributor

@kristoffeys kristoffeys commented Feb 16, 2024

  • updated dependencies
  • made changes to make element-api compatible with craft 5

@thisisablock
Copy link

thisisablock commented Feb 26, 2024

@kristoffeys thanks for your work - just tested out your PR but got some issues.
Due to the type changes i can reproduce multiple errors caused bey accessing uninitialized properties:

Typed property craft\elementapi\Plugin::$_defaultResourceAdapterConfig must not be accessed before initialization

This can be fixed with on craft\elementapi\Plugin:72 by changing
if ($this->_defaultResourceAdapterConfig !== null) {
to
if (isset($this->_defaultResourceAdapterConfig) && $this->_defaultResourceAdapterConfig !== null) {

Typed property craft\elementapi\resources\ElementResource::$resourceKey must not be accessed before initialization
Typed property craft\elementapi\resources\ElementResource::$meta must not be accessed before initialization

These errors will occur because the default values wont be set on craft\elementapi\resources\ElementResource.

Hotfix is to set the defaults in the config files:

'defaults' => [
        'resourceKey' => 'data',
        'meta' => null,
],

but may its better to fix that in the class to

    public string $resourceKey = 'data';
    public array $meta = [];

Used the 5.0.0-beta1 with an really simple config

<?php

use craft\elements\Entry;

return [
    'defaults' => [
        'resourceKey' => 'data',
        'meta' => null,
    ],
    'endpoints' => [
        'homepage.json' => function() {
            return [
                'elementType' => Entry::class,
                'cache' => false,
                'transformer' => function(Entry $entry) {
                    return [
                        'title' => 'Cool Page'
                    ];
                },
            ];
        }
    ]
];

@kristoffeys
Copy link
Contributor Author

Thanks for the feedback, for some reason i didn't have this issue in earlier versions. I have added the fixes you indicated.

@brandonkelly brandonkelly merged commit e8d0a9e into craftcms:main Mar 12, 2024
@brandonkelly
Copy link
Member

Thanks for the PR! I’ve just tagged Element API with these changes, however I brought Craft 4 compatibility back as well, as nothing here actually requires changes in Craft 5.

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.

None yet

3 participants