TypeError: Drupal\field_permissions\FieldPermissionsService::fieldGetPermissionType(): Argument #1 ($field) must be of type Drupal\field\FieldStorageConfigInterface

Created on 10 July 2023, 12 months ago
Updated 13 May 2024, about 1 month ago

Problem/Motivation

WSOD, making Drupal site unusable.

Steps to reproduce

Update to latest -dev

Proposed resolution

Back to stable version to stable version, and the site is online again.

Remaining tasks

Haven't investigate what is the problem, maybe will try to do it later.

For now, here is the complete error and top10 stack:

TypeError: Drupal\field_permissions\FieldPermissionsService::fieldGetPermissionType(): Argument #1 ($field) must be of type Drupal\field\FieldStorageConfigInterface, Drupal\Core\Field\BaseFieldDefinition given, called in /(...)/modules/contrib/field_permissions/src/FieldPermissionsService.php on line 163 na Drupal\field_permissions\FieldPermissionsService->fieldGetPermissionType() (linha 138 de /(...)/modules/contrib/field_permissions/src/FieldPermissionsService.php)
#0 /(...)/modules/contrib/field_permissions/src/FieldPermissionsService.php(163): Drupal\field_permissions\FieldPermissionsService->fieldGetPermissionType()
#1 /(...)/modules/contrib/field_permissions/field_permissions.module(39): Drupal\field_permissions\FieldPermissionsService->getFieldAccess()
#2 /(...)/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php(350): field_permissions_entity_field_access()
#3 /(...)/core/lib/Drupal/Core/Extension/ModuleHandler.php(405): Drupal\Core\Entity\EntityAccessControlHandler->Drupal\Core\Entity\{closure}()
#4 /(...)/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php(351): Drupal\Core\Extension\ModuleHandler->invokeAllWith()
#5 /var/www/clients/client1/web1/web/repos/igot/web/core/lib/Drupal/Core/Field/FieldItemList.php(154): Drupal\Core\Entity\EntityAccessControlHandler->fieldAccess()
#6 /(...)/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php(253): Drupal\Core\Field\FieldItemList->access()
#7 /(...)/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php(266): Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple()
#8 /(...)/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(340): Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple()
#9 /(...)/core/modules/node/src/NodeViewBuilder.php(24): Drupal\Core\Entity\EntityViewBuilder->buildComponents()
#10 /(...)/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(282): Drupal\node\NodeViewBuilder->buildComponents()
🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇵🇹Portugal jrochate

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

Comments & Activities

  • Issue created by @jrochate
  • 🇮🇳India ayush.pandey

    I have explored the issue, but the issue seems not to be reproducible on the field_permission module version 8.x-1.x-dev and PHP version 8.1.
    Please add proper steps to reproduce incase it is reproducible at your end. jrochate

  • 🇺🇸United States mariacha1

    Agreed, I'm going to assume you've got a patch applied that's conflicting with the latest push to 8.x-1.x. I had this trouble related to a patch in https://www.drupal.org/node/2881776 . I'll update that issue with an updated patch.

  • Status changed to Needs review 12 months ago
  • 🇺🇸United States mariacha1

    Patch over at https://www.drupal.org/files/issues/2023-07-10/2881776-39.patch should fix it. If you're using the previous patch (32), you should be able to roll that one instead.

  • 🇵🇹Portugal jrochate

    Sorry, it didn't work.

    We will try to find where is this coming from, and will keep you posted. thanks Maria

  • 🇵🇹Portugal lolgm

    It seems to me that the problem is being caused by the change implemented in issue 🐛 Invalid context for call to FieldDefinitionInterface->isDisplayConfigurable() Fixed .

    function field_permissions_entity_field_access($operation, FieldDefinitionInterface $field_definition, $account, FieldItemListInterface $items = NULL) {
      $context = ($operation == 'view') ? 'view' : 'edit';
      if (!$field_definition->isDisplayConfigurable($context) || empty($items)) {
        return AccessResult::neutral();
      }
      $access_field = \Drupal::service('field_permissions.permissions_service')->getFieldAccess($operation, $items, $account, $field_definition);
      ...
    }
    

    This alteration causes the unexpected behavior of allowing the entity's base fields to be validated by the getFieldAccess function, however this is not prepared to receive this type of fields, hence the error reported above.
    My suggestion is to check if it is a base field of the entity. If it is, we should not process it with the module's permissions service.
    Example below:

    function field_permissions_entity_field_access($operation, FieldDefinitionInterface $field_definition, $account, FieldItemListInterface $items = NULL) {
      $context = ($operation == 'view') ? 'view' : 'edit';
      if (!$field_definition->isDisplayConfigurable($context) || empty($items) || $field_definition->getFieldStorageDefinition()->isBaseField()) {
        return AccessResult::neutral();
      }
      $access_field = \Drupal::service('field_permissions.permissions_service')->getFieldAccess($operation, $items, $account, $field_definition);
      ...
    }
    

    Or maybe the solution should be implemented at the root of the problem?
    Drupal\field_permissions\FieldPermissionsService::fieldGetPermissionType()

    I would like to know your opinions about this.

  • 🇺🇸United States mariacha1

    Ahh, good catch. More the fool me for reviewing on a site with my own bunch of patches applied.

    I think this is a fine solution for the problem for now. I'd like base fields to be useable in the future, but that's a different problem.

    The most fool-proof solution is to just check to see if $field_definition implements \Drupal\Core\Field\FieldConfigInterface, since that's what fieldGetPermissionType expects.

    So this:

      if (!$field_definition->isDisplayConfigurable($context) || empty($items) || !is_a($field_definition, '\Drupal\Core\Field\FieldConfigInterface')) {
        return AccessResult::neutral();
      }
    

    Just on the off chance there's a non-base-field that is also not a config field (this is something a module like https://www.drupal.org/project/entity may define for BundleFieldDefinition)

    Ultimately we'll fix that difference so that fieldGetPermissionType handles non-FieldConfigInterface field definitions better, but there are other issues open for that, and this solution should fix the immediate need.

    I'll send up a patch for this in a bit.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 12 months ago
    48 pass
  • 🇺🇸United States mariacha1

    Here's that patch. Let me know if it solves the issue!

  • 🇺🇸United States jhedstrom Portland, OR

    I think this makes sense to commit now if it fixes the issue, since this is critical. It is a bit disconcerting though that neither the issue here, or the change over in 🐛 Invalid context for call to FieldDefinitionInterface->isDisplayConfigurable() Fixed were caught by tests. I'd recommend we do a follow-up to add test coverage for this so we don't regress it again in the future.

  • 🇺🇸United States mariacha1

    100% -- I don't even think it'd be too hard to write that test. I'll merge this the second someone who's experiencing this gets me a thumbs up, since I don't have a clean install running locally just now.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 12 months ago
    48 pass
  • 🇵🇹Portugal lolgm

    Thanks a lot for the help.
    Unfortunately the issue #8 patch didn't work.
    It seems to me that even with that condition it is not working for BaseFieldDefinition and BaseFieldOverride fields.
    Perhaps the best solution is to directly check the type of FieldStorageDefinition, this way we guarantee that we filter only the expected fields.
    Example below:

    function field_permissions_entity_field_access($operation, FieldDefinitionInterface $field_definition, $account, FieldItemListInterface $items = NULL) {
      $context = ($operation == 'view') ? 'view' : 'edit';
      if (!$field_definition->isDisplayConfigurable($context) || empty($items) || !is_a($field_definition->getFieldStorageDefinition(), '\Drupal\field\FieldStorageConfigInterface')) {
        return AccessResult::neutral();
      }
      $access_field = \Drupal::service('field_permissions.permissions_service')->getFieldAccess($operation, $items, $account, $field_definition);
      ...
    }
    
  • Status changed to RTBC 12 months ago
  • 🇺🇸United States mariacha1

    Aha, yep, this patch both fixes the problem and is more correct than mine. Nice! I tested on a clean branch this morning, and will merge shortly. Thanks for the teamwork everyone!

    • lolgm authored 1e6ad554 on 8.x-1.x
      Issue #3373500 by mariacha1, lolgm: TypeError: Drupal\field_permissions\...
    • 35ef7b33 committed on 8.x-1.x
      Issue #3373500: TypeError: Drupal\field_permissions\...
  • Status changed to Fixed 12 months ago
  • 🇺🇸United States mariacha1

    Patch is applied to the latest 8.x-1.x branch, and I've opened a followup ticket at https://www.drupal.org/project/field_permissions/issues/3374151 📌 Tests: field_permissions_entity_field_access field_definition is a base field Needs work to test the hook for non-FieldConfig elements in the future.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • 🇺🇸United States stephenplatz

    I'm seeing something similar in

    TypeError: Drupal\field_permissions\FieldPermissionsService::fieldGetPermissionType(): Argument #1 ($field) must be of type Drupal\field\FieldStorageConfigInterface, Drupal\Core\Field\BaseFieldDefinition given, called in /app/web/modules/contrib/field_permissions/src/FieldPermissionsService.php on line 198 in Drupal\field_permissions\FieldPermissionsService->fieldGetPermissionType() (line 138 of /app/web/modules/contrib/field_permissions/src/FieldPermissionsService.php).
    

    using 8.x-1.3 and Drupal 10.2.4.

  • Status changed to Needs work about 2 months ago
  • 🇺🇸United States mariacha1

    @stephenplatz I'm re-opening with "Needs work" so you can submit a patch or merge request here -- I think your proposal to add !is_a($field_definition->getFieldStorageDefinition(), '\Drupal\field\FieldStorageConfigInterface') to the`field_permissions_jsonapi_entity_field_filter_access` is great.

  • 🇺🇸United States stephenplatz

    @mariacha1 I went ahead and created a child issue for this here https://www.drupal.org/project/field_permissions/issues/3443837 🐛 field_permissions_jsonapi_entity_field_filter_access needs to check for instance of \Drupal\field\FieldStorageConfigInterface Active , I thought that may make sense, MR also there.

  • Status changed to Fixed about 2 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024