mstenta / farm_crop_plan

Crop plan type and related features for farmOS. (ALPHA 3.x IN DEVELOPMENT)
https://drupal.org/project/farm_crop_plan
GNU General Public License v2.0
5 stars 1 forks source link

Exception thrown on farmOS 3.1 due to harvest_days field not being available on plant types #33

Closed wotnak closed 4 months ago

wotnak commented 4 months ago

CropPlanAddPlantingForm uses harvest_days field value from plant type to populate plan record harvest days value. But the harvest_days field is not yet available on plant types in farmOS release. https://github.com/farmOS/farmOS/pull/794

This causes an exception to be thrown when trying to use the module on farmOS 3.1.x:

InvalidArgumentException: Field harvest_days is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 616 of /opt/farmos/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php). ``` #0 /opt/farmos/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php(597): Drupal\Core\Entity\ContentEntityBase->getTranslatedField() #1 /opt/farmos/web/modules/farm_crop_plan/src/Form/CropPlanAddPlantingForm.php(288): Drupal\Core\Entity\ContentEntityBase->get() #2 /opt/farmos/web/modules/farm_crop_plan/src/Form/CropPlanAddPlantingForm.php(153): Drupal\farm_crop_plan\Form\CropPlanAddPlantingForm->plantingDefaultValues() #3 [internal function]: Drupal\farm_crop_plan\Form\CropPlanAddPlantingForm->buildForm() #4 /opt/farmos/web/core/lib/Drupal/Core/Form/FormBuilder.php(536): call_user_func_array() #5 /opt/farmos/web/core/lib/Drupal/Core/Form/FormBuilder.php(375): Drupal\Core\Form\FormBuilder->retrieveForm() #6 /opt/farmos/web/core/lib/Drupal/Core/Form/FormBuilder.php(633): Drupal\Core\Form\FormBuilder->rebuildForm() #7 /opt/farmos/web/core/lib/Drupal/Core/Form/FormBuilder.php(325): Drupal\Core\Form\FormBuilder->processForm() #8 /opt/farmos/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm() #9 [internal function]: Drupal\Core\Controller\FormController->getContentResult() #10 /opt/farmos/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array() #11 /opt/farmos/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #12 /opt/farmos/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext() #13 /opt/farmos/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() #14 /opt/farmos/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #15 /opt/farmos/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw() #16 /opt/farmos/web/modules/simple_oauth/src/HttpMiddleware/BasicAuthSwap.php(54): Symfony\Component\HttpKernel\HttpKernel->handle() #17 /opt/farmos/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle() #18 /opt/farmos/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle() #19 /opt/farmos/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle() #20 /opt/farmos/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\Core\StackMiddleware\ContentLength->handle() #21 /opt/farmos/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() #22 /opt/farmos/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() #23 /opt/farmos/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle() #24 /opt/farmos/web/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle() #25 /opt/farmos/web/index.php(19): Drupal\Core\DrupalKernel->handle() #26 {main} ```

A possible fix could be to check if the plan type has harvest_days field before using it:

diff --git a/src/Form/CropPlanAddPlantingForm.php b/src/Form/CropPlanAddPlantingForm.php
index 16ca11f..8611080 100644
--- a/src/Form/CropPlanAddPlantingForm.php
+++ b/src/Form/CropPlanAddPlantingForm.php
@@ -285,7 +285,7 @@ class CropPlanAddPlantingForm extends FormBase {
       }

       // Populate harvest_days from the plant_type.
-      if ($plant_type->get('harvest_days')->value) {
+      if ($plant_type->hasField('harvest_days') && $plant_type->get('harvest_days')->value) {
         $values['harvest_days'] = $plant_type->get('harvest_days')->value;
       }
     }

This would make the form work on farmOS 3.1 as well as 3.2+ where the field will be available. Alternatively the module could be marked as compatible only with farmOS 3.2+ but I think fixing the compatibility with 3.1 would be better approach.

mstenta commented 4 months ago

Thanks @wotnak! I committed your suggestion (and gave you commit author credit): https://github.com/mstenta/farm_crop_plan/commit/ac63da2141586ebe44cbb1fd473ab3562c63c2a4