Closed vinkla closed 3 months ago
Would it work do use static properties on the custom field and update Field::make
to use those if called without arguments? (not sure how static properties work with inheritance)
class BlockID extends Field
{
protected static string $label = 'HTML Anchor ID';
protected static string $name = 'block_id';
}
If not, I think overriding make
would be a sufficient given how little that method is doing. Note that I have not yet used extended-acf in production, but I can definitely see the need for creating common / shared fields like this.
Thanks for sharing ideas @nikrowell 🙌
Would it work do use static properties on the custom field and update
Field::make
to use those if called without arguments?
This is an interesting idea. Currently, we set the name and label in the $settings
property:
If not, I think overriding make would be a sufficient given how little that method is doing.
You're probably right. I've solved it by overriding the make
method and giving it default values:
final class BlockID extends Text
{
public static function make(string $label = 'HTML Anchor ID', string|null $name = 'block_id'): static
{
return parent::make($label, $name)->helperText(
'Enter a word or two — without spaces — to make a unique web address just for this block, called an "anchor". Then, you\'ll be able to link directly to this section of your page. [Learn more about anchors.](https://wordpress.org/documentation/article/page-jumps/)',
);
}
}
The application I'm working on contains multiple blocks with the field below. Currently, we have kept the code in a WET approach, copying and pasting the block across all blocks.
It would be great to create a custom field class and import it instead:
The
Field
class can be extended. However, if you want to overwrite themake
method, you must include the arguments forlabel
andname
to comply with theField
class.What can we do instead? We could set up a new method called
create
that calls themake
method internally, but it would be nice to be able to use themake
method without passing any arguments. We can keep all settings internal in theBlockID
field class. We could also add default values to themake
parmeters.Do you have any ideas on how to improve this? How do Laravel Nova or Filament handle this?