Login/Register

Blogs

My Profile - Modiying the standard My Account/User Profile

In this post we'll modify the standard "My Account" user/profile menu and edit forms. We'll code the routines that allow users to edit their own "my profile alias".

In out previous post we discussed the fact that an administrator can turn this feature on or off. In other words, our code needs to check to see what the current system settings are before we allow a user to edit their alias.

First lets add our menu item. Note, we are appending the standard path "user". The following item is added to our hook_menu() impementation:

My Profile - Add Configuration Controls

In this post we are going to add a configuration page for the system administrator.

Our "My Profile" project has grown. So far the system is set up so that logically and administrator performs all the work. The design assumption so far has been that only a user granted special permissions can edit or delete "my profile" records. A new user is allowed to enter their "my profile alias" when they create an account. However, we haven't facilitated a user editing their alias at some later date.

Drupal 7 Module Development - Application Screenshots

In our "proof of concept" module, we employ ajax to perform a partial page repaint. The following are screen shots of the "proof of concept" module (my_color). We demonstrate the initial form and report state, invalid data entered and finally valid (success) data entered.

Drupal 7 Database Abstraction Layer

In example below we demonstrate some of the new Drupal 7 database abstraction layer features.

To add a record we are using db_insert(). "db_insert()" is more elegant than it's closest Drupal 6 counterpart (e.g. drupal_write_record). The method db_insert() returns the database key generated by the database.

Drupal7 Ajax Response Handler

In our hook_form() implementation, we added ajax attributes to a couple of our form elements. The example below is our ajax response handler. In Drupal7 you can use a simple response handler which simply updates a single element (not shown here), or you can send a batch of client side commands. Our example below sends a batch of client side directives.

Drupal7 Generate Report

We are using the Drupal7 implementation of theme_table to render our report. As of version alpha 6, theme_table() requires you to include at least 2 data rows (note! may be a bug).

In the example below, we check to see if we have any records to report.

If we don't have any records to report, we notify the user there is no records, create a default display.

If we do have records to display, we add the array elements to our $vars array structure.

Drupal7 Form API Ajax Directives

Drupal 7 adds "ajax" attributes to the Form API (FAPI).

In our example below, we've added ajax attributes to our "report" button. When a user "clicks" on the button, we will send a request to a server side handler named "my_color_ajax_response". When the server side handler sends a response, we replace the contents of the html div element "report_table". We use the tag "id" attribute to identify the appropriate target html div tag.

Drupal 7 Automatic Database Table Installation

Drupal 7 automatically adds database tables described with hook_schema. In our example below, even though we don't have an implicit hook_install or hook_uninstall method defined, Drupal adds the "my_color" database table when our module is enabled, and drops the "my_color" database table when our module is un-installed.

Drupal 7 Dynamic Class Loading

Drupal 7 introduces dynamic class loading. When you package your modules, you need to supply Druapl with a ".info" file. Inside the ".info" file you now declare an array of file listings. Drupal's new Module Registry inventories the file listings and dynamically loads method and objects as needed.

In our example below, we have have a file listing called "my_color_class.inc". The file listing includes several application objects. Before Drupal 7 we would need to use an "include..." statement in any other file listing before we could use those objects.

my_color.info

Module Development in Drupal 7 - Presentation Outline

What Are Modules
  • Customize, change and extend Drupal business functions.
    • You don't change the distributed Drupal source code. You add computer instructions in a module.

Community Modules versus Custom Modules

  • Community modules are generalized solutions.
  • Use Custom Modules for very specialized, narrowly scoped solutions. Modules are not required to be downloaded from Drupal.org.

Drupal 7 Runtime Requirments

  • PDO  -  Drupal 7 requires PDO enabled in your PHP environment.
    • PDO provides transaction support
    • PDO supports prepared statements
      • Prepared statements are faster (database engine caches the exection plan).
      • Prepared satements are safer, much less vulnerable to sql injection attacks.

Module Requirements

  • ".install" files now support implicit database table installation (and table drop). All you need to do is include schema expression.
  • ".info" files now allow you to declare file listings that are dynamically loaded. You don't need to use a PHP "include" expression for Drupal to find methods and objects provided in the dynamic load files. Dynamic loading allows Drupal to reduce your module's memory footprint (usage).

 Drupal Form API

  • Redirect moved from the $form object to the $form_state object.
  • Drupal 7 adds a set of "ajax" attributes that peform some partial page repaint.
    • drupal_set_message() and form_set_error() invoke full page paint. Therefore, we moved our validation and message notification in to our ajax response.
    • ajax response can be either a:

 Drupal Theme

  • Arguments to theme_table() are now required
    • Current version requires a minimum of 2 rows (a bug).

Drupal's Database Abstraction Layer

  • db_insert() is new to Drupal 7.
    • Closest relative in Drupal 6 is the drupal_write_record() method.
    • Both methods return the database autogen (serial) field values.
    • Db_Insert is a more elegant solution (easier to read, has an implicit return value -- drupal_write_record stores response in an object passed by reference).
  • db_query() in Drupal 7 returns a prepared statement
    • Drupal 6 db_query() returned a database query result object.
  • Method Chaining -- Since PDO is an Object Oriented Library it allows for method chaining. One exression can invoke many methods. Note! You may want to alter how much chaining you are using to control granularity of your exception handling.

 

Syndicate content