Drupal 6, Matt has also written Mastering OpenLDAP, Managing and Customizing .. In this chapter, you will learn about the basics of module development. New Drupal Book: Learning Drupal 6 Module Development Creating Our First Module (M PDF) to get a feel for how the book is written. Attempted to obtain Learning Drupal 6 Module Development Butcher Matt by swe medical-site.info Studio as pdf, kindle, word, txt, ppt, rar and/or zip paper on this web.
|Language:||English, Spanish, German|
|Distribution:||Free* [*Register to download]|
learning drupal 6 module pdf. Drupal is an open source platform for building amazing digital experiences. It's made by a dedicated community. Anyone can use. Learning Drupal 6 Module Development. Core themes. Drupal includes core themes, which customize the "look and feel" of Drupal sites, for example, Garland . Drupal Add-Ons: Modules, Themes, Distributions, and Translations. 2 . learn about the dos and don'ts of Drupal programming. Every few years, the Drupal project releases a new major version of Drupal (Drupal 6, Subscribe to keep up -to-date on new developments in Drupal and to read blog posts.
Once all of these modules have been initialized, Drupal moves on to the next step. As it progresses through the request, it calls hook after hook, giving modules the opportunity to do their thing. Drupal's hook system is perhaps the single most important aspect of Drupal programming. Most if not all modules are nothing more than a collection of hook implementations that Drupal automatically calls as it works its way through the request lifecycle.
It's not just the Drupal core that declares and calls hooks.
Any module can declare and call its own hook. Many modules do in fact declare their own hooks, offering other modules the opportunity to integrate deeply with their services. Drupal core modules As noted earlier, Drupal provides several core modules.
These modules cannot be disabled, as their capabilities are integral to the standard functioning of Drupal. Just like other modules including the ones we will be writing , core modules function by implementing Drupal hooks. As Drupal invokes these hooks, each core module will respond as necessary, performing crucial functions at specific times during the course of a request.
In the following section, we will discuss several core modules in more detail, explaining what purposes these modules serve. Chapter 1 [ 15 ] The database We have taken a brisk walk through a typical Drupal request, and we have learned a little about modules and libraries. However, what about the database? Unlike many architectures, for Drupal the database doesn't stand front and center.
The database layer is not a module, nor do modules need to declare a specific relationship with the database. In fact, many Drupal modules do not ever interact directly with the database. For that reason, a module can exist, yet not have any database structures models , a central controller, or even any user- centered theming view.
In stead of treating the database as a central architectural component, Drupal merely provides an API for working with the database.
In Drupal, the database is just a place to store data. Need custom tables? Drupal provides a system for declaring them. Need to get data out of the database, or insert or update new data?
Drupal provides functions and an OO library for doing so. However, if you don't need such features for your code, you needn't work with the database at all.
In fact, in the next chapter we will write our first module without writing a single SQL query. Later in this book, we will see how to interact with the database using Drupal's robust database tools.
More than just data The Drupal database does not store just application data or content , but also configuration, caches, metadata data about data , structural information, and sometimes even PHP code. While Drupal may not be database centric, it most certainly requires a database. During initialization, Drupal will connect to a database and retrieve certain configuration data.
Later, as many of the core modules load, they too, contact the database to retrieve or update information. For most Drupal modules, the database is the location for data storage. Developing for Drupal 7 [ 16 ] The theme system The final component from our initial architectural diagram is the theme system.
Drupal separates the look-and-feel components of the system from the rest of Drupal, and provides a programmatic way for theming data.
The system for handling this is collectively called the theme system. Some of the theme system resides in the Drupal core libraries. This part is responsible for initializing themes and locating what theme functions and templates should be applied under certain circumstances. However, the majority of the theme code resides in themes and modules.
A theme is a structured bundle of code like a module that provides tools for transforming raw data into formatted output. Sites use at least one theme to apply a consistent and custom look-and-feel to all of the pages on the site. However, Not all theme code resides inside of a theme. One of the distinct advantages offered by Drupal is the capability to define default theming inside modules, and then provide mechanisms by which the theme layer can selectively override those themes.
In other words, a module might declare a rough layout for a component, but Drupal provides the structure for a theme developer to later modify the theme not the module to re-layout that component in a different way.
If this all sounds conceptually difficult, don't worry. Chapter 3 and Chapter 4 of this book are dedicated to working with the theming system. Now that we've had a quick architectural overview, let's change perspectives and quickly peruse the major subsystems offered by Drupal.
Drupal's major subsystems In the previous section we took a birds-eye view of Drupal's architecture. Now we are going to refine our perspective a bit.
We are going to walk through the major subsystems that Drupal 7 has to offer. Themes The theme subsystem was introduced above, and since Chapter 3 and Chapter 4 will cover it, we won't dwell too much on it here.
However, there are a few details that should be mentioned at the outset. Chapter 1 [ 17 ] The responsibility of theming a given piece of data is spread out over the Drupal core, the modules, and the applied theme itself.
While we don't modify the Drupal core code, it is important for developers to be able to understand that both module code and theme code can manipulate the look and feel of data. In this book, our focus will be on the module perspective. We work primarily with theming functions and templates that are defined within the module.
Typically, it is the best practice to work this way first—to ensure that every module has the ability to theme it's own data. Menus Drupal not only maintains content, but also details about how the site itself is organized. That is, it structures how content is related. The principle way that it does this is through the menu subsystem. This system provides APIs for generating, retrieving, and modifying elements that describe the site structure.
Put in common parlance, it handles the system's navigational menus. Two menu systems? One source of frustration for developers new to Drupal is the fact that the application's front controller is called the menu router.
However, this system is not identical to the menu subsystem. Its responsibility is to actually map the URLs to callback functions. We will return to the menu router in later chapters. Menus are hierarchical, that is, they have a tree-like structure. A menu item can have multiple children, each of which may have their own children, and so on.
In this way, we can use the menu system to structure our site into sections and subsections. Nodes Perhaps the most important subsystem to know is the node system. In Drupal parlance, a node is a piece of text-based, publishable content. It can have numerous fields defined, but typically it has a title, a body, and various pieces of auxiliary data, such as timestamps, publishing state, and author identification.
Nodes are content In computer science, the term "node" often has a special meaning. Drupal's own definition of node is distinct. It is not a point on a graph, but rather a piece of content. One might prefer to think of a Drupal node as a structured document. Developing for Drupal 7 [ 18 ] The node system is mostly implemented in the node module.
This sophisticated module provides dozens of hooks, though means that many other modules can and do interact with the node module via hook implementations.
Since nodes account for the content of the site, understanding the node system is an indispensable requirement for the Drupal developer. For that reason, we discuss aspects of the system throughout the book.
Files In previous versions of Drupal, externally generated files notably images were not handled directly by Drupal itself. Instead, there were a plethora of modules available for working with files. This has changed in Drupal 7, which now has a file-centered subsystem.
This means working with images, documents, and so on is now substantially easier. While Drupal has long had a sophisticated suite of tools for dealing with the filesystem in the files. Chapter 11 discusses this new API. Users Drupal is not designed to be merely a CMS, but also a platform for social media. Central to any concept of social media is a robust user system that can support not only administrative users, but also site members.
Drupal offers a powerful user subsystem that allows developers to work with just about all aspects of user lifecycle, from what fields show up on a user profile, to what permissions at a fine-grained level users have, to what particular encryption scheme is used to encrypt the user's password. Drupal's user system even provides tools for making authentication and other aspects of user management pluggable. Modules provide, for instance, LDAP integration or authentication through many of the publicly available authentication services like OpenID.
We discuss the user system, particularly the permissions aspects, throughout this book. Comments Perhaps the most common social media tool is comments. Drupal provides a subsystem that provides comment functionality for nodes and by extension, other data types. Chapter 1 [ 19 ] While one could imagine that comments are merely a type of node and, in fact, there are modules that do this , Drupal developers have chosen to implement comments as a separate type.
The comment module contains the majority of the comment code. However, again, as with the node system, it provides numerous hooks, and thus many other modules interact with the comment system.
Fields and entities In previous versions of Drupal, the node system was really the only system for creating structured pieces of textual content. Comments are too focused to be generally useful for extension. In order to extend node content beyond simple title and body fields, one needed to either write custom node types or use the Content Construction Kit CCK to build node types. However, Drupal 7 introduces two substantial subsystems that change this: The fields system brings most of CCK's functionality into core The entities system makes it possible to define other structured data types that are not nodes Already these new systems are making waves among Drupal developers, with the Drupal Commerce module leading the way in defining sophisticated entities that are not nodes.
These two subsystems are new, important, and also complex. So we will cover them in detail in Chapter 6. This system provides a robust programmatic tool for defining, displaying, validating, and submitting forms. It takes much of the busy-work out of developing forms, and also adds a layer of security. FAPI is so integral to Drupal that we use it numerous times throughout the book. Installation Profiles More sophisticated Drupal use-cases may benefit from the ability to customize the installation process.
Drupal provides an installation profile subsystem that can be leveraged to create a custom installer. Simple test Programmatically testing code is a well-established practice in the software development industry. In Drupal 7, it is a capability of the core Drupal distribution.
Using the Simple Test framework, developers can now use functional and unit tests to validate their code. We employ testing throughout this book.
In fact, we will write some of our first tests in Chapter 2. Blocks Along with the primary content, most web pages also have additional content displayed along the top, bottom, or sides of the page.
Drupal's block subsystem handles the configuration and display of these units of content. Most of this functionality is concentrated in the block module, and we will develop our first custom block in Chapter 2. Other subsystems In this section, we have provided some basic information on several high-profile subsystems. However, this list is not exhaustive. There are numerous others, and even some very important ones like Views that are not in core, but provided by third party modules.
Some of these other subsystems will be introduced and discussed throughout this book. However, Drupal is a sophisticated system, and no book of a manageable length can go into all of the details. For that reason, we provide references throughout the book pointing developers to the appropriate resources on the web and elsewhere.
Tools for developing Drupal code Drupal is a sophisticated platform, and from the glimpse above we can see already that there are numerous systems and structures to keep track of.
In this section, we try to provide tools that simplify or streamline the development process. Chapter 1 [ 21 ] We assume that you have your own web server stack and your own PHP development tools. The authors of this book each use different editors, operating systems, and web server stacks, so we collectively understand that there are many good tools for developing PHP applications. And Drupal itself doesn't require anything special. If you are just getting started, you may want to look at Acquia Drupal http: While running a PHP debugger is certainly not necessary, you may find running Xdebug or the Zend Debugger to be useful.
One of the authors of this book first learned how Drupal worked by stepping through an entire page load. Version control with Git and CVS Managing source code is a major part of any software development lifecycle. In this regard, Drupal 7 coincides with a major transition period for the Drupal community. However, Drupal has grown and the needs of the community have changed. Drupal is now moving to the Git distributed version control system. As we begin working with Drupal code, it will help to be able to have the tools necessary to work with Git.
From command-line programs to full-featured desktop applications, there is no shortage of tools for this. The book's code and Git The authors of this book have been working with Git for some time one, in fact, is leading the CVS-to-Git conversion. We have done our best to make sure that all of the code contributions in this book are available from a Git repository. You can access the code for this book, view it online in a web browser, submit patches, or even branch your own copy and build your own tool.
All the code is located at GitHub: Developing for Drupal 7 [ 22 ] The API site and coding standards A lot of background knowledge is required for writing good Drupal code. Of course, the aim of a book such as this is to provide that background knowledge. However, there are two reference resources that a burgeoning Drupal developer should have on-hand.
The first is the official online API documentation. Just about every function in Drupal is documented using in-line code documentation. The Doxygen program is then used to extract that documentation and format it. You can access the full API documentation online at http: Best practices in software development include keeping code clean, consistent, and readable.
One aspect of this is removing nuances in code formatting by following a fixed standard. This is particularly important on a platform like Drupal where thousands of developers all contribute to the code.
Without coding standards, the code would become a cluttered mishmash of styles, and valuable development time would be spent merely deciphering code instead of working on it.
The Drupal site has a manual on best practices http: All Drupal developers abide by these standards. While we have attempted to follow all of the coding guidelines in this book, we don't always explicitly point out what these standards are.
So new developers are encouraged to peruse the coding standards given on the previously mentioned web address. Developer-oriented modules There are a few Drupal-specific development and administrative modules that deserve a mention.
These are tools that are installed on the server to help simplify Drupal development. The developer module The Developer module provides several sophisticated tools designed to help developers create and debug Drupal code. For this, please refer to the following page: Functions used for dumping objects and arrays into formatted Drupal output Tools for analyzing database usage and performance A theme tool which indicates graphically which elements of a page were themed by which functions or templates A content generator for quickly populating your site with testing content Drush the Drupal shell Sometimes it is much easier to run some tasks with a single command in a console.
Drush provides a command-line Drupal interface. It can be used to execute tasks with a few keystrokes at the console: Drush can help accomplish tasks like this. Coder The Coder module provides two big features: It can examine code for compliance against the Drupal coding standards It can automatically convert modules from one version of Drupal to another: Summary This chapter has been an overview of Drupal for developers.
We saw what technologies Drupal uses. We looked at Drupal's architecture. We took a cursory glance at several prominent subsystems of Drupal's. We also got a feel of which developer-oriented tools are to be used while working with Drupal. Starting in the next chapter, we will be working with code. In fact, each of the subsequent chapters will focus on practical aspects of working with Drupal.
Coming up next is an introduction to the block system, where we will write our first module. In the last chapter we surveyed Drupal's architecture advanced. We learned about the basic features and subsystems. We also saw some tools available for development. Now we are going to begin coding. Here are some of the important topics that we will cover in this chapter: Starting a new module Creating. Our goal: The module will use the Block Subsystem to add a new custom block.
The block that we add will simply display a list of all of the currently enabled modules on our Drupal installation. The block subsystem was introduced in the previous chapter alongside other important Drupal subsystems.
Create a new module folder and module files Work with the Block Subsystem Write automated tests using the SimpleTest framework included in Drupal We are going to proceed in that order for the sake of simplicity.
One might object that, following agile development processes, we ought to begin by writing our tests. Agile software development is a particular methodology designed to help teams of developers effectively and efficiently build software. While Drupal itself has not been developed using an agile process, it does facilitate many of the agile practices. To learn more about agile, visit http: However, our goal here is not to exemplify a particular methodology, but to discover how to write modules.
It is easier to learn module development by first writing the module, and then learn how to write unit tests. It is easier for two reasons: SimpleTest in spite of its name is the least simple part of this chapter. It will have double the code-weight of our actual module. We will need to become acquainted with the APIs we are going to use in development before we attempt to write tests that assume knowledge of those APIs.
In regular module development, though, you may certainly choose to follow the TDD approach of writing tests first, and then writing the module. Let's now move on to the first step of creating a new module.
Creating a new module Creating Drupal modules is easy. How easy?
Easy enough that over 5, modules have been developed, and many Drupal developers are even PHP novices! In fact, the code in this chapter is an illustration of how easy module coding can be. We are going to create our first module with only one directory and two small files. For hooks, it is customary for the documentation therein to indicate which hook it is implementing.
Each parameter is initialized to a default value. Here, we follow the customary defaults, but you can defi ne them otherwise if you prefer. For that reason, the fi rst thing we do in this function is use a switch statement to fi nd out which operation to execute. Each case in the switch statement handles one of the different operations. For now, we don't have any administration confi guration to perform, so there are no cases to handle either configure or save operations.
We just need to handle the list and view operations. Let's look at each. Our block will only return one value we don't make use of deltas , so there is only one entry in the block descriptor array. A block descriptor can contain several different fi elds in the associative array. One is required: the 'info' fi eld that we have set above. But we could also provide information on caching, default weighting and placement, and so on.
The t Function In this example, there is one more thing worthy of mention. We use the function t. This is the translation function. It is used to provide multi-language support and also provide a standard method of string substitution. When t is called, Drupal will check to see if the user's preferred language is other than the default US English.
If the user prefers another language, and that language is supported, then Drupal will attempt to translate the string into the user's preferred language. For multi-language support, you will need to enable the Content translation module.
Whenever we present hard-coded text to a user, we will use the t function to make sure that multi-language support is maintained.
In simple cases, the t function takes just a string containing a message. In this case, the entire string will be translated. But sometimes, extra data needs to be passed into the string function. For example, we may want to add a URL into a string dynamically: 'Trying to access!
To do this, we would call t with the following parameters: t 'Trying to access! Running the above when the locale is set to English will result in a string as follows: Trying to access.
There are three different kinds of placeholder. We have seen one above. Sometimes it is desirable to do some escaping of the variables before substituting them into the string. The other two placeholder markers indicate that extra escaping is necessary.
This will, for example, convert HTML tags to escaped entities. We will look at theming in the next chapter. Usually, the result of this theming is that the output value is placed in italics. Don't trust user-entered data It is always better to err on the side of caution.
Do not trust data from external sources like users or remote sites. When it comes to the t function, this means you should generally not use placeholders beginning with! For example, it is inadvisable to do this: t 'Hello! We will use the t function throughout this book. A view Operation Now let's turn to the view case. The subject is the title of the block, and the content is main content of the block. The value of the subject entry will be used as a title for the block, while the content will be placed into the block's content.
Lesson Three Extend First Module Implement access control, alter an existing form, show a message when users login, programmatically create a block Learn: access control, form alters, blocks, hooks Lesson Four Todotoday Feature Module Build a todo list.
You will start off by building the functionality without writing any code. You will then turn it into a module using Features. Learn: Exporting configuration with Features into a module Lesson Five Extend Todotoday Feature Module You will add some custom code to display data on a custom path, add a View, a block and recreate the Feature code.
Learn: Recreating Features, extending Features with custom code. Lesson Six Welcome Module You will create a module that displays a configurable message to users when they log in. You will learn about the Token system, admin forms, pre-populate forms, storing persistent data in the Variables table and more about the Form API. You will learn how to set up custom database tables using the Schema API, use the Drupal query builder, save submitted data to the custom table and how to architect larger modules.
Allow other modules to add to the welcome message by invoking your very own hook.