"InvalidArgumentException: Field amount is unknown." when accessing "/admin/commerce/reports/orders"

Created on 3 February 2025, 2 months ago

Problem/Motivation

Currently, when accessing "/admin/commerce/reports/orders", we get the following error:

InvalidArgumentException: Field amount is unknown.

This happens because, there is no "amount" field on the "OrderReport" entity.

Steps to reproduce

Proposed resolution

Remove the "amount" key from the "ReportsListBuilder".

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇩🇪Germany Grevil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Grevil
  • 🇮🇱Israel jsacksick

    This is an amount field on the OrderReport?

        $fields['amount'] = BundleFieldDefinition::create('commerce_price')
          ->setLabel(t('Total Amount'))
          ->setDescription(t('The total amount of the order'))
          ->setCardinality(1)
          ->setRequired(TRUE)
          ->setDisplayConfigurable('view', TRUE);
  • 🇮🇱Israel jsacksick

    Tried installing commerce_reports on a local test Commerce site and I'm not experiencing this issue.

  • 🇩🇪Germany Grevil

    Here is the stack trace:

    The website encountered an unexpected error. Try again later.
    
    InvalidArgumentException: Field amount is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 616 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).
    Drupal\Core\Entity\ContentEntityBase->get() (Line: 32)
    Drupal\commerce_reports\ReportsListBuilder->buildRow() (Line: 272)
    Drupal\Core\Entity\EntityListBuilder->render() (Line: 23)
    Drupal\Core\Entity\Controller\EntityListController->listing()
    call_user_func_array() (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 638)
    Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)
    Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 50)
    Drupal\ban\BanMiddleware->handle() (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 49)
    Drupal\remove_http_headers\StackMiddleware\RemoveHttpHeadersMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)
    Drupal\Core\DrupalKernel->handle() (Line: 19)
    

    I'll see if I get the issue on a new installation.

  • 🇩🇪Germany Grevil

    Can't reproduce this issue on a fresh installation! @jsacksick mentioned on Slack, that it might be related to an installation error and the entity bundle fields didn't install on installation.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks, I'll try reinstalling the module! Weird...

  • 🇩🇪Germany Anybody Porta Westfalica

    Crazy I just cleanly uninstalled the module and reinstalled it and still the same issue! And yes, I cleared caches ;)

    InvalidArgumentException: Field amount is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 616 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).

    Drupal\Core\Entity\ContentEntityBase->get() (Line: 32)
    Drupal\commerce_reports\ReportsListBuilder->buildRow() (Line: 272)
    Drupal\Core\Entity\EntityListBuilder->render() (Line: 23)
    Drupal\Core\Entity\Controller\EntityListController->listing()
    call_user_func_array() (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 638)
    Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)
    Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 50)
    Drupal\ban\BanMiddleware->handle() (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 49)
    Drupal\remove_http_headers\StackMiddleware\RemoveHttpHeadersMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)
    Drupal\Core\DrupalKernel->handle() (Line: 19)

    I'll investigate further!

  • 🇮🇱Israel jsacksick

    I'm guessing an error occurs while installing the module preventing the installation of entity bundle plugins defined by the module.

    See entity_modules_installed(). You might need to run this logic yourself for the bundles to install properly... This shouldn't be needed but you can try.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks @jsacksick. Module uninstalled and installed without any errors. Log says:

    system 	05.02.2025 - 09:15 	commerce_reports module installed. 
    system 	05.02.2025 - 09:14 	commerce_reports module uninstalled.

    So still strange, I'll take a deeper look.

  • 🇩🇪Germany Anybody Porta Westfalica

    @jsacksick just had a look at the entity in devel and the field exists!
    Maybe the issue is related to translation?

    Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (

  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica

    The database table is also present and filled... I'll dig into core backtrace now.

  • 🇩🇪Germany Grevil

    @anybody, note that this is NOT reproducible on a fresh Drupal installation, so I am unsure if this noise is needed here?

    I'd say we comment again, if it is somehow related to "commerce_reports" (but I doubt it is).

  • 🇩🇪Germany Anybody Porta Westfalica

    @jsacksick I think I'm closer now. Don't know how we ever came to this path:

    But the lines you're referring to in #2 are not the related ones, instead it's these:

      public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
        $fields = parent::baseFieldDefinitions($entity_type);
    
        $fields['order_id'] = BaseFieldDefinition::create('entity_reference')
          ->setLabel(t('Order'))
          ->setDescription(t('The parent order.'))
          ->setSetting('target_type', 'commerce_order')
          ->setReadOnly(TRUE);
        $fields['created'] = BaseFieldDefinition::create('created')
          ->setLabel(t('Created'))
          ->setDescription(t('The time when the order report was created.'))
          ->setReadOnly(TRUE);
        $fields['updated'] = BaseFieldDefinition::create('timestamp')
          ->setLabel(t('Updated'))
          ->setDescription(t('The time when the order report was updated.'))
          ->setReadOnly(TRUE);
    
        return $fields;
      }
    

    And the URL I mentioned is defined here:

     *   links = {
     *     "collection" = "/admin/commerce/reports/orders",
    

    https://git.drupalcode.org/project/commerce_reports/-/blob/8.x-1.x/src/E...

    So the error is correct, there's no amount field defined for the OrderReport Entity type.

    All reports linked in the menus are working fine, but the collection URL from the OrderReport Entity is broken, which makes sense to me code-wise.

    Just FYI. I'll go deeper in a vanilla installation with @Grevil now.

  • 🇮🇱Israel jsacksick

    Pretty simple fix, try the patch attached.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanky @jsacksick that's it! Digging deeper I found we simply have order entities without an amount (which seems to be okay) and that causes the issue!

    Your patch fixes the issue, but leads to wrong columns being populated. So instead of leaving out the column we should make it empty.

    Do you still have this open in your IDE? Otherwise I'll create a MR and change it accordingly.

  • 🇮🇱Israel jsacksick

    I thought you'd be able to debug this yourself :p.
    We're supposed to set an empty string when the amount is empty.

    $row['amount']['data'] = '';

  • 🇩🇪Germany Anybody Porta Westfalica

    @jsacksick Yeah that was not my point, before I at least thought it's a schema issue.

    However, my last point was asking if you still have the file open and can change the patch / MR easily.

    Thank you very much for your effort here!

  • 🇩🇪Germany Anybody Porta Westfalica

    Oh damn my fault... Sorry I made the quickfix and didn't see that line -.-
    Shame on me.

    RTBC!

  • 🇩🇪Germany Anybody Porta Westfalica
Production build 0.71.5 2024