I am hoping to get the field name inside hook_preprocess_file_link().

I only want to alter the link of the file for certain field names.

hook_preprocess_field() would probably work, but I would prefer to use hook_preprocess_file_link().

Is it possible to get the field name in this hook?


function mymodule_preprocess_file_link(&$variables, $fieldName) {
  // This returns "file_link" instead of the field name like "field_myfilefield".

function mymodule_preprocess_field(&$variables) {
  if (isset($variables['field_name'])) {
    $fieldName = $variables['field_name'];

  mymodule_preprocess_file_link($variables, $fieldName);

1 Answer 1


You need to pass the field name to the file link through hook_preprocess_field()
A similar approach is used in Themable Forms module.
Check its module file.

  • Is that possible between two preprocess functions? Jul 2, 2019 at 3:17
  • I tried it with form and form elements. Did you try to implement it? Jul 2, 2019 at 5:18
  • Updated my question with some more code I've tried. Jul 4, 2019 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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