Commit 0db5d0de authored by bojanz's avatar bojanz

Issue #2872570 followup: Fix the default value handling.

parent cded69f9
......@@ -91,8 +91,7 @@ field.value.address_country:
label: 'Country code'
field.value.address_zone:
type: address_zone
label: 'Default value'
type: ignore
field.field_settings.address:
type: mapping
......
......@@ -113,7 +113,6 @@ class Zone extends FormElement {
$element_state['territories'] = $element_state['territories'] ?: [NULL];
self::setElementState($element['#parents'], $form_state, $element_state);
}
$element['#required'] = TRUE;
$element = [
'#tree' => TRUE,
......
......@@ -16,6 +16,7 @@ use Drupal\Core\TypedData\DataDefinition;
* label = @Translation("Zone"),
* description = @Translation("An entity field containing a zone"),
* category = @Translation("Address"),
* list_class = "\Drupal\address\Plugin\Field\FieldType\ZoneItemList",
* default_widget = "address_zone_default",
* cardinality = 1,
* )
......
<?php
namespace Drupal\address\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Form\FormStateInterface;
/**
* Represents a list of zone item field values.
*
* Works around core not serializing/unserializing default values.
*/
class ZoneItemList extends FieldItemList {
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
if ($default_value = $this->getFieldDefinition()->getDefaultValue($this->getEntity())) {
foreach ($default_value as $index => $value) {
$default_value[$index] = unserialize($value);
}
$this->setValue($default_value, $notify);
}
else {
parent::applyDefaultValue($notify);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
$default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);
if ($default_value) {
foreach ($default_value as $index => $value) {
$default_value[$index] = serialize($value);
}
}
return $default_value;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment