-1

I am trying to modify a file field value and add a download svg icon and then add it to a twig file by using a preprocess field.

function myfi_preprocess_field(&$variables) {
  if ($variables['element']['#field_name'] == 'field_download') {
    print_r($variables);
  }
}

I am not sure what I am doing wrong here but the variable is not loading and it is timing out (memory exhaustion)

  • Objects in Drupal 8 often are massive, with rescursion, and can end up with the issue you are seeing. Dumping objects often does not work so well. You may want to look at the Devel module's kint(), as I think it may have come up with ways to work around this. – Jaypan Feb 6 at 2:18
  • @Jaypan thanks for your quick answer. I have enabled it but still facing the same issue, is there a way to see what elements are present within variables without facing this issue – Regokonda Feb 6 at 2:48
  • You need to use a real debugger. Kint/Devel + the browser simply cannot handle this. – Kevin Feb 6 at 2:55
  • @Kevin I have tried that and I am getting an error saying exception has occurred. This is the error : Symfony\Component\Routing\Exception\RouteNotFoundException: Route "entity.node.create" does not exist. on 201 of routeProvider.php in core/Drupal/Routing/RouteProvider.php – Regokonda Feb 6 at 5:41
  • 2
    @Regokonda debuggers don’t produce errors like that, they just help you to debug the code. You’ll need to learn about and use XDebug or similar; as others have mentioned dumping vars in the browser isn’t going to work in a lot of cases – Clive Feb 6 at 7:58
0

Use Devel with vardumper and dump only the $element instead of the whole $variables something like:

function myfi_preprocess_field(&$variables) {
  $element = $variables['element'];
  if ($element['#field_name'] == 'field_download') {
    // Dump only the element.
    dpm($element);
  }
}
0

If you have installed devel module, you can enable kint module. In your code you can use the kint() function to print the variable...

Example:

function myfi_preprocess_field(&$variables) {
    if ($variables['element']['#field_name'] == 'field_download') {
        kint($variables);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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