mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Provide traits with helper methods for creating quantities and terms.
This commit is contained in:
parent
78fa65945b
commit
a52fd0b5ae
|
@ -4,5 +4,7 @@ type: module
|
||||||
package: farmOS
|
package: farmOS
|
||||||
core_version_requirement: ^9
|
core_version_requirement: ^9
|
||||||
dependencies:
|
dependencies:
|
||||||
|
- drupal:taxonomy
|
||||||
- farm:asset
|
- farm:asset
|
||||||
|
- farm:farm_quantity_standard
|
||||||
- log:log
|
- log:log
|
||||||
|
|
57
modules/core/quick/src/Traits/QuickQuantityTrait.php
Normal file
57
modules/core/quick/src/Traits/QuickQuantityTrait.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\farm_quick\Traits;
|
||||||
|
|
||||||
|
use Drupal\fraction\Fraction;
|
||||||
|
use Drupal\quantity\Entity\Quantity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides methods for working with quantities.
|
||||||
|
*/
|
||||||
|
trait QuickQuantityTrait {
|
||||||
|
|
||||||
|
use QuickTermTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a quantity.
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* An array of values to initialize the quantity with.
|
||||||
|
*
|
||||||
|
* @return \Drupal\quantity\Entity\QuantityInterface
|
||||||
|
* The quantity entity that was created.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
65
modules/core/quick/src/Traits/QuickTermTrait.php
Normal file
65
modules/core/quick/src/Traits/QuickTermTrait.php
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\farm_quick\Traits;
|
||||||
|
|
||||||
|
use Drupal\taxonomy\Entity\Term;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides methods for working with terms.
|
||||||
|
*/
|
||||||
|
trait QuickTermTrait {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a term.
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* An array of values to initialize the term with.
|
||||||
|
*
|
||||||
|
* @return \Drupal\taxonomy\TermInterface
|
||||||
|
* The term entity that was created.
|
||||||
|
*/
|
||||||
|
public function createTerm(array $values = []) {
|
||||||
|
|
||||||
|
// Alias 'vocabulary' to 'vid'.
|
||||||
|
if (!empty($values['vocabulary'])) {
|
||||||
|
$values['vid'] = $values['vocabulary'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start a new term entity with the provided values.
|
||||||
|
/** @var \Drupal\taxonomy\TermInterface $term */
|
||||||
|
$term = Term::create($values);
|
||||||
|
|
||||||
|
// Save the term.
|
||||||
|
$term->save();
|
||||||
|
|
||||||
|
// Return the term entity.
|
||||||
|
return $term;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a term name, create or load a matching term entity.
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* The term name.
|
||||||
|
* @param string $vocabulary
|
||||||
|
* The vocabulary to search or create in.
|
||||||
|
*
|
||||||
|
* @return \Drupal\taxonomy\TermInterface
|
||||||
|
* The term entity that was created or loaded.
|
||||||
|
*/
|
||||||
|
public function createOrLoadTerm(string $name, string $vocabulary) {
|
||||||
|
|
||||||
|
// First try to load an existing term.
|
||||||
|
$search = taxonomy_term_load_multiple_by_name($name, $vocabulary);
|
||||||
|
if (!empty($search)) {
|
||||||
|
return reset($search);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new term.
|
||||||
|
return $this->createTerm([
|
||||||
|
'name' => $name,
|
||||||
|
'vid' => $vocabulary,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue