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

Move transplant_days field to farm_transplant module #795

Merged
merged 2 commits into from
Apr 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Do not trim whitespace from quantity field item content #820](https://github.com/farmOS/farmOS/pull/820)
- [Do not install base modules when --existing-config is used #821](https://github.com/farmOS/farmOS/pull/821)
- [Set the minimum value of maturity_days and transplant_days to 1 #794](https://github.com/farmOS/farmOS/pull/794)
- [Move transplant_days field to farm_transplant module #795](https://github.com/farmOS/farmOS/pull/795)

## [3.1.2] 2024-02-26

Expand Down
2 changes: 1 addition & 1 deletion docs/model/type/term.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ above, some types add additional type-specific fields. These include:
Terms in the "Plant type" vocabulary have the following additional attributes:

- Days to maturity (Integer)
- Days to transplant (Integer)
- Days to transplant (Integer) (Added by the optional Transplanting module)
- Days of harvest (Integer)

And the following additional relationships:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
- taxonomy.vocabulary.plant_type
enforced:
module:
- farm_plant_type
- farm_transplanting
id: taxonomy_term.plant_type.transplant_days
field_name: transplant_days
entity_type: taxonomy_term
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ status: true
dependencies:
enforced:
module:
- farm_plant_type
- farm_transplanting
module:
- taxonomy
id: taxonomy_term.transplant_days
Expand Down
1 change: 1 addition & 0 deletions modules/log/transplanting/farm_transplanting.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ core_version_requirement: ^10
dependencies:
- farm:farm_entity
- farm:farm_plant
- farm:farm_plant_type
- log:log
43 changes: 43 additions & 0 deletions modules/log/transplanting/farm_transplanting.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* @file
* Contains farm_transplanting.module.
*/

use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;

/**
* Implements hook_entity_form_display_alter().
*/
function farm_transplanting_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) {
if ($context['entity_type'] == 'taxonomy_term' && $context['bundle'] == 'plant_type' && $form_display->getMode() == 'default' && $form_display->isNew()) {
$form_display->setComponent('transplant_days', [
'type' => 'number',
'settings' => [
'placeholder' => '',
],
'region' => 'content',
'weight' => 15,
]);
}
}

/**
* Implements hook_entity_view_display_alter().
*/
function farm_transplanting_entity_view_display_alter(EntityViewDisplayInterface $display, array $context) {
if ($context['entity_type'] == 'taxonomy_term' && $context['bundle'] == 'plant_type' && $display->getMode() == 'full' && $display->isNew()) {
$display->setComponent('transplant_days', [
'type' => 'number_integer',
'label' => 'inline',
'settings' => [
'thousand_separator' => '',
'prefix_suffix' => TRUE,
],
'region' => 'content',
'weight' => 15,
]);
}
}
3 changes: 3 additions & 0 deletions modules/quick/planting/tests/src/Kernel/QuickPlantingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class QuickPlantingTest extends QuickFormTestBase {
* {@inheritdoc}
*/
protected static $modules = [
'entity_reference_validators',
'farm_harvest',
'farm_land',
'farm_plant',
Expand All @@ -34,6 +35,7 @@ class QuickPlantingTest extends QuickFormTestBase {
'farm_seeding',
'farm_transplanting',
'farm_unit',
'field',
];

/**
Expand All @@ -45,6 +47,7 @@ protected function setUp(): void {
'farm_harvest',
'farm_land',
'farm_plant',
'farm_plant_type',
'farm_quantity_standard',
'farm_seeding',
'farm_transplanting',
Expand Down
18 changes: 0 additions & 18 deletions modules/taxonomy/plant_type/farm_plant_type.module
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ function farm_plant_type_entity_form_display_alter(EntityFormDisplayInterface $f
'region' => 'content',
'weight' => 20,
]);
$form_display->setComponent('transplant_days', [
'type' => 'number',
'settings' => [
'placeholder' => '',
],
'region' => 'content',
'weight' => 15,
]);
$form_display->setComponent('harvest_days', [
'type' => 'number',
'settings' => [
Expand Down Expand Up @@ -86,16 +78,6 @@ function farm_plant_type_entity_view_display_alter(EntityViewDisplayInterface $d
'region' => 'content',
'weight' => 20,
]);
$display->setComponent('transplant_days', [
'type' => 'number_integer',
'label' => 'inline',
'settings' => [
'thousand_separator' => '',
'prefix_suffix' => TRUE,
],
'region' => 'content',
'weight' => 15,
]);
$display->setComponent('harvest_days', [
'type' => 'number_integer',
'label' => 'inline',
Expand Down
34 changes: 34 additions & 0 deletions modules/taxonomy/plant_type/farm_plant_type.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,37 @@ function farm_plant_type_post_update_add_harvest_days(&$sandbox) {
]);
$field->save();
}

/**
* Move transplant_days field to farm_transplant module.
*/
function farm_plant_type_post_update_move_transplant_days() {

// The transplant_days field was previously part of this module. It has moved
// to the farm_transplanting module, so that it is only made available in
// instances that deal with transplants. If there is transplant_days data in
// the database, but the farm_transplant module is not installed, we should
// install it so that module can be responsible for the data moving forward.
$data_count = \Drupal::database()->query('SELECT COUNT(*) FROM {taxonomy_term__transplant_days}')->fetchField();
if (!empty($data_count)) {
if (!\Drupal::service('module_handler')->moduleExists('farm_transplanting')) {
\Drupal::configFactory()->getEditable('field.field.taxonomy_term.plant_type.transplant_days')->delete();
\Drupal::configFactory()->getEditable('field.storage.taxonomy_term.transplant_days')->delete();
\Drupal::service('module_installer')->install(['farm_transplanting']);
}
}

// Otherwise, we can delete the transplant_days field.
// Using FieldConfig::load() and FieldStorageConfig::load() and their
// associated delete() methods will delete the config and database tables.
else {
$field = FieldConfig::load('taxonomy_term.plant_type.transplant_days');
if (!empty($field)) {
$field->delete();
}
$field_storage = FieldStorageConfig::load('taxonomy_term.transplant_days');
if (!empty($field_storage)) {
$field_storage->delete();
}
}
}