Field factory improvements #666

This commit is contained in:
Michael Stenta 2023-03-30 14:55:05 -04:00
commit cbe71f9cd0
3 changed files with 112 additions and 4 deletions

View File

@ -12,12 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Add "Speed" to the list of quantity measures #658](https://github.com/farmOS/farmOS/pull/658)
- [Include Fraction bundle fields in default Views #664](https://github.com/farmOS/farmOS/pull/664)
- [Allow map to be resized vertically #663](https://github.com/farmOS/farmOS/pull/663)
- [Add integer, decimal, and email field support to field factory service #666](https://github.com/farmOS/farmOS/pull/666)
### Changed
- [Do not add birth log mother to animal assets that already have parents #655](https://github.com/farmOS/farmOS/pull/655)
- [Simplify all map resize logic to use ResizeObserver #662](https://github.com/farmOS/farmOS/pull/662)
- [Replace all usages of docker-compose with native docker compose #627](https://github.com/farmOS/farmOS/pull/627)
- [Allow max_length to be overridden on string fields #666](https://github.com/farmOS/farmOS/pull/666)
### Fixed

View File

@ -120,6 +120,12 @@ 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.
- `email` - Email field.
- `entity_reference` - Reference other entities. Additional options:
- `target_type` (required) - The entity type to reference (eg: `asset`,
`log`, `plan`)
@ -133,10 +139,15 @@ Both methods expect an array of field definition options. These include:
- `fraction` - High-precision decimal number storage.
- `geofield` - Geometry on a map.
- `image` - Image upload.
- `integer` - Integer number. Additional options:
- `size` (optional) - The integer database column size (`tiny`,
`small`, `medium`, `normal`, or `big`). Defaults to `normal`.
- `list_string` - Select list with allowed values. Additional options:
- `allowed_values` - An associative array of allowed values.
- `allowed_values_function` - The name of a function that returns an
associative array of allowed values.
- `string` - Unformatted text field of fixed length. Additional options:
- 'max_length' - Maximum length. Defaults to 255.
- `string_long` - Unformatted text field of unlimited length.
- `text_long` - Formatted text field of unlimited length.
- `timestamp` - Date and time.

View File

@ -116,6 +116,14 @@ class FarmFieldFactory implements FarmFieldFactoryInterface {
$this->modifyBooleanField($field, $options);
break;
case 'decimal':
$this->modifyDecimalField($field, $options);
break;
case 'email':
$this->modifyEmailField($field, $options);
break;
case 'entity_reference':
$this->modifyEntityReferenceField($field, $options);
break;
@ -141,6 +149,10 @@ class FarmFieldFactory implements FarmFieldFactoryInterface {
$this->modifyIdTagField($field, $options);
break;
case 'integer':
$this->modifyIntegerField($field, $options);
break;
case 'inventory':
$this->modifyInventoryField($field, $options);
break;
@ -230,6 +242,62 @@ 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);
}
/**
* Email field modifier.
*
* @param \Drupal\Core\Field\BaseFieldDefinition &$field
* A base field definition object.
* @param array $options
* An array of options.
*/
protected function modifyEmailField(BaseFieldDefinition &$field, array $options = []) {
// Build form and view display settings.
$field->setDisplayOptions('form', [
'type' => 'email_default',
'weight' => $options['weight']['form'] ?? 0,
]);
$field->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'email_mailto',
'weight' => $options['weight']['view'] ?? 0,
]);
}
/**
* Entity reference field modifier.
*
@ -698,6 +766,33 @@ class FarmFieldFactory implements FarmFieldFactoryInterface {
]);
}
/**
* Integer field modifier.
*
* @param \Drupal\Core\Field\BaseFieldDefinition &$field
* A base field definition object.
* @param array $options
* An array of options.
*/
protected function modifyIntegerField(BaseFieldDefinition &$field, array $options = []) {
// Set the size, if specified.
if (!empty($options['size'])) {
$field->setSetting('size', $options['size']);
}
// Build form and view display settings.
$field->setDisplayOptions('form', [
'type' => 'number',
'weight' => $options['weight']['form'] ?? 0,
]);
$field->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'number_integer',
'weight' => $options['weight']['view'] ?? 0,
]);
}
/**
* Inventory field modifier.
*
@ -758,10 +853,10 @@ class FarmFieldFactory implements FarmFieldFactoryInterface {
*/
protected function modifyStringField(BaseFieldDefinition &$field, array $options = []) {
// Set default settings.
$field->setSetting('max_length', 255);
$field->setSetting('is_ascii', FALSE);
$field->setSetting('case_sensitive', FALSE);
// Set the maximum length, if specified.
if (!empty($options['max_length'])) {
$field->setSetting('max_length', $options['max_length']);
}
// Build form and view display settings.
$field->setDisplayOptions('form', [