4

I am a PHP programmer but I have never developed anything yet for drupal. I have used Drupal as a CMS before but I didn't do any custom code since everything I needed at before can be accomplished with existing modules.

Now, with my new project, I have an existing single-page app using a PHP RESTful API and AngularJS on the frontend. I want it to be integrated with a Drupal CMS.

I am aware of the following possible ways to do this:

  1. Rewrite my existing code into a Drupal module - There is very little possibility that this app will be re-used. Furthermore, it is more likely that this app will be used stand-alone without Drupal. I also don't think I have the time to learn how to write a Drupal module.

  2. Keep the backend API as it is now and write the frontend as a Drupal page. - This will put my frontend code inside the Drupal database and NOT in the filesytem anymore so I can't version control the frontend code.

I am not happy with the above two choices and I believe that there is a better way to do achieve what I want.

Additional background of my custom app:

I created this app as a way for me to learn more about single-page applications. It is already currently functioning well as a standalone app. The app basically collects information from several users and collates all those information to be displayed on an HTML table.

I am sure there is also a Drupal way of doing the things that my Angular app does (something like a custom node type with fields coming from the input of different users) but I would like to use my existing code if that makes it easier.

6

If your application can be displayed inside a Drupal page, writing a small custom module is the easiest clean solution. You don't need to rewrite your application, but only to provide some integration code. Your application should avoid output the complete HTML for the page, only its own content. It should also not initialize the PHP session as Drupal will take care of that. Otherwise it is as simple as:

/**
 * Implements hook_menu().
 */
function your_application_menu() {
  return array(
    'title' => 'Your Application',
    'page callback' => 'your_application_page',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
}

/**
 * Page callback: Displays Your Application on the page.
 *
 * @see your_application_menu()
 */
function your_application_page() {
   module_load_include('php', 'your application', 'your_application');
   $path = drupal_get_path('module', 'your application');
   drupal_add_js($path . '/js/angular.js');
   drupal_add_js($path . '/js/your_application.js');
   drupal_add_css($path . '/css/your_application.css');
   return your_application_output();
}

You may also be interested by the Angular JS module, which likely provide a much nicer way to embed Angular JS applications in Drupal.

6

I think you need to abstract your problem even more.

Answer this: What do you want Drupal to do for you?

With Drupal you can:

  • Build a routing system and facilitate a RESTfull API
  • Build a "single page" site.
  • Manage the content.

I write custom modules (like many) on a daily base, but not always give it back to the community because it is to project specific. But i create patches when i think my code fixes a bug or improves a module.

So start small with 2 files (MYMODULE.info, MYMODULE.module) and copy paste the code from Pierre in the module file, and your set.

Then you can expand it with tons of functionality.

TIP:

  • 1
    Yes, I think I should start learning module creation in Drupal. – dashmug Jul 27 '13 at 15:44

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.