public function QuickQuantityTrait::createQuantity in farmOS 2.x
Create a quantity.
Parameters
array $values: An array of values to initialize the quantity with.
Return value
\Drupal\quantity\Entity\QuantityInterface The quantity entity that was created.
2 calls to QuickQuantityTrait::createQuantity()
- QuickLogTrait::createLog in modules/
core/ quick/ src/ Traits/ QuickLogTrait.php - Create a log.
- Test::submitForm in modules/
core/ quick/ tests/ modules/ farm_quick_test/ src/ Plugin/ QuickForm/ Test.php - Form submission handler.
File
- modules/
core/ quick/ src/ Traits/ QuickQuantityTrait.php, line 24
Class
- QuickQuantityTrait
- Provides methods for working with quantities.
Namespace
Drupal\farm_quick\TraitsCode
public function createQuantity(array $values = []) {
// If a type isn't set, default to "standard".
if (empty($values['type'])) {
$values['type'] = 'standard';
}
// Split value into numerator and denominator, if it isn't already.
if (!empty($values['value']) && !is_array($values['value'])) {
$fraction = Fraction::createFromDecimal($values['value']);
$values['value'] = [
'numerator' => $fraction
->getNumerator(),
'denominator' => $fraction
->getDenominator(),
];
}
// If the units are a term name, create or load the unit taxonomy term.
if (!empty($values['units'])) {
$term = $this
->createOrLoadTerm($values['units'], 'unit');
$values['units'] = $term;
}
// Start a new quantity entity with the provided values.
/** @var \Drupal\quantity\Entity\QuantityInterface $quantity */
$quantity = Quantity::create($values);
// Save the quantity.
$quantity
->save();
// Return the quantity entity.
return $quantity;
}