-
-
Notifications
You must be signed in to change notification settings - Fork 178
Code completion for field methods #7078
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
Comments
I'd be happy to work on this. My preferred approach would be to migrate all the built-in field methods to the The biggest difficulty seems to be that ~10 of those methods, such as the Markdown and KirbyText-related ones, need access to the When a |
Don't start working on this cause I have a local branch very far along on this already. |
This is the branch where I moved all methods to the A major roadblock for this is the current possibility to overwrite/extend core field methods. I don't have a good idea yet how/if this could work when we move all field methods to the If we don't find a solution for that, it would be either adding a big breaking change or shifting to solution 2 ( |
The reference currently says that extensions cannot override the default Field methods, and links to the longer list of field methods: Thought after looking at the source code of
One solution would be to define every overridable method with a condition that checks for the presence of a registered method: class Field implements Stringable
{
// (…)
public function toEntries(Field $field): Collection
{
if ($this->hasMethodOverride('toEntries')) {
return $this->__call('toEntries');
}
// (…) Default implementation here
}
public function toFiles(string $separator = 'yaml')
{
if ($this->hasMethodOverride('toFiles')) {
return $this->__call('toFiles', [$separator]);
}
// (…) Default implementation here
}
} It requires a bit of boilerplate but looks manageable. (The boilerplate could be reduced with something like decorators, but it looks like PHP8's Attributes don't really work as decorators.) |
Resolves #7078 BREAKING CHANGE: Removed `Kirby\Content\Field::$aliases`. BREAKING CHANGE: Removed `Kirby\Cms\Core::fieldMethods()` and `Kirby\Cms\Core::fieldMethodsAliases()`.
The problem
The
Kirby\Content\Field
class is a type returned by common templating methods such as$page->content()->get('field_name')
.This object supports 56 methods documented here.
But in an IDE, only a few methods (10) are visible, because only 10 methods are statistically declared by the
Field
class (generated docs). The other 46 are created here instead:https://github.com/getkirby/kirby/blob/5.0.0-beta.4/config/methods.php
This means that most helper methods one might want to use with fields show an error in an IDE with any PHP language server. For instance, in Zed with the phpactor plugin:
This makes it needlessly hard to work with content fields in PHP code, since you have to rely on the documentation exclusively.
Expected outcome
When working with a
Kirby\Content\Field
instance, all utility methods listed in the docs should be visible in the IDE's code completion.Out of scope: field methods provided by extensions would not be listed.
Possible solutions
config/methods.php
to move all the built-in methods directly to theKirby\Content\Field
class.@method
tags to theKirby\Content\Field
class.Kirby\Content\Field
, but call the current implementation:The first option would be ideal, but is a bigger refactoring which might have some technical challenges and more technical risk.
The second option has no risk of breakage, but risks getting out of sync with the actual implementation. Because it uses the PHPDoc
@method
tag, it's a bit limited in what information it can convey (vs what a dedicated PHPDoc comment could contain).The third option is a variant of the second one, and has much of the same tradeoffs. It's a bit nicer to author, and the IDE's jump-to-implementation is just a tad nicer.
The text was updated successfully, but these errors were encountered: