-3

So i upgraded PHP7.0 to PHP7.4 due to a requirement from a module. Now when enabling/disabling modules, clearing cache, importing config i get the memory exhausted error. After some debugging with a new installation i found out that one of my custom modules with hook_entity_type_alter causes this errors. In this hook i'm trying to add my custon viewmodes depending on the role for the profile edit form. Using the following piece of code:

function custom_module_entity_type_alter(array &$entity_types) {
  $current_user = \Drupal::currentUser();

  $roles = $current_user->getRoles();

  $node_type = $entity_types['user'];
  $default_handler_class = $node_type->getHandlerClasses()['form']['default'];
  $node_type->setFormClass('profile_edit',  $default_handler_class);
  if(in_array('organization', $roles)) {
    $node_type->setFormClass('organization_edit',  $default_handler_class);
  } else {
    $node_type->setFormClass('profile_edit',  $default_handler_class);
  }
}

I had to remove all code in this hook to make the site work again, even:

function custom_module_blocks_entity_type_alter(array &$entity_types) {
  $current_user = \Drupal::currentUser();
  kint($current_user);
}

Gives me this error, without kint it works. But any more code in this hook will give me the memory error. I already tried putting memory_limit on 500M and even -1 where my PC completely froze due to this memory leek.

2
  • What is the question? Has this been definitively proven to be a memory leak? – Kevin Apr 23 '20 at 18:28
  • Not sure, but this piece of code apparently used lots of memory with PHP7.2 or higher – Skywalker Apr 24 '20 at 13:35
0

I fixed it by using a different hook

function custom_module_entity_form_display_alter(&$form_display, $context) {
  if($context['entity_type'] == 'user' && $context['bundle'] == 'user'){
    $user = \Drupal::currentUser();
    if(in_array('organization', $user->getRoles())){
      $storage = \Drupal::service('entity_type.manager')->getStorage('entity_form_display');
      $form_display = $storage->load('user.user.organization_edit');
    } else {
      $storage = \Drupal::service('entity_type.manager')->getStorage('entity_form_display');
      $form_display = $storage->load('user.user.profile_edit');
    }
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.