Tuesday, October 8, 2013

Create a simple Drupal 7 module - Hello World page

A Drupal module is made at least by 2 elements :
  • a .info file
  • an other file (.php, .inc ?)

To choose a name for you module, don't forget those basic rules :
  • no capital letter
  • no special character (excepted _ underscore)

Your first .info file

A .info is in fact a your_module_name.info file.
Let's make in your example a drupal_test module.

name (mandatory)

The name of your module (with capital letters and spaces this time, if you want)
name = Drupal Test Module

description (recommended)

description = A super duper test module !

core (mandatory)

core = 7.x

files[]

The location of your (php ?) files
files[] = inc/example.php

The whole .info file 

name = Drupal Test Module
description = Provides a really neat widget for your site's sidebar.
core = 7.x
files[] =
inc/example.php
For Drupal 7, you can find all informations about .info files here:
https://drupal.org/node/542202

 Now the example.php file

 You can now edit your php file, located in /sites/all/modules/drupal_test/inc/example.php

We will make a simple "Hello World" test. We will
  • make a menu link (make shortcut like http://www.mywebsite.com/drupal_test/mylink)
  • create a basic content (your "Hello World")
in your example.php module, create a drupal_test_menu() function.
function drupal_test_menu() {
    $items['drupal_test/mylink'] = array(
      'page callback' => 'drupal_test_hello_world',
    );
    return $items;
  }
This part redirects http://www.mywebsite.com/drupal_test/mylink to your website, and calls the
drupal_test_hello_world()function.
function drupal_test_hello_world() {
    return array(
      '#markup' => '<p>Hello world</p>', 
    );
  }
 That's it !

If you have questions, i'll try to answer you. Do not hesitate to leave comments.

Monday, June 10, 2013

Drupal 7 - How to set permissions on your module using hook_permission() and user_access()

You're developing a module and you need site administrator to handle with permissions.

There are two steps :

  • Make your permission string (such as "administer nodes" in the admin/people/permissions page)
  • Check if user has access to the defined string
Let's see how to do it.
  • We will use hook_permission() to add your own "string" in the permission management page.
function mymodulename_permission() {
  return array(
    'my own permission string' => array(
      'title' => t('Administer my module'),
      'description' => t('Perform administration tasks for my module.'),
    ),
  );
}
Now you can administer permissions for your module. Note that you can add many permissions strings in the array you return.
You can now find your permission string at this page : admin/people/permissions
  •  When you're coding your page, you can user user_access() to check if the connected user has the given right.

    $string = "my own permission string";
    user_access($string)
    the result is a boolean value. True if the user has access, False if not.
    You can use user_access in the hook_menu() function. Here is an example :
    function mymodulename_menu() {
        $items = array();

        $items['mymodule-page-address'] = array(//this creates a URL that will call this form at "examples/form-example"
            'title' => 'Menu link name', //page title
            'description' => 'A form to mess around with.',
            'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
            'page arguments' => array('my_callback_function'), //put the name of the form here
            'access callback' => 'user_access',
            'access arguments' => array('my own permission string
    '),
        );

        return $items;
    }