clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,18 @@
langcode: en
status: true
dependencies:
module:
- farm_log
- farm_log_quantity
enforced:
module:
- farm_input
third_party_settings:
farm_log_quantity:
default_quantity_type: material
id: input
label: Input
description: ''
name_pattern: 'Input log [log:id]'
workflow: farm_log_workflow
new_revision: true
@@ -0,0 +1,9 @@
name: Input log
description: Adds an Input log type.
type: module
package: farmOS Logs
core_version_requirement: ^10
dependencies:
- farm:farm_entity
- farm:farm_quantity_material
- log:log
@@ -0,0 +1,31 @@
<?php
/**
* @file
* Provides Views data for farm_input module.
*/
/**
* Implements hook_views_data().
*/
function farm_input_views_data() {
$data = [];
// Add a quantity_material_type pseudo field to the log_field_data table.
// This pseudo field only has a filter configured to support filtering logs
// by the quantity material type.
$data['log_field_data']['quantity_material_type'] = [
'description' => t('Filter by the material type of quantities referenced by this log.'),
'entity_type' => 'quantity',
'entity_field' => 'material_type',
'filter' => [
'title' => t('Quantity material type'),
'description' => t('Filter by the material type of quantities referenced by this log.'),
'id' => 'log_quantity_material_type',
'field_name' => 'material_type',
],
];
return $data;
}
@@ -0,0 +1,46 @@
<?php
/**
* @file
* Provides Views runtime hooks for farm_input module.
*/
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_pre_view().
*/
function farm_input_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
// Alter the farm_log View.
if ($view->id() == 'farm_log') {
// Only alter the page_type and page_asset displays.
if (!in_array($display_id, ['page_type', 'page_asset'])) {
return;
}
// Bail if not a view of input logs.
if (!in_array('input', $args)) {
return;
}
// Add a filter for the quantity material type.
$table = 'log_field_data';
$field = 'quantity_material_type';
$filter_options = [
'id' => 'material_type_target_id',
'table' => $table,
'field' => $field,
'exposed' => TRUE,
'expose' => [
'label' => t('Material type'),
'identifier' => $field,
'multiple' => TRUE,
],
'type' => 'select',
'limit' => TRUE,
];
$view->addHandler($display_id, 'filter', $table, $field, $filter_options);
}
}
@@ -0,0 +1,74 @@
<?php
namespace Drupal\farm_input\Plugin\Log\LogType;
use Drupal\farm_entity\Plugin\Log\LogType\FarmLogType;
/**
* Provides the input log type.
*
* @LogType(
* id = "input",
* label = @Translation("Input"),
* )
*/
class Input extends FarmLogType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
// Lot number.
$options = [
'type' => 'string',
'label' => $this->t('Lot number'),
'description' => $this->t('If this harvest is part of a batch or lot, enter the lot number here.'),
'weight' => [
'form' => -45,
'view' => -45,
],
];
$fields['lot_number'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Method.
$options = [
'type' => 'string',
'label' => $this->t('Method'),
'description' => $this->t('How was this input applied?'),
'weight' => [
'form' => -30,
'view' => -30,
],
];
$fields['method'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Purchase date.
$options = [
'type' => 'timestamp',
'label' => $this->t('Purchase date'),
'description' => $this->t('When was this input purchased (if applicable)?'),
'weight' => [
'form' => -35,
'view' => -35,
],
];
$fields['purchase_date'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Source.
$options = [
'type' => 'string',
'label' => $this->t('Source'),
'description' => $this->t('Where was this input obtained? Who manufactured it?'),
'weight' => [
'form' => -40,
'view' => -40,
],
];
$fields['source'] = $this->farmFieldFactory->bundleFieldDefinition($options);
return $fields;
}
}
@@ -0,0 +1,71 @@
<?php
namespace Drupal\farm_input\Plugin\views\filter;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid;
/**
* Filter handler for log quantity material type terms.
*
* @ViewsFilter("log_quantity_material_type")
*/
class LogQuantityMaterialType extends TaxonomyIndexTid {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
// Hard code the material_type vocabulary.
$options['vid'] = ['default' => 'material_type'];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildExtraOptionsForm($form, $form_state);
// Remove the vocabulary form element, use the hard coded default.
unset($form['vid']);
}
/**
* {@inheritdoc}
*/
public function query() {
// Bail if there are no filter values.
if (count($this->value) == 0) {
return;
}
elseif (count($this->value) == 1) {
// Sometimes $this->value is an array with a single element so convert it.
// @see TaxonomyIndexTidDepth::query().
if (is_array($this->value)) {
$this->value = current($this->value);
}
$operator = '=';
}
else {
$operator = 'IN';
}
// Build a subquery to find logs that reference a quantity with the
// specified material type.
$subquery = Database::getConnection()->select('log', 'l');
$subquery->addField('l', 'id');
$subquery->innerJoin('log__quantity', 'lq', 'l.id = lq.entity_id');
$subquery->innerJoin('quantity__material_type', 'qmt', 'lq.quantity_target_id = qmt.entity_id AND lq.quantity_target_revision_id = qmt.revision_id');
$subquery->condition('qmt.material_type_target_id', $this->value, $operator);
// Get the table alias for log_field_data.
$this->tableAlias = $this->query->ensureTable($this->view->storage->get('base_table'));
// Use the subquery in a condition on the views query to prevent duplicates.
$this->query->addWhere($this->options['group'], "$this->tableAlias.id", $subquery, 'IN');
}
}
@@ -0,0 +1,195 @@
<?php
namespace Drupal\Tests\farm_input\Functional;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
use Drupal\log\Entity\Log;
use Drupal\quantity\Entity\Quantity;
use Drupal\taxonomy\Entity\Term;
/**
* Test material type filter functionality.
*
* @see \Drupal\farm_input\Plugin\views\filter\LogQuantityMaterialType
*
* @group farm
*/
class InputViewMaterialTypeTest extends FarmBrowserTestBase {
/**
* Test user.
*
* @var \Drupal\user\Entity\User
*/
protected $user;
/**
* Material type terms.
*
* @var \Drupal\taxonomy\Entity\Term[]
*/
protected array $materialTypes;
/**
* Logs for testing.
*
* @var \Drupal\log\Entity\Log[]
*/
protected array $testLogs;
/**
* {@inheritdoc}
*/
protected static $modules = [
'farm_ui',
'farm_quantity_standard',
'farm_input',
];
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
// Create and login a user with permission to view logs.
$this->user = $this->createUser(['view any log']);
$this->drupalLogin($this->user);
// Create material type terms.
$material_type_names = ['compost', 'fertilizer', 'spray'];
$this->materialTypes = [];
foreach ($material_type_names as $term_name) {
$term = Term::create(['vid' => 'material_type', 'name' => $term_name]);
$term->save();
$this->materialTypes[$term_name] = $term;
}
// Create logs with quantities.
$test_log_definitions = [
// Input log with no quantities.
[
'log' => ['type' => 'input'],
'quantities' => [],
],
// Input log with standard quantity.
[
'log' => ['type' => 'input'],
'quantities' => [
['type' => 'standard', 'measure' => 'count'],
],
],
// Input log with compost material type quantity.
[
'log' => ['type' => 'input'],
'quantities' => [
['type' => 'material', 'measure' => 'weight', 'material_type' => $this->materialTypes['compost']],
],
],
// Input log with fertilizer material type quantity.
[
'log' => ['type' => 'input'],
'quantities' => [
['type' => 'material', 'measure' => 'weight', 'material_type' => $this->materialTypes['fertilizer']],
],
],
// Input log with multiple quantities.
[
'log' => ['type' => 'input'],
'quantities' => [
['type' => 'standard', 'measure' => 'count'],
['type' => 'material', 'measure' => 'weight', 'material_type' => $this->materialTypes['compost']],
['type' => 'material', 'measure' => 'weight', 'material_type' => $this->materialTypes['fertilizer']],
],
],
];
$this->testLogs = [];
foreach ($test_log_definitions as $log_definition) {
$this->createLogWithQuantities($log_definition['log'], $log_definition['quantities']);
}
}
/**
* Test material type filter functionality.
*/
public function testMaterialTypeFilter() {
// Get the input logs view.
$this->drupalGet('/logs/input');
// Assert that the Material type exposed filter exists.
$this->assertSession()->pageTextContains('Material type');
$this->assertSession()->fieldExists('edit-quantity-material-type');
// Assert that each material type is an option.
foreach ($this->materialTypes as $material_type) {
$this->assertSession()->pageTextContains($material_type->label());
}
// No filter, assert that each log is visible.
foreach ($this->testLogs as $log) {
$this->assertSession()->pageTextContainsOnce($log->label());
}
// Collect all material type ids.
$material_type_ids = array_map(function ($term) {
return $term->id();
}, $this->materialTypes);
// Test each material type individually.
$test_filters = $material_type_ids;
// Test filtering by all material types.
$test_filters[] = $material_type_ids;
// Filter by each material type test.
foreach ($test_filters as $material_types) {
// Query expected logs.
$expected_logs = \Drupal::entityTypeManager()->getStorage('log')->loadByProperties([
'quantity.entity:quantity.material_type' => $material_types,
]);
// Filter to only include logs with the specified material type.
// Make sure the parameter is an array.
$material_types = is_array($material_types) ? $material_types : [$material_types];
$this->drupalGet('/logs/input', ['query' => ['quantity_material_type' => $material_types]]);
// Assert that each expected log exists.
foreach ($expected_logs as $log) {
$this->assertSession()->pageTextContainsOnce($log->label());
}
}
}
/**
* Helper function to create logs with quantities.
*
* @param array $log
* The log definition.
* @param array $quantities
* An array of quantity definitions.
*
* @return \Drupal\log\Entity\Log
* The log that was created.
*/
protected function createLogWithQuantities(array $log, array $quantities) {
// Create each quantity.
$quantity_entities = [];
foreach ($quantities as $definition) {
$quantity = Quantity::create($definition);
$quantity->save();
$quantity_entities[] = $quantity;
}
// Create the log and reference the quantities.
$log_entity = Log::create($log);
$log_entity->set('quantity', $quantity_entities);
$log_entity->save();
return $log_entity;
}
}