Skip to content

Commit

Permalink
Add integer field support to field factory service.
Browse files Browse the repository at this point in the history
  • Loading branch information
mstenta committed Mar 30, 2023
1 parent 46adaa3 commit 2c0aef3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/development/module/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ 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
Expand Down
31 changes: 31 additions & 0 deletions modules/core/field/src/FarmFieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ protected function buildFieldDefinition(BaseFieldDefinition &$field, array $opti
$this->modifyIdTagField($field, $options);
break;

case 'integer':
$this->modifyIntegerField($field, $options);
break;

case 'inventory':
$this->modifyInventoryField($field, $options);
break;
Expand Down Expand Up @@ -698,6 +702,33 @@ protected function modifyIdTagField(BaseFieldDefinition &$field, array $options
]);
}

/**
* 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.
*
Expand Down

0 comments on commit 2c0aef3

Please sign in to comment.