This caused #731724-334: Convert comment settings into a field to make them work with CMI and non-node entities 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

Sorry but I don't understand what's expected here

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 :(

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 ?

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: Convert Field API to CMI

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 ?

to specify a default value for field instance

@andypost : you're being too cryptic.

This is filed as a bug report. Please explain, with sentences, what the bug is.

Category:bug» task
Issue tags:+API clean-up

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

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)

Status:Active» Postponed

Discussed in IRC. After #1735118: Convert Field API to CMI we could use any of _presave() hooks on Instance entity_presave()

Status:Postponed» Active

This still valid. Tryed after #1777956: Provide a way to define default values for entity fields

--- core/modules/comment/lib/Drupal/comment/Type/CommentItem.php
+++ core/modules/comment/lib/Drupal/comment/Type/CommentItem.php
@@ -32,6 +32,7 @@ public function getPropertyDefinitions() {
       static::$propertyDefinitions['status'] = array(
         'type' => 'integer',
         'label' => t('Comment status value'),
+        'settings' => array('default_value' => COMMENT_OPEN),
       );
     }
     return static::$propertyDefinitions;

No success

Status:Active» Closed (duplicate)

Issue summary:View changes

Updated issue summary.