Drupal bad practices

Fri, 2014-05-09 18:47

Below is a list of bad practices I have seen from other people and myself. Feel free to comment if you have your own thoughts.

You are a bad Drupal developer if:

  • You have no idea what is the difference between uninstalling a disabling a module

  • Remember - Disabling module does not remove any database tables from the database, nor remove any variables the module might have stored. You must disable a module to uninstall it. Uninstalling will remove any tables defined using hook_schema, and it will also invoke hook_uninstall which modules use to remove their variables and other data.

  • You abuse variable_set() and system_settings_form

  • It's really easy to store a variable or create a settings form using them, but storing tons of data will cause your site to hold all the data in memory for each and every page request.

  • You don't really use Form API's #tree property.

  • If you want to get the values in form values array in a structured way, go ahead and use #tree property.
    When creating configuration forms that you store values Drupal core variable system, note that you can throw anything to the database. Drupal will serialize your data anyway. If you have forms that has dynamic number of variables to store (per-node-type configurations, etc), consider using the #tree in the parent fieldset. This will put only a few rows to the the variable table, and will perform faster in when unseriazing data.

    Example (don't do this!):

  $form = array();
  $types = node_type_get_names();
  foreach ($types as $type => $label) {
    $form['my_module_config_' . $type] = array(
      '#type' => 'select',
      '#options' => drupal_map_assoc(range(1, 10)),
      '#title' => t('Select value for %type', array('%type' => $label)),
      '#default_value' => variable_get('my_module_config_' . $type, 5),

  return system_settings_form($form);

Above snippet simply gets the list of node types available, and adds a select list for each node type.
Why this bad:

  • Many rows in the variable table
  • It's good in performance to unserialize() a large array than unserialize a number of small elements.
  • Deleting a node type will not remove the row from the variable table unless you hook into that process as well.
  • Form is ugly

Do this:

  $form = array();
  $types = node_type_get_names();
  $form['my_module_config_node'] = array(
    '#type' => 'fieldset',
    '#title' => 'Node type configuration',
    '#tree' => TRUE, //  $label) {
    $form['my_module_config_node'][$type] = array(
      '#type' => 'select',
      '#options' => $opts,
      '#title' => t('Select value for %type', array('%type' => $label)),
      '#default_value' => !empty($values[$type]) ? $values[$type] : 5,

  return system_settings_form($form);

Why this is good:

  • Only one row in the variable table
  • Requires only one unserialize() call.
  • If you delete a node type, it will be removed when saving the form next time
  • Only one variable_del('my_module_config_node') in hook_uninstall.
  • It's sexy to have fieldsets and big arrays. Drupal is all about giant arrays after all.

Even if you use system_settings_form, you can add your own form validation hooks to alter $form_state['values'] before saving them to database.

  • You use t() function in your installation hooks, hook_schema and hook_menu

  • Note that t() won't work well during installation. Also, don't wrap your schema definition labels with t() function calls. It's no use unless you are looking for some troubles. Same goes to hook_menu implementations. Drupal will pass the title and description to t() function automatically unless you override the title callback.

  • Don't use user_access() frequently enough

  • Don't check if the user has a particular role, path checks, or anything to work around an access check. Use user_access() to check proper permissions.

  • You edit the tpl.php file in module folder

  • Some modules provide template files to make it easier for you to override the default output. Copy the template file from the module to your theme's templates folder and make your changes in the copied file.

  • Drupal core is >=7.x and you still call drupal_add_js() and drupal_add_css()

  • It works, right? Not always. Cached pages won't probably have the the assets because you are not attaching them what Drupal tends to cache. Use #attached property to attach the assets in a cache-friendly way.

  • You haven't used #states at all and you still think your forms are sexy

  • Make your forms sexier without any single line or JavaScript. See the awesome documentations here and here.

  • You can't live without PHP module in core or Views PHP module

  • For the 132,946th time, don't enable PHP module, and try your best to avoid putting PHP snippets in the database. If you need a special Views field, filter or sort handler, just extend the most matching handler and ut your precious PHP code there. PHP in a block ? hook_block_info / hook_block_view. PHP in a node ? hook_menu, and a custom page callback.

  • You always use mail() function directly to send emails

  • It's just a drupal_mail call and a hook_mail implementation. Don't use mail() or call the send method in the mail class directly.

  • You don't enable CSS and JS aggregation in Drupal core

  • I live in the country that has cheapest access to the Internet (though I doubt the quality is good). No regular use wants to know how well commented your CSS files are. If you have forgotten, go to Administer > Site Configuration > Development > Performance and enable CSS/JS aggregation. If your site is broken after that, see my point above to not use drupal_add_js and drupal_add_css.

  • You have 10+ roles in your site

  • Slideshow manager, Developer, Administrator, Blog writer, Junior Blog writer, Content approver, Backup maker... it's messy isn't it?

    Edit: Removing this item. Number of roles will depend on the requirement, but nonetheless, try to keep the number of roles as little as possible. It will not only make administering permissions easier, but also the server use less memory (permissions are cached in a static array indexed by the roles the current user has. If you have too similar roles with a large number of granted permissions, that can make the memory use slightly high). Also see user_role_permissions().

  • phpmyadmin cries internally when you SHOW TABLES

  • Reuse the fields as they match, uninstall modules properly, and don't host all your sites in a single database.

  • You have Colorbox, Shadowbox, Views Slideshow and 10 more modal and slideshow plugins enabled

  • Note that many of those module tend to add the assets even if you don't use it anywhere. Try to keep the number of such module low and reuse them.

    You are probably not aware about this, but ctools module comes with an awesome modal framework. Custom forms, in-modal-form validation, graceful degradation, and almost everything Chuck Norris might need.

  • You include jQuery in a tpl.php file

  • The demo web page of almost every 989,947 jQuery plugins out there, they include jQuery in the page source. If you are copying anything from that demo file, don't copy jQuery. It comes with Drupal core, and reusing it can save you nearly 70kb of page size.

  • You have several page-* and node-*tpl.php files

  • For smaller variants, implement hook_preprocess_node in your theme and make those changes. You can change, remove and add custom elements there. Besides, adding too much logic to tpl.php files is not a very good idea either.

  • variable_set() calls in regular page loads

  • Note that if you change a variable, Drupal has to refresh its caches which can add up a lot of time. Doing it on each page is nearly a disaster.


    Good stuff man, just keep posting more valueble points in drupal, meny thanks!

    Heya! I realize this is kind of off-topic but I needed to
    ask. Doees managing a well-established blog such as yours require a massive amount work?

    I am completely new to operating a blog but I ddo write in my journal every day.
    I'd like too start a blog so I can easily share my personal experience and feelings online.
    Please let me know if you hae any idea or tips for brand new asspiring bloggers.

    decrease of weight, lack of inches, or lower body fat percentage,
    this could indicate a desire to re-evaluate your plan and efforts.
    It's important that you are tracking how many calories you adopt in on a daily basis because that will make an extremely large difference in how much quicker it is possible to
    get a flat stomach. Drink Water-- The only liquid you ought to drink is water.

    Somebody essentially help to make seriously posts I'd state.
    This is the first time I frequented your website page and up to now?

    I surprised with the research you made to create this particular publish extraordinary.

    Fantastic job!

    God desires to break you out and break you into a new life.
    So would you like to understand the best way to get the very best iPhone Themes?
    With that said, there's a pattern that more android programmers will get to the trade.

    Save your money for something more worthwhile as the asking price simply
    doesn't out-weigh, let alone come close to matching, the benefits.
    They have succeeded in getting the gold mining town of Redding hooked on their products, which
    has resulted in their effectively owning the main supply of gold coinage produced in the Wasteland.
    Another option may be your local bank, the one that knows everything about
    you including your financial history, current holdings,
    and existing bank account.

    But there isn't a magic to steroids.

    For lateszt news you have to pay a quick visit
    internet and onn the web I found this web site as a best wweb site for latest updates.

    I love ont tendance à être trop. Ce type de rapports!
    Gardez les superbes travaille les gars, je suis
    ajout vous les gars à mon propre blogroll.

    Hello there! I know this is kinda off topic but I was wondering if you knew where I could
    find a captcha plugin for my comment form? I'm using the same blog platform as
    yours and I'm having difficulty finding one? Thanks a lot!

    There's certainly a lot to learn about this issue. I like all of the points you've

    I was recommended this blog by my cousin. I'm not sure whether
    this post is written by him as no one else know such detailed
    about my trouble. You're wonderful! Thanks!

    What's up, just wanted to tell you, I loved this article.
    It was helpful. Keep on posting!

    My brother recommended I might like this blog.
    He used to be totally right. This post truly made
    my day. You cann't believe just how a lot time I had spent for this information! Thanks!

    I am really impressed with your writing skills as well as with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?

    Anyway keep up the excellent quality writing,
    it is rare to see a great blog like this one these days.

    Hi there, I discovered your blog by the use of Google while searching for
    a comparable subject, your web site got here up, it appears to be like good.

    I have bookmarked it in my google bookmarks.
    Hello there, just turned into aware of your weblog thru Google,
    and located that it is truly informative. I am going to be careful for brussels.
    I'll be grateful if you happen to continue this in future.
    A lot of people will be benefited from your writing.


    Please let me know if you're looking for a writer for your blog.
    You have some really great articles and I think I would be a good asset.

    If you ever want to take some of the load off, I'd absolutely love to write some content for your
    blog in exchange for a link back to mine.
    Please blast me an e-mail if interested. Many thanks!

    I am really enjoying the theme/design of your weblog. Do you ever run into any browser compatibility issues?
    A small number of my blog readers have complained about
    my site not operating correctly in Explorer but looks
    great in Chrome. Do you have any recommendations to help fix this issue?

    Peculiar article, exactly what I wanted to find.

    This web site truly has all the information and facts
    I wanted about this subject and didn't know who
    to ask.

    Thanks very interesting blog!

    Hi there, You've done an incredible job. I will definitely digg it and personally suggest
    to my friends. I am confident they will be benefited from this
    web site.

    Helpful information. Lucky me I discovered your web
    site by chance, and I'm stunned why this twist of fate
    didn't came about in advance! I bookmarked it.

    I absolutely love your blog and find most of your post's to be precisely what I'm
    looking for. Would you offer guest writers to write content to suit your needs?
    I wouldn't mind producing a post or elaborating on a number of
    the subjects you write concerning here. Again, awesome web log!

    An outstanding share! I've just forwarded this onto a colleague who
    had been conducting a little research on this.
    And he actually ordered me lunch because I stumbled upon it for him...
    lol. So let me reword this.... Thanks for the meal!!
    But yeah, thanks for spending the time to discuss this issue here on your web site.

    Why viewers still make use of to read news papers when in this
    technological globe all is available on web?

    I was curious if you ever considered changing the page layout of
    your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having one or 2 pictures.
    Maybe you could space it out better?

    Ridiculous story there. What happened after? Thanks!

    Way cool! Some very valid points! I appreciate you penning this post plus the rest of the site is extremely good.

    For latest news you have to pay a visit the web and on world-wide-web I found this
    web page as a most excellent web site for most up-to-date updates.

    It's important to note that any marketing effort needs to be carefully planned.
    Add images, graphics and video increase conversion. Human nature survives intact on the virtual
    showroom and it must be woven into the cloth of all communications,
    on the internet or in particular person.

    Yesterday, while I was at work, my cousin stole my iphone and tested to see if
    it can survive a 30 foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views.
    I know this is completely off topic but I had to share it with someone!

    Have you ever considered about adding a little bit more than just your articles?

    I mean, what you say is fundamental and all.
    However think of if you added some great visuals or video clips to
    give your posts more, "pop"! Your content is excellent but
    with pics and videos, this blog could certainly be one of the best
    in its niche. Wonderful blog!

    Awesome! Its actually awesome paragraph, I have got much
    clear idea about from this post.

    Write more, thats all I have to say. Literally,
    it seems as though you relied on the video to make your point.
    You clearly know what youre talking about, why throw away your intelligence on just posting videos to your site
    when you could be giving us something informative to read?

    We don't AS a basic policy investigate the solvency of companies pointed out (exactly how most likely they are to fail), however there is a risk
    any company could struggle and it's hardly ever made public
    till it's too late (see the section 75 overview for defense suggestions).

    You really make it seem really easy along with your presentation but
    I to find this matter to be actually one thing that I think I would never understand.
    It kind of feels too complex and extremely extensive for me.

    I'm taking a look forward for your subsequent put up, I'll attempt to get the cling
    of it!

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You definitely know what youre talking about, why waste your intelligence on just posting videos to your
    blog when you could be giving us something informative to read?

    Hurrah! In the end I got a weblog from where I can truly take useful information concerning my study and knowledge.

    Great article! That is the kind of information that are supposed
    to be shared around the internet. Disgrace on Google for now
    not positioning this put up higher! Come on over and consult with my site .
    Thank you =)

    Hi! I realize this is somewhat off-topic but I had to ask.
    Does managing a well-established website such as yours take a large amount of work?
    I am completely new to operating a blog however I do write
    in my diary daily. I'd like to start a blog so
    I can easily share my experience and feelings online.
    Please let me know if you have any kind of recommendations or tips for brand
    new aspiring blog owners. Appreciate it!

    Hey! I could have sworn I've been to this site before but after checking through some of the post I realized it's new to me.
    Anyways, I'm definitely delighted I found
    it and I'll be book-marking and checking back frequently!

    Thanks to my father who shared with me regarding this web site, this website is truly remarkable.

    I’m not that much of a online reader to
    be honest but your blogs really nice, keep it up! I'll go
    ahead and bookmark your website to come back later on. All the best

    My spouse and I stumbled over here from a different web page
    and thought I should check things out. I like what I see so i am just following you.
    Look forward to going over your web page again.

    I've read a few excellent stuff here. Certainly worth bookmarking for revisiting.
    I surprise how a lot attempt you place to make the sort
    of great informative website.

    Hi there! This is kind of off topic but I need some advice from an established blog.
    Is it difficult to set up your own blog? I'm not very techincal but I can figure things out pretty
    fast. I'm thinking about setting up my own but I'm not sure where to start.
    Do you have any tips or suggestions? Many thanks

    I like it when individuals get together and share
    opinions. Great website, continue the good work!

    Very good blog you have here but I was curious
    about if you knew of any user discussion forums that cover the same topics discussed here?
    I'd really like to be a part of group where I can get suggestions from other
    experienced people that share the same interest.
    If you have any recommendations, please let me know.

    My brother recommended I might like this blog. He was once entirely right.
    This put up actually made my day. You cann't consider simply how much time
    I had spent for this info! Thanks!


    Your thoughts