$name
$name : string
The name for this helper, used when you want to define a global custom callback for a given field
Helpers in the Dewdrop Fields API allow you to inject custom code into loops and other structures that would normally be tricky to customize.
Helpers do this by enabling your to set custom callbacks for a field. In cases where a custom callback has not been assigned, helpers may attempt to automatically create a callback, using information in the field (e.g. the type of a database-related field).
For all helpers, you can assign custom callbacks globally or on a per-instance basis. Here's an example of defining a custom callback globally for a field:
$field->assignHelperCallback( 'NameOfHelperToCustomize', function ($helper) { // Any custom logic you'd like to perform for this field. } );
In the above example, "NameOfHelperToCustomize" is the name of the helper as defined in the helper's $name class property. This string is case insensitive.
To defined a custom callback on a per-instance basis for a helper, you can do the following:
$helper->assign( 'my_model:field_id', function ($helper) { // Any custom logic you'd like to perform for this field. } );
If you'd like to decorate an existing callback with additional logic, that's possible using the getFieldAssignment() method:
$field->assignHelperCallback( 'NameOfHelperToCustomize', function ($helper) { return '' . call_user_func($helper->getFieldAssignment($field), $helper) . ''; } );
When a per-instance callback is assigned, that overrides any global or fallback callbacks. So, you can have an application-wide default (e.g. in a model) behavior for a field that is superseded in specific cases by adding a per-instance callback assignment for the field (e.g. in a view script).
assign(mixed $assignments, callable $callable = null) : \Dewdrop\Fields\Helper\HelperAbstract
Assign one more custom per-instance callbacks for this helper. If the $arguments param is an array, this method expects that the keys will be field IDs and the values will be callables, assigning custom callbacks for multiple fields in one call. If, however, $assignments is a string or a FieldInterface object, a single custom callback assignment will be made.
mixed | $assignments | |
callable | $callable |
getFieldAssignment(\Dewdrop\Fields\FieldInterface $field) : callable
Get the callback that will be used for the given FieldInterface object.
\Dewdrop\Fields\FieldInterface | $field |
detectCallableForField(\Dewdrop\Fields\FieldInterface $field) : mixed
Try to supply a default callback by looking at the supplied FieldInterface object. This method will only be called for a field if no global or per-instance custom callbacks are assigned.
If no callback candidate is found, just return false from this method, which will be detected by getFieldAssignment(), causing execution to halt.
\Dewdrop\Fields\FieldInterface | $field |
Either false or a callable.
wrapCallable(callable $callable, \Dewdrop\Fields\FieldInterface $field = null) : callable
Wrap a field's callback to ensure that a reference to the helper is always supplied as the first argument to the callback.
callable | $callable | |
\Dewdrop\Fields\FieldInterface | $field |