Posted by andypost on February 18, 2013 at 12:56am
6 followers
Jump to:
| Project: | Drupal core |
| Version: | 8.x-dev |
| Component: | field system |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | postponed |
| Issue tags: | API clean-up |
Issue Summary
This caused #731724-334: Decouple comment.module from node by turning comment settings into a field code all over comment field
Problem
For field instance that created from Field UI module (means with browser) the default_value is not assigned and no hooks allows to inject needed default into saving field_instance
Proposed solution
Add a 'default_value' => array('#value' = $field_default_value... in field instance form for new field
Comments
#1
Sorry but I don't understand what's expected here
#2
I mean that code needs always check for existence a value in widget and everywhere
but better to populate "default_value" for field instance upon when it got saved first time but I found no way to make it
<?php
// in FieldOverview::submit()
$field = array(
'field_name' => $values['field_name'],
'type' => $values['type'],
'translatable' => $values['translatable'],
);
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => $this->entity_type,
'bundle' => $this->bundle,
'label' => $values['label'],
'widget' => array(
'type' => $values['widget_type'],
'weight' => $values['weight'],
),
);
// Create the field and instance.
try {
field_create_field($field);
field_create_instance($instance);
// and latter for existing field
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => $this->entity_type,
'bundle' => $this->bundle,
'label' => $values['label'],
'widget' => array(
'type' => $values['widget_type'],
'weight' => $values['weight'],
),
);
try {
field_create_instance($instance);
?>
No ability to alter :(
#3
So there's http://api.drupal.org/api/drupal/core%21modules%21field%21field.api.php/... but that's not ideal, maybe we should have a drupal_alter here ?
#4
quickly discussed with @swentel in IRC
<swentel> andypost: hook_field_create_instance() maybe ? Just skimmed fast through the issue, pretty busy here.<swentel> the annoying thing is of course that the instance has been saved at that point :/
<swentel> so there would be two saves, that's kind of stupid
<swentel> so, yeah, not ideal
<swentel> hmm there should be an alter for that maybe
<andypost> swentel, suppose drupal_alter() is enough. But maybe any kind of instance presave planned with field cmi?
<swentel> andypost: there's a presave method in the entity storage controller right ?
* swentel quickly checks
<swentel> hmm apparently not
<andypost> swentel, maybe hook_field_instance_presave() as analog of hook_field_presave()
<swentel> andypost: yeah, something like that could work
Probably we get this hook with field CMI convertion #1735118: Change notice: Convert Field API to CMI
#5
Hmm, it seems what you'd want is the field type to be able to specify a "default default value" for new fields of this type ?
#6
to specify a default value for field instance
#7
@andypost : you're being too cryptic.
This is filed as a bug report. Please explain, with sentences, what the bug is.
#8
np, I think that code #2 explains that we have no hook_field_instance_presave of drupal_alter() for instance when fiel_ui creates instance
#9
For example I'm adding a Comments field so the next step I need to configure it's default value (Open,Closed)
But field ui creates a field and instance for me without default value (Open) so widget, formatter needs always check for
isset($values[0])to not throw notices.Ability to provide default value on field/instance level will help DX to simplify formatters and widgets. Also we get standard way to store defaults for field instance.
Currently core "datetime" implements own constructor and instance settings value to provide a default value via annotations so:
What is a better way to initialize default value for field instance now?
Will CMI conversion help to set "default_value" or "default_value_function" for Instance? (probably presave hook for Instance entity is enough to control this)
#10
Discussed in IRC. After #1735118: Change notice: Convert Field API to CMI we could use any of
_presave()hooks on Instance entity_presave()