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,20 @@
langcode: en
status: true
dependencies:
config:
- log.type.lab_test
enforced:
module:
- farm_lab_test
id: log.lab_test.timestamp
field_name: timestamp
entity_type: log
bundle: lab_test
label: 'Date sampled'
description: 'The date when the sample was collected.'
required: true
translatable: true
default_value: { }
default_value_callback: 'Drupal\log\Entity\Log::getRequestTime'
settings: { }
field_type: timestamp
@@ -0,0 +1,8 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_lab_test
id: soil
label: Soil test
@@ -0,0 +1,8 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_lab_test
id: tissue
label: Tissue test
@@ -0,0 +1,8 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_lab_test
id: water
label: Water test
@@ -0,0 +1,18 @@
langcode: en
status: true
dependencies:
module:
- farm_log
- farm_log_quantity
enforced:
module:
- farm_lab_test
third_party_settings:
farm_log_quantity:
default_quantity_type: test
id: lab_test
label: 'Lab test'
description: ''
name_pattern: 'Lab test log [log:id]'
workflow: farm_log_workflow
new_revision: true
@@ -0,0 +1,11 @@
# Schema for lab_test_type config entity.
farm_lab_test.lab_test_type.*:
type: config_entity
label: 'Lab test type'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
@@ -0,0 +1,10 @@
name: Lab test log
description: Adds a Lab test log type.
type: module
package: farmOS Logs
core_version_requirement: ^10
dependencies:
- farm:farm_entity
- farm:farm_lab
- farm:farm_quantity_test
- log:log
@@ -0,0 +1,54 @@
<?php
/**
* @file
* The farmOS lab test log module.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Lab test type options helper.
*
* @return array
* Returns an array of lab test types for use in form select options.
*/
function farm_lab_test_type_options() {
/** @var \Drupal\farm_lab_test\Entity\FarmLabTestTypeInterface[] $lab_test_types */
$lab_test_types = \Drupal::entityTypeManager()->getStorage('lab_test_type')->loadMultiple();
$options = [];
foreach ($lab_test_types as $id => $lab_test_type) {
$options[$id] = $lab_test_type->getLabel();
}
return $options;
}
/**
* Allowed values callback function for the lab test type field.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* The field definition.
* @param \Drupal\Core\Entity\ContentEntityInterface|null $entity
* The entity being created if applicable.
* @param bool $cacheable
* Boolean indicating if the allowed values can be cached. Defaults to TRUE.
*
* @return array
* Returns an array of allowed values for use in form select options.
*/
function farm_lab_test_type_field_allowed_values(FieldStorageDefinitionInterface $definition, ?ContentEntityInterface $entity = NULL, bool &$cacheable = TRUE) {
return farm_lab_test_type_options();
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function farm_lab_test_form_log_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only show the "Soil texture" field if the lab test type is "soil".
if (isset($form['lab_test_type']) && isset($form['soil_texture'])) {
$form['soil_texture']['#states']['visible'] = [':input[name="lab_test_type"]' => ['value' => 'soil']];
}
}
@@ -0,0 +1,211 @@
<?php
/**
* @file
* Post update hooks for the farm_lab_test module.
*/
use Drupal\Core\Utility\UpdateException;
use Drupal\farm_lab_test\Entity\FarmLabTestType;
use Drupal\taxonomy\Entity\Term;
/**
* Add "Date received" and "Date processed" fields to lab test logs.
*/
function farm_lab_test_post_update_add_received_processed_date_fields(&$sandbox) {
// Date received.
$options = [
'type' => 'timestamp',
'label' => t('Date received'),
'description' => t('The date when the sample was received by the lab.'),
'weight' => [
'form' => -35,
'view' => -35,
],
];
$field_definition = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('lab_received_date', 'log', 'farm_lab_test', $field_definition);
// Date processed.
$options = [
'type' => 'timestamp',
'label' => t('Date processed'),
'description' => t('The date when the sample was processed by the lab.'),
'weight' => [
'form' => -34,
'view' => -34,
],
];
$field_definition = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('lab_processed_date', 'log', 'farm_lab_test', $field_definition);
}
/**
* Install the lab taxonomy module.
*/
function farm_lab_test_post_update_enable_farm_lab(&$sandbox) {
if (!\Drupal::service('module_handler')->moduleExists('farm_lab')) {
\Drupal::service('module_installer')->install(['farm_lab']);
}
}
/**
* Migrate laboratory names to taxonomy terms.
*/
function farm_lab_test_post_update_migrate_lab_terms(&$sandbox) {
// This function will be run as a batch operation to save the new term
// reference values on existing logs. On the first run, we will make
// preparations. This logic should only run once.
if (!isset($sandbox['current_log'])) {
// Query the database for all lab field data on logs.
// Save it to $sandbox for future reference.
$sandbox['log_map'] = \Drupal::database()->query('SELECT entity_id, lab_value FROM {log__lab} WHERE deleted = 0')->fetchCol();
// Create taxonomy terms for each of the labs.
// Add them to a term map in $sandbox for future reference.
$unique_labs = array_unique($sandbox['log_map']);
$sandbox['term_map'] = [];
foreach ($unique_labs as $lab_name) {
$term = Term::create(['vid' => 'lab', 'name' => $lab_name]);
$term->save();
$sandbox['term_map'][$lab_name] = $term->id();
}
// Get the Drupal entity definition update manager.
$update_manager = \Drupal::entityDefinitionUpdateManager();
// Delete the old lab field.
$storage_definition = $update_manager->getFieldStorageDefinition('lab', 'log');
$update_manager->uninstallFieldStorageDefinition($storage_definition);
// Install the new lab field.
$options = [
'type' => 'entity_reference',
'label' => t('Laboratory'),
'description' => t('What laboratory performed this test?'),
'target_type' => 'taxonomy_term',
'target_bundle' => 'lab',
'auto_create' => TRUE,
'weight' => [
'form' => -40,
'view' => -40,
],
];
$field_definition = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options);
$update_manager->installFieldStorageDefinition('lab', 'log', 'farm_lab_test', $field_definition);
// If there are no lab test logs with lab field values, bail.
if (empty($sandbox['log_map'])) {
return NULL;
}
// Track progress.
$sandbox['current_log'] = 0;
$sandbox['#finished'] = 0;
}
// Iterate over logs, 10 at a time.
$log_ids = array_keys($sandbox['log_map']);
$log_count = count($log_ids);
$end_log = $sandbox['current_log'] + 10;
$end_log = $end_log > $log_count ? $log_count : $end_log;
for ($i = $sandbox['current_log']; $i < $end_log; $i++) {
// Iterate the global counter.
$sandbox['current_log']++;
// Get the log ID.
$id = $log_ids[$i];
// If there is no taxonomy term to assign, skip.
if (empty($sandbox['log_map'][$id]) || empty($sandbox['term_map'][$sandbox['log_map'][$id]])) {
continue;
}
// Load the log.
$log = \Drupal::service('entity_type.manager')->getStorage('log')->load($id);
// If the log didn't load, throw an update exception.
if (empty($log)) {
throw new UpdateException('Could not load log. ID: @id', ['@id' => $id]);
}
// Assign the new lab taxonomy term.
$log->set('lab', $sandbox['term_map'][$sandbox['log_map'][$id]]);
// Save a new revision of the log.
$log->setNewRevision(TRUE);
$log->setRevisionLogMessage(t('Automatically migrated laboratory name to taxonomy term in the new Lab vocabulary.'));
$log->save();
// Declare that the log has been fixed.
\Drupal::logger('farm_lab_test')->notice(t('Log @id lab has been migrated.', ['@id' => $id]));
}
// Update progress.
$sandbox['#finished'] = $sandbox['current_log'] / count($sandbox['log_map']);
return NULL;
}
/**
* Install the test quantity module.
*/
function farm_lab_test_post_update_enable_farm_quantity_test(&$sandbox) {
if (!\Drupal::service('module_handler')->moduleExists('farm_quantity_test')) {
\Drupal::service('module_installer')->install(['farm_quantity_test']);
}
}
/**
* Add tissue lab test type.
*/
function farm_lab_test_post_update_add_tissue_type(&$sandbox) {
$type = FarmLabTestType::create([
'id' => 'tissue',
'label' => 'Tissue test',
'dependencies' => [
'enforced' => [
'module' => [
'farm_lab_test',
],
],
],
]);
$type->save();
}
/**
* Override the lab_test log timestamp label and description.
*/
function farm_lab_test_post_update_override_lab_test_timestamp_label_description(&$sandbox) {
// Override the timestamp field label and description on lab_test logs to
// make it clear that it should be used for the date the sample was taken.
// This also checks to make sure they haven't been overridden already first.
/** @var \Drupal\Core\Field\Entity\BaseFieldOverride $config */
$config = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('log')['timestamp']->getConfig('lab_test');
if ($config->isNew()) {
$config->set('label', 'Date sampled');
$config->set('description', 'The date when the sample was collected.');
$config->save();
}
}
/**
* Add "Soil texture" field to lab test logs.
*/
function farm_lab_test_post_update_soil_texture(&$sandbox) {
$options = [
'type' => 'string',
'label' => t('Soil texture'),
'weight' => [
'form' => -45,
'view' => -45,
],
];
$field_definition = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('soil_texture', 'log', 'farm_lab_test', $field_definition);
}
@@ -0,0 +1,51 @@
<?php
namespace Drupal\farm_lab_test\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
/**
* Defines the FarmLabTestType entity.
*
* @ConfigEntityType(
* id = "lab_test_type",
* label = @Translation("Lab test type"),
* label_collection = @Translation("Lab test type"),
* handlers = { },
* admin_permission = "administer site configuration",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* },
* config_export = {
* "id",
* "label",
* },
* )
*
* @ingroup farm
*/
class FarmLabTestType extends ConfigEntityBase implements FarmLabTestTypeInterface {
/**
* The lab test type ID.
*
* @var string
*/
protected $id;
/**
* The lab test type label.
*
* @var string
*/
protected $label;
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->label;
}
}
@@ -0,0 +1,22 @@
<?php
namespace Drupal\farm_lab_test\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface for defining FarmLabTestType config entities.
*
* @ingroup farm
*/
interface FarmLabTestTypeInterface extends ConfigEntityInterface {
/**
* Returns the lab test type label.
*
* @return string
* The lab test type label.
*/
public function getLabel();
}
@@ -0,0 +1,88 @@
<?php
namespace Drupal\farm_lab_test\Plugin\Log\LogType;
use Drupal\farm_entity\Plugin\Log\LogType\FarmLogType;
/**
* Provides the lab test log type.
*
* @LogType(
* id = "lab_test",
* label = @Translation("LabTest"),
* )
*/
class LabTest extends FarmLogType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
// Date received.
$options = [
'type' => 'timestamp',
'label' => $this->t('Date received'),
'description' => $this->t('The date when the sample was received by the lab.'),
'weight' => [
'form' => -70,
'view' => -70,
],
];
$fields['lab_received_date'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Date processed.
$options = [
'type' => 'timestamp',
'label' => $this->t('Date processed'),
'description' => $this->t('The date when the sample was processed by the lab.'),
'weight' => [
'form' => -60,
'view' => -60,
],
];
$fields['lab_processed_date'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Lab test type.
$options = [
'type' => 'list_string',
'label' => $this->t('Test type'),
'allowed_values_function' => 'farm_lab_test_type_field_allowed_values',
'weight' => [
'form' => -50,
'view' => -50,
],
];
$fields['lab_test_type'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Soil texture.
$options = [
'type' => 'string',
'label' => $this->t('Soil texture'),
'weight' => [
'form' => -45,
'view' => -45,
],
];
$fields['soil_texture'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Lab.
$options = [
'type' => 'entity_reference',
'label' => $this->t('Laboratory'),
'description' => $this->t('What laboratory performed this test?'),
'target_type' => 'taxonomy_term',
'target_bundle' => 'lab',
'auto_create' => TRUE,
'weight' => [
'form' => -40,
'view' => -40,
],
];
$fields['lab'] = $this->farmFieldFactory->bundleFieldDefinition($options);
return $fields;
}
}