Call to undefined method getEntity()

Created on 11 April 2024, 8 months ago
Updated 2 May 2024, 8 months ago

Problem/Motivation

When I create a view using @views_entity_form_field and want to display it in modal I am receiving following error:

 Error: Call to undefined method Drupal\views\Form\ViewsForm::getEntity() in _field_permissions_field_widget_form_alter() (line 85 of /modules/contrib/field_permissions/field_permissions.module) #0 /modules/contrib/field_permissions/field_permissions.module(53): _field_permissions_field_widget_form_alter() 
<a class="button use-ajax" data-dialog-type="modal" data-dialog-options='{"width":"80%", "height":"750", "fluid":"true", "resizable":"false", "hide":"fadeOut", "show":"fadeIn", &quot;title&quot;:&quot;Bulk_Update&quot;}' href="/milestone-bulk-update/{{ raw_arguments.nid }}&edit?destination=/node/{{ nid }}"> Bulk Update</a>

It all started when I upgraded from 8.x-1.1+4-dev version released in 2021 to a latest one : 8.x-1.3 which means it is not related to @views_entity_form_field module but to this one.

When I rollback to old 8.x-1.1+4-dev everything works as expected and no issue.

Here we can see the difference in code (field_permissions.module)

Current 8.x-1.3 line 85 (starting with function on line 76)

function _field_permissions_field_widget_form_alter(array &$element, FormStateInterface $form_state, array $context): void {
  $account = \Drupal::currentUser();

  $field_definition = $context['items']->getFieldDefinition();
  if ($field_definition instanceof FieldConfig) {
    $permission_type = \Drupal::service('field_permissions.permissions_service')->fieldGetPermissionType($field_definition->getFieldStorageDefinition());
    // Make sure field uses custom access.
    if ($permission_type === FieldPermissionTypeInterface::ACCESS_CUSTOM) {
      $field_name = $field_definition->get('field_name');
      $op = $form_state->getFormObject()->getEntity()->isNew() ? 'create' : 'edit';
      if (($account->hasPermission('create read-only ' . $field_name) && !$account->hasPermission('create ' . $field_name))
        || ($account->hasPermission('edit read-only ' . $field_name) && !$account->hasPermission('edit ' . $field_name))) {
        $element['#disabled'] = TRUE;
      }
    }
  }
}

and working one from old :
The same function was in line 50

function field_permissions_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $account = \Drupal::currentUser();

  $field_definition = $context['items']->getFieldDefinition();
  if ($field_definition instanceof FieldConfig) {
    $permission_type = \Drupal::service('field_permissions.permissions_service')->fieldGetPermissionType($field_definition->getFieldStorageDefinition());
    // Make sure field uses custom access.
    if ($permission_type === FieldPermissionTypeInterface::ACCESS_CUSTOM) {
      $field_name = $field_definition->get('field_name');
      $op = $context['items']->getEntity()->isNew() ? 'create' : 'edit';
      if ($account->hasPermission($op . ' read-only ' . $field_name) && !$account->hasPermission($op . ' ' . $field_name)) {
        $element['#disabled'] = TRUE;
      }
    }
  }
}

Looks like this change created this issue.

Anyone can help?

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΈπŸ‡°Slovakia coaston

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

Comments & Activities

Production build 0.71.5 2024