Add decimal field support to field factory service.
This commit is contained in:
parent
2c0aef36ce
commit
5f9b32beef
|
@ -120,6 +120,11 @@ Both methods expect an array of field definition options. These include:
|
|||
- `type` (required) - The field data type. Each type may require additional
|
||||
options. Supported types include:
|
||||
- `boolean` - True/false checkbox.
|
||||
- `decimal` - Decimal number with fixed precision. Additional options:
|
||||
- `precision` (optional) - Total number of digits (including after the
|
||||
decimal point). Defaults to 10.
|
||||
- `scale` (optional) - Number digits to the right of the decimal point.
|
||||
Defaults to 2.
|
||||
- `entity_reference` - Reference other entities. Additional options:
|
||||
- `target_type` (required) - The entity type to reference (eg: `asset`,
|
||||
`log`, `plan`)
|
||||
|
|
|
@ -116,6 +116,10 @@ class FarmFieldFactory implements FarmFieldFactoryInterface {
|
|||
$this->modifyBooleanField($field, $options);
|
||||
break;
|
||||
|
||||
case 'decimal':
|
||||
$this->modifyDecimalField($field, $options);
|
||||
break;
|
||||
|
||||
case 'entity_reference':
|
||||
$this->modifyEntityReferenceField($field, $options);
|
||||
break;
|
||||
|
@ -234,6 +238,40 @@ class FarmFieldFactory implements FarmFieldFactoryInterface {
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decimal field modifier.
|
||||
*
|
||||
* @param \Drupal\Core\Field\BaseFieldDefinition &$field
|
||||
* A base field definition object.
|
||||
* @param array $options
|
||||
* An array of options.
|
||||
*/
|
||||
protected function modifyDecimalField(BaseFieldDefinition &$field, array $options = []) {
|
||||
|
||||
// Set the precision and scale, if specified.
|
||||
if (!empty($options['precision'])) {
|
||||
$field->setSetting('precision', $options['precision']);
|
||||
}
|
||||
if (!empty($options['scale'])) {
|
||||
$field->setSetting('scale', $options['scale']);
|
||||
}
|
||||
|
||||
// Build form and view display settings.
|
||||
$field->setDisplayOptions('form', [
|
||||
'type' => 'number',
|
||||
'weight' => $options['weight']['form'] ?? 0,
|
||||
]);
|
||||
$view_display_options = [
|
||||
'label' => 'inline',
|
||||
'type' => 'number_decimal',
|
||||
'weight' => $options['weight']['view'] ?? 0,
|
||||
];
|
||||
if (!empty($options['scale'])) {
|
||||
$view_display_options['settings']['scale'] = $options['scale'];
|
||||
}
|
||||
$field->setDisplayOptions('view', $view_display_options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Entity reference field modifier.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue