-
-
Notifications
You must be signed in to change notification settings - Fork 567
[5.x] Add a creating-entry
hook to allow default values to be set
#8643
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
base: 5.x
Are you sure you want to change the base?
[5.x] Add a creating-entry
hook to allow default values to be set
#8643
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On line 267, the event is already being dispatched:
$blueprint = $collection->entryBlueprint($request->blueprint);
The entryBlueprint
method can accept an entry as the second argument. So, maybe we can make the entry earlier and pass it into that, rather than manually dispatching the event again.
$entry = Entry::make()->collection($collection);
$blueprint = $collection->entryBlueprint($request->blueprint, $entry);
//...
$values = $entry->values()->all();
That was what I tried to do originally, but the event fires without the entry parameter being set. |
Oh yeah there's code in there to intentionally not fire the event. Bah. |
Any chance this PR or a solution for this topic (pre-filling of fields, replicator fields in our case, based on blueprints) will be part of the core? Currently we apply this PR as an patch to our code base after every cms update. Thanks again @ryanmitchell for this PR and solution! |
In it's current state, I don't think it'll get merged. But I'm leaving this open temporarily as a reminder to think about a permanent solution. |
You can achieve almost the same thing by applying a -
handle: replicator_field
field:
collapse: false
previews: true
fullscreen: true
sets:
new_set_group:
display: 'New Set Group'
instructions: null
icon: null
sets:
test_set_1:
display: 'Test set 1'
instructions: null
icon: null
fields:
-
handle: title
field:
input_type: text
antlers: false
type: text
display: Title
icon: text
localizable: false
listable: hidden
instructions_position: above
visibility: visible
hide_display: false
test_set_2:
display: 'Test set 2'
instructions: null
icon: null
fields:
-
handle: title
field:
input_type: text
antlers: false
type: text
display: Title
icon: text
localizable: false
listable: hidden
instructions_position: above
visibility: visible
hide_display: false
replicator_in_replicator:
display: 'Replicator in replicator'
instructions: null
icon: null
fields:
-
handle: replicator_field
field:
collapse: false
previews: true
fullscreen: true
sets:
new_set_group:
display: 'New Set Group'
instructions: null
icon: null
sets:
new_set:
display: 'New Set'
instructions: null
icon: null
fields:
-
handle: text
field:
input_type: text
antlers: false
type: text
display: Text
icon: text
localizable: false
listable: hidden
instructions_position: above
visibility: visible
hide_display: false
type: replicator
display: 'Replicator Field'
icon: replicator
localizable: false
listable: hidden
instructions_position: above
visibility: visible
hide_display: false
type: replicator
display: 'Replicator Field'
icon: replicator
localizable: false
listable: hidden
instructions_position: above
visibility: visible
hide_display: false
default:
-
id: lkhwp0kp
title: Ryan
type: test_set_1
enabled: true
-
id: lki12mr5
replicator_field:
-
id: lki12nxn
text: text
type: new_set
enabled: true
type: replicator_in_replicator
enabled: true Obviously the event lets you apply use non-fixed values, eg if you were pulling from an API. So it would be good to come up with a solution that allows for that. |
Thanks @ryanmitchell 🙏 So we hope @jasonvarga and team find a mergeable solution. |
You can use a composer patch to automate it. |
Awesome! Thank you @jasonvarga, I didn't know about this solution to apply patches! |
What about using a hook for this? You could pass the blank entry to it and let it be amended? |
EntryBlueprintFound
before creatingEntryBlueprintFound
before creating
@jasonvarga I've updated this to use a hook instead of an event. Does this sit any better with you? |
Just chiming in to say that this works great in its current state. Wouldn't mind to see it merged :) We're using it to define a random seed that needs to be present on first save. // app/Providers/AppServiceProvider.php
EntriesController::hook('creating-entry', function ($entry) {
$entry->set('rand', random_int(1, 9999));
}); |
EntryBlueprintFound
before creatingcreating-entry
hook to allow default values to be set
This PR fires a
creating-entry
hook on the 'create' screen of the CP, allowing you to programatically set default values for fields.You can see this has been asked for in a few places, e.g. statamic/ideas#964, https://discord.com/channels/489818810157891584/489818810157891586/1146021950112665682
You can then add a listener like follows:
Which lets you modify the default values:

Obviously titles can have their default values in other ways, but where this becomes really useful is when you want to have default replicator or grid values.