I have a process running that renders a PDF to PNGs and attaches those PNGs to a file field. There is one PDF's results per node, but there we have a bunch of PDFs with 10,000+ pages. We've found that this results in the operation of loading an edit form to eat quite a lot of memory (a node with 12,000 attached files ate up all 4GB of RAM on our server before Apache killed the process for taking up too much memory).

Does anyone have any ideas how we might prevent the field from loading? It looks like I could hack file_field_load() to not perform its task given the user is on an edit page and the troubled field is expected but it would be cool if there was a solution that wasn't a hack.

The stack trace when the troubled field is set to not display is:

 1. {main}()  /index.php:0
 2. menu_execute_active_handler()  /index.php:18
 3. call_user_func_array()  /includes/menu.inc:348
 4. node_page_edit()  /includes/menu.inc:0
 5. drupal_get_form()  /modules/node/node.pages.inc:15
 6. drupal_process_form()  /includes/form.inc:121
 7. form_builder()  /includes/form.inc:406
 8. form_builder()  /includes/form.inc:930
 9. form_builder()  /includes/form.inc:930
10. _form_builder_handle_input_element()  /includes/form.inc:889
11. imagefield_widget_value()  /includes/form.inc:1020
12. filefield_widget_value()  /sites/all/modules/contrib/imagefield/imagefield_widget.inc:207
13. field_file_load()  /sites/all/modules/contrib/filefield/filefield_widget.inc:215
14. _field_file_cache()  /sites/all/modules/contrib/filefield/field_file.inc:51

And when the field is set to display:

1. {main}()    /index.php:0
2. menu_execute_active_handler()    /index.php:18
3. call_user_func_array()    /includes/menu.inc:348
4. node_page_edit()    /includes/menu.inc:0
5. drupal_get_form()    /modules/node/node.pages.inc:15
6. drupal_process_form()    /includes/form.inc:121
7. form_builder()    /includes/form.inc:406
8. form_builder()    /includes/form.inc:930
9. form_builder()    /includes/form.inc:930
10. form_builder()    /includes/form.inc:930
11. _form_builder_handle_input_element()    /includes/form.inc:889
12. process_weight()    /includes/form.inc:1063
  • I traveled up the call stack trying to unset the field closer and closer to the source. I got all the way to the top and realized the right place to cut it off is during the loading of the node. I can use hook_nodeapi() when $op == 'load' and some other conditions to do this. +1 for Drupal giving me a clean way to do this :). Apparently I'm not yet allowed to answer my own question here :P – rjstatic Aug 3 '11 at 16:50

Would it be enough to deny editing rights to that field using Content Permissions?

  • I do believe that would work given that Content Permissions module would prevent the field from being added to the node object on load. – rjstatic Aug 3 '11 at 16:48
  • I don't think this would work for uid==1. – mpdonadio Aug 5 '11 at 17:46
  • It wouldn't, but IMO you shouldn't be using user/1 for day-to-day content entry. – akalata Aug 9 '11 at 4:04

Per my comment, use hook_nodeapi() when $op == 'load' as a place to unset() that field to prevent it from being built in the form code.

You could try to hook_form_alter the edit form for that content type, but I am not sure it will happen too late in the process.

This post can provide guidance: https://stackoverflow.com/questions/1067782/drupal-form-alter

It would essentially start out as

function module_form_alter (&$form, &$form_state, $form_id)
{
  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
  }
}

You would need to add logic to check for your content type, and then unset the necessary elements.

I am not 100% sure if it will leave the fields as-is when you do this, though, so test first on a safe copy of your site.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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