Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By heshanlk on
Hi
I found the in the attaching a new theme to the Drupal we have to replace the same code at the node.tpl.php and so on , those are the anti patterns in the Drupal cause code in node.tpl.php can be consider as a dead code so the "Old Baggage" will occur. So what are the other types of anti patterns we can found i n Drupal
Thanks
Hesh
Comments
anti pattern
yes yes your correct
Drupal Design Patterns / Anti Patterns
Drupal Design Patterns / Anti Patterns
Drupal is a free and open source modular framework and content management system (CMS) written in the programming language PHP. There are many constructs in Drupal that fit the description of an "object". Some of the more prominent Drupal components that could be considered objects are modules, themes, nodes, and users. Nodes, Modules and themes are object-like as well, filling the "controller" role in many ways. Each module is a source file, but also bundles together related functions and follows a pattern of defining Drupal hooks.
Design Patterns in Drupal
Singleton
As I mentioned above we can consider modules and themes as an Objects. These objects do not encapsulate data, while we can identify one module from another by contained functions, so it should be thought as a class with a singleton instance.
hook functions :
Decorator
The core of the view node done by node_view () which is in node.module. If we want to display a content from a module we have to call node_view() from that module with validated parameters. The actual rendering, though, will depend on which type of node is passed to the function; this is directly analogous to having the class of an object determine its behavior when a message is sent to it.
More interesting is the use of hook_nodeapi(), which allows arbitrary modules to extend the behavior of all nodes. This feature allows for a wide variety of behaviors to be added to nodes without the need for subclassing. For instance, a basic story node has only a few pieces of associated data like: title, author, body, teaser etc.
Let say, its need files to be uploaded and attached to a node, so one could design a new node type that had the story node's features plus the ability to attach files. Drupal's upload module satisfies this need in a much more modular fashion by using nodeAPI to grant every node that requests it the ability to have attached files.
Observer
When a modification is made to a vocabulary in Drupal's taxonomy system, the taxonomy hook is called in all modules that implement it. By implementing the hook, they have registered as observers of the vocabulary object; any changes to it can then be acted on as is appropriate.
Bridge
The Drupal database abstraction layer is implemented in a fashion similar to the Bridge design pattern. Modules need to be written in a way that is independent of the database system being used, and the abstraction layer provides for this. New database layers can be written that conform to the API defined by the bridge, adding support for additional database systems without the need to modify module code.
Chain of Responsibility
Drupal's menu system follows the Chain of Responsibility pattern. On each page request, the menu system determines whether there is a module to handle the request, whether the user has access to the resource requested, and which function will be called to do the work. To do this, a message is passed to the menu item corresponding to the path of the request. If the menu item cannot handle the request, it is passed up the chain. This continues until a module handles the request, a module denies access to the user, or the chain is exhausted.
Command
Many of Drupal's hooks use the Command pattern to reduce the number of functions that are necessary to implement, passing the operation as a parameter along with the arguments. In fact, the hook system itself uses this pattern, so that modules do not have to define every hook, but rather just the ones they care to implement.
Front controller pattern
Index.php will respond on requests for a web based application, deciding what happens next.
Anti Patterns
Old Baggage
In the theme implementation in Drupal we have to replace the node.tpl.php files with the theme’s node.tpl.php file so we can consider node.tpl.php file in the modules/nodes can be consider as dead-code so the old baggage occurs.
No Name
In some Drupal functions which separate parameters into odd orders that must be memorized, when one variable as an array when one is numeric.
Control Freak
In Drupal thousand of rows of code will loaded in per request. But when end user add some more modules the Drupal the overhead will be high.
Spaghetti
When we use global for certain function the reusing parts will be impossible and also we can’t use that function in another script and also global the him itself so the what it does and from where it coming is difficult to understand.
Hard coding
Another one is you cannot query multiple databases by default there is db_set_active() which makes a call to the hardcoded db settings. But Drupal is not set up to handle multiple database natively.
// Below BY CHAMIN
Duplicate code
Duplication code segment can be found in the Drupal core (theme.inc) and the function grupal_get_html_head() in the common.inc .
Heshan Wanigasooriya
Github