5

I'm wondering if there's some kind of inheritance system for Drupal 7 modules.
I want to create a module where users can upload a PDF file (attached to a node) that will bet uploaded to Calameo (a PDF online viewer) via REST web services.

How would I go about that? hook_file() doesn't seem to be sufficient and I'd feel kind of stupid just copying the whole file module and doing my adjustments, since I don't want to cope with upload of the file, just processing and displaying of it.

1

2 Answers 2

4

The hook system is the "OOP" way in Drupal. Drupal has been created when PHP was the only PHP version; that means that PHP didn't implement concepts present in other OOP languages, such as protected and private methods, interfaces, etc.

As result, Drupal didn't originally implement as much classes as the ones you find in the latest Drupal release, and classes where not used for the most important parts of the API implemented by Drupal, such as the content API, the form API, and the menu API. Drupal uses a lot of plain functions, but the way they are used allows to implement something similar to OOP concepts. The difference is that in Drupal it is not necessary to create a class that extends another class. Every module that implements the right hook can extend the code implemented by another module.

For example, if you need to extend the form provided by another module, you implement hook_form_alter() or hook_form_FORM_ID_alter(), and you can add form fields from the form created by another module, without to know which is "class" of that module. (Differently, in the OOP paradigm, you need to know which is the class that you want to extend.)

In the specific case, if you need to provide a way to allow users to upload a file while they are editing a node, which is then uploaded to a service site, then you can:

$form['mymodule_pdf_file'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#description' => t('The uploaded PDF will be loaded on the Calameo web server.'),
  '#default_value' => variable_get('mymodule_pdf_file', ''),
  '#upload_location' => 'public://mymodule_pdf_files/',
);
  • Add a form submission handler that load the file using $file = file_load($form_state['values']['mymodule_pdf_file']) upload it to the Calameo server.

  • The files uploaded with the "managed_file" form field are temporary, by default; that means they are removed when they are 6 hours old, the first time the cron tasks are executed. If you want to make them permanent, then you should use code similar to the following one:

$file = file_load($form_state['values']['my_file_field']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
2

Drupal uses the the hook system, se the docs about the OOP in Drupal.

To make it short.

You can hook into the file system and do the processing you need; where and how to hook into depends on how you plan on using the file system, for example a file field or something else. If you want to create the upload form itself, save file uploads is pretty simple with file_save_upload() and file_move().

Drupal has the entire theme system to control how something is displayed. There are many hooks that allows module to alter the output, and the site theme has the final say. Again how to proceed depends on your use case. If you use fields, creating a formatter would be the simplest way to create your custom output of the file.

You might want to take a look at the Scribd field, which might be doing what you want (only Drupal 6 atm, but the concept is still worth exploring it).

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.