Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added weight quick form #507

Open
wants to merge 10 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Document farmOS cron set-up: https://farmos.org/hosting/install#cron
- Added a Weigh quick form module.

### Changed

Expand Down
8 changes: 8 additions & 0 deletions modules/quick/weight/farm_quick_weight.info.yml
@@ -0,0 +1,8 @@
name: Weight Quick Form
description: Quick form for recording animal weights
type: module
package: farmOS Quick Forms
core_version_requirement: ^9
dependencies:
- farm:farm_observation
- farm:farm_quick
204 changes: 204 additions & 0 deletions modules/quick/weight/src/Plugin/QuickForm/Weight.php
@@ -0,0 +1,204 @@
<?php

namespace Drupal\farm_quick_weight\Plugin\QuickForm;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\farm_quick\Plugin\QuickForm\QuickFormBase;
use Drupal\farm_quick\Traits\QuickAssetTrait;
use Drupal\farm_quick\Traits\QuickLogTrait;
use Drupal\farm_quick\Traits\QuickQuantityTrait;
use Drupal\farm_quick\Traits\QuickStringTrait;
use Psr\Container\ContainerInterface;

/**
* Weight quick form.
*
* @QuickForm(
* id = "weight",
* label = @Translation("Weights"),
* description = @Translation("Record animal weights."),
* helpText = @Translation("This form will create animal weight logs"),
* permissions = {
* "create observation log",
* }
* )
*/
class Weight extends QuickFormBase {

use QuickAssetTrait;
use QuickLogTrait;
use QuickQuantityTrait;
use QuickStringTrait;

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;

/**
* Constructs a QuickFormBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MessengerInterface $messenger, ModuleHandlerInterface $module_handler, TimeInterface $time, EntityTypeManagerInterface $entity_type_manager, StateInterface $state) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $messenger);
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
$this->state = $state;
$this->time = $time;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('messenger'),
$container->get('module_handler'),
$container->get('datetime.time'),
$container->get('entity_type.manager'),
$container->get('state'),

);
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {

// Get the units from storage.
$unit_id = $this->state->get('farm_quick_weight.unit');
$unit = $this->entityTypeManager->getStorage('taxonomy_term')->load($unit_id);

// Date of weighing.
$form['date'] = [
'#type' => 'date',
'#title' => $this->t('Date'),
'#date_year_range' => '-10:+3',
'#default_value' => date('Y-m-d', $this->time->getRequestTime()),
'#required' => TRUE,
];

// Animal being weighed.
$form['animal'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('Animal'),
'#target_type' => 'asset',
'#target_bundle' => 'animal',
'#required' => TRUE,
];

// Weight.
$form['weight'] = [
'#type' => 'textfield',
'#title' => $this->t('Weight'),
'#size' => 10,
'#required' => TRUE,
];

// Weight unit.
$form['unit'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('Units'),
'#target_type' => 'taxonomy_term',
'#selection_settings' => [
'target_bundles' => ['unit'],
],
'#autocreate' => [
'bundle' => 'unit',
],
'#default_value' => $unit,

];

return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {

$log_type = "observation";

// Get the form values.
$date = $form_state->getValue('date');
$weight = $form_state->getValue('weight');
$unit = $form_state->getValue('unit');
$animal = $form_state->getValue('animal');

// Get the entities that belong to the form values.
$unit_entity = $this->entityTypeManager->getStorage('taxonomy_term')->load($unit);
$this->state->set('farm_quick_weight.unit', $unit_entity->id());
$asset = $this->entityTypeManager->getStorage('asset')->load($animal);

// Create the log name from the animal asset, weight and unit.
$log_name = $this->t("Weight of @asset is @weight @unit", [
'@asset' => $asset->label(),
'@weight' => $weight,
'@unit' => $unit_entity->label(),
]);

// Create the log.
$this->createLog([
'type' => $log_type,
'name' => $log_name,
'timestamp' => strtotime($date),
'asset' => $asset,
'quantity' => [
[
'measure' => 'weight',
'value' => $weight,
'unit' => $unit_entity,
],
],
'status' => 'done',
]);
}

}
86 changes: 86 additions & 0 deletions modules/quick/weight/tests/Kernel/QuickWeightTests.php
@@ -0,0 +1,86 @@
<?php

namespace Drupal\Tests\farm_quick_weight\Kernel;

use Drupal\asset\Entity\Asset;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\farm_quick\Kernel\QuickFormTestBase;

/**
* Tests for farmOS weight quick form.
*
* @group farm
*/
class QuickWeightTests extends QuickFormTestBase {

/**
* Quick form ID.
*
* @var string
*/
protected $quickFormId = 'weight';

/**
* {@inheritdoc}
*/
protected static $modules = [
'farm_animal',
'farm_id_tag',
'farm_observation',
'farm_quantity_standard',
'farm_quick_weight',
'farm_unit',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig([
'farm_animal',
'farm_observation',
'farm_quantity_standard',
]);
}

/**
* Test weight quick form submission.
*/
public function testQuickBirth() {

// Create three animals (two females of different breeds, one male).
$animal1 = Asset::create([
'name' => 'Animal 1',
'type' => 'animal',
'sex' => 'F',
]);
$animal->save();

$unit = Term::create([
'name' => 'kg',
'vid' => 'kg',
]);
$unit->save();

// Submit the birth quick form.
$this->submitQuickForm([
'animal' => ['target_id' => $animal1->id()],
'weight' => '12.5',
'unit' => ['target_id' => $unit->id()],
]);

// Check the log.
$logs = $this->logStorage->loadMultiple();
$this->assertCount(1, $logs);

$log = $logs[1];
$this->assertEquals($log->getType(), 'observation');
$this->assertEquals($log->getName(), $this->t("Weight of @asset is @weight @unit", [
'@asset' => $animal1->label(),
'@weight' => '12.5',
'@unit' => $unit->label(),
]));
}

}