Follow

Creating a Custom Module

In this tutorial we will:

  1. Create a blank Module
  2. Add a custom content type
  3. Create a submodule
  4. Add a Pugpig Helper Function
  5. Add a Pugpig hook

STEP 1: CREATE A Module

Once we've got a Drupal Connector set up we can create a custom module. Custom modules are useful for creating a custom user interface, defining new fields for your template to use, implementing Pugpig hooks and helper functions.

A Drupal Module in its most basic form is a info file defining it to Drupal. All Drupal modules are stored in .../sites/yoursite/modules/ folder.

Open a new info file and copy the below into the file. The rest is blank.

name = Pugpig MyModule
description = Administration for Pugpig MyModule
core = 7.x
version = "7.x-0.0_0"
php = 5.2
package = MyModule
dependencies[] = pugpig
dependencies[] = field_ui

Save the file in to your .../sites/yoursite/modules/my-new-custom-module folder.

Open a new php file and save it as mymodule.module. Your .module file will contain Pugpig Hooks that will help you manage your data correctly.

All though your module has no practical function, yet, you can now view your module under the 'module' tab in your Drupal instance. It should be named Pugpig MyModule, under the heading MyModule.

STEP 2: ADD A CUSTOM Content type

Drupal has the ability to add custom content type. These can be very useful to structure your edition. To create a new content type go to: Structure > Content types > Add content type. A good machine naming format to follow is ModuleName ContentName.

Save your new content type and go to Manage fields. You should have a title and body field already. Under Add new field add a field name and select the field type to match. You may need to rename the machine name to make it unique.

This will open up the field settings tab where you can set some default settings for this field.

You can now create content for this content type. To be able to add this content to your edition go to Structure > Content types > Pugpig Edition > Manage fields > Page > Field Settings and tick the box marked ContentName.

STEP 3: Create a submodule

To save your content type for use on another computer or Drupal instance we can create it as a submodule. Go to Structure > Features > Create Feature. Enter the ModuleName ContentName and package it with the same package used above. In our example this is MyModule. Under the Content Types tab on the right you should see your content. Tick this box. Some other fields will automatically tick. In the example below my content MyModule Article had the field MyModule Image which had the field type image.

Download the feature. Unzip this and copy it into your .../sites/yoursite/modules/my-new-custom-module folder. It is often a good idea to put these custom features in to a subdirectory such as 'features'. Drupal will find all modules inside the .../sites/yoursite/modules/ folder no matter what level they are at.

You should add your submodule as a dependency of your module to make sure it is enabled whenever you enable your module. To do this add the below to your module.info file

dependencies[] = mymodule_article

STEP 4: Add a Pugpig Helper Function

Pugpig has several built in hooks to help you with your app. A full list of Pugpig hooks can be found here. We will add one such hook to the plugin. pugpig_value returns value of a field for a given node. In the example below pugpig_value returns the boolean value true or false as field_hide_in_table_contents is a check box.

$hidden = pugpig_value($node, 'field_hide_in_table_contents');

STEP 5: Add a Pugpig Hook

Once we've set-up our custom module we can add some hooks. Drupal hooks are functions with a predetermined ending. Pugpig has several built in hooks to help you with your app. A full list of Pugpig hooks can be found here. We will add one such hook, _pugpig_page_custom_categories, to the plugin. 

This hook adds custom categories in the OPDS feed. Custom categories can be used for tags, setting options (see our API Spec) and other custom needs in your app. If you want to hide ads you should return an empty section. In the below example the vale of the field field_hide_in_table_contents, a checkbox, is being sought. Then depending on the value a custom category toc_style may be added to the OPDS feed.

/*
* Implements hook_pugpig_page_custom_categories().
*/
function pugpig_rsc_admin_pugpig_page_custom_categories($node) {
$custom_categories = array();
$hidden = pugpig_value($node, 'field_hide_in_table_contents');
if ($hidden) {
$custom_categories['toc_style'] = 'hidden';
}
return $custom_categories;
}

Further information in about Hooks in Drupal can be found here

NEXT STEPS

Have a look at Create a Custom Theme guide.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk