• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Drupal Design Patterns
 

Drupal Design Patterns

on

  • 8,272 views

Drupal's code involves many common coding techniques that make your code more readable, maintainable, and efficient. Although there are countless PHP tricks that could be used, I will focus on helper ...

Drupal's code involves many common coding techniques that make your code more readable, maintainable, and efficient. Although there are countless PHP tricks that could be used, I will focus on helper functions and other common methods used specifically by Drupal developers when writing good code.

Statistics

Views

Total Views
8,272
Views on SlideShare
6,903
Embed Views
1,369

Actions

Likes
32
Downloads
0
Comments
0

20 Embeds 1,369

http://erikwebb.net 422
http://acn.akarismatik.lan 398
http://acn.projet-karismatik.com 325
http://www.erikwebb.net 105
http://acn.houssem.blibech.interne.akarismatik.lan 28
http://acn.projets-en-cours.net 17
http://surfmagix.com 14
http://acn.naim.belkaid.interne.akarismatik.lan 14
http://paper.li 9
http://expandingsource.com 6
http://localhost 5
http://www.linkedin.com 5
https://auroraearth.atlassian.net 5
http://dos.localhost 4
http://dosdev.vf-pt.internal.vodafone.com 4
http://feeds.feedburner.com 4
http://acn.naimbelkaid.lan 1
http://twitter.com 1
http://dos.localnet 1
https://twitter.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Action - allow other modules to respond to actions, e.g. hook_node_(load|view|presave|etc.) or hook_user(load|view|presave|etc.)\nAggregate - collect data, e.g. hook_views_default_views and other CTools implementations\nAlter - using drupal_alter, pass variable through hooks for altering\n
  • Action - allow other modules to respond to actions, e.g. hook_node_(load|view|presave|etc.) or hook_user(load|view|presave|etc.)\nAggregate - collect data, e.g. hook_views_default_views and other CTools implementations\nAlter - using drupal_alter, pass variable through hooks for altering\n
  • Action - allow other modules to respond to actions, e.g. hook_node_(load|view|presave|etc.) or hook_user(load|view|presave|etc.)\nAggregate - collect data, e.g. hook_views_default_views and other CTools implementations\nAlter - using drupal_alter, pass variable through hooks for altering\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.\nCACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.\nA Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.\n
  • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.\nCACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.\nA Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.\n
  • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.\nCACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.\nA Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.\n
  • \n
  • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.\nCACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.\nA Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.\n
  • \n
  • drupal_write_record() returns SAVED_NEW or SAVED_UPDATED\n
  • drupal_write_record() returns SAVED_NEW or SAVED_UPDATED\n
  • drupal_write_record() returns SAVED_NEW or SAVED_UPDATED\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Title -.\nPage - arguments to pass to page callback.\nAccess - arguments to pass to access callback.\nLoad - used to load URL arguments into objects.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Drupal Design Patterns Drupal Design Patterns Presentation Transcript

  • Drupal Design Patterns Erik Webb Technical Consultant Acquia, Inc.
  • Introduction• Technical Consultant at Acquia, Inc.• Best practices guru• Working with Drupal 3+ years• PHP programmer for 10+ years• Linux sysadmin for 6+ years 2
  • Agenda1. Creating your own hooks2. Effectively using static variables3. Utilizing Drupals cache4. Storing objects in the database5. Making the most of menus 3
  • These aren’t“design patterns”... i know!
  • Creating Your Own Hooks “Hooks” by lovestruck. http://www.flickr.com/photos/ lovestruck94/3329381854/5
  • Hook Types• Action• Aggregate• Alter 6
  • Using Hooks1. Simple invoking2. Aggregating results3. Altering data4. Passing by reference 7
  • Simple Invoking<?php  // will call all modules // implementing hook_hook_name  module_invoke_all(hook_name);?> 8
  • Aggregate Results<?php  $result = array();  foreach (module_implements(hook_name) as$module) {    // will call all modules implementing // hook_hook_name and    // push the results onto the $result array    $result[] = module_invoke(hook_name);  }?> 9
  • Altering Data<?php  $data = array(    key1 => value1,    key2 => value2,  );  // will call all modules // implementing hook_my_data_alter  drupal_alter(my_data, $data);?> 10
  • Passing by Reference<?php  // @see user_module_invoke()  foreach (module_implements(hook_name) as $module) {    $function = $module . _hook_name;    // will call all modules implementing hook_hook_name    // and can pass each argument as reference determined    // by the function declaration    $function($arg1, $arg2);  }?> 11
  • Effectively using static variables “Time for Mommy to Windex those glass tank walls” by Ninithedreamer. http://www.flickr.com/photos/ ninithedreamer/4682877279/12
  • Static Variables• Not Drupal-specific (PHP feature)• Prevent duplicate work within a request• Used in often-called functions 13
  • Simple PHP Method<?php  function i_get_called_way_too_much() {    static $static_var;    if (!isset($static_var)) {      // generate contents of static variable      $static_var = some value;    }    return $static_var;  }?> 14
  • Drupal 7 Method<?php  function i_get_called_way_too_much() {    $var = &drupal_static(__FUNCTION__);    if (!isset($var)) {      // generate contents of static variable      $var = some value;    }    return $var;  }  // allows for static variables to be reset // from another function  drupal_static_reset(i_get_called_way_too_much);?> 15
  • Utilizing Drupals cache “Hello, I m Johny Cash comic book” by Bascom Hogue. http://www.flickr.com/photos/christophererin/ 4873831108/16
  • Caching system• Prevent duplicate work between requests• Used in complicated or remote functions• Further benefits using distributed cache• Only ephemeral data 17
  • Cache Expirationcache_set($cid, $data, $bin = cache, $expire = CACHE_PERMANENT) • CACHE_PERMANENT • CACHE_TEMPORARY • A Unix timestamp 18
  • Usage Example<?php  $value = cache_get(cache_key);  if (FALSE == $value) {    // generate $value    cache_set(cache_key, $value);  }?> 19
  • Create Your Own Cache<?php  /* @file my_module.install */  function my_module_schema() {    $schema[cache_my_cache] = drupal_get_schema_unprocessed(system, cache);    $schema[cache_my_cache][description] = A cache table for my module.;    return $schema;  }?><?php  /* @file my_module.module */  function my_module_flush_caches() {    return array(cache_my_cache);  }?> 20
  • Storing objects in the database “146/365 square peg into a round hole” by rosipaw. http://www.flickr.com/photos/rosipaw/ 4643095630/21
  • Saving Objects• drupal_write_record()• Single statements INSERT/UPDATE• Use with new $fetchStyle in D7 or db_fetch_object in D6 22
  • Schema Example<?php  // use the following table schema  $schema[my_table] = array(    description => My table.,    fields => array(      id => array(type => serial, unsigned => TRUE, not null => TRUE,),      value => array(type => varchar, length => 255, not null => TRUE, default => ,),    ),    primary key => array(id),  );?>
  • Example<?php  $my_data = new stdClass();  $my_data->id = 1;    // remove this to see INSERT behavior  $my_data->value = some value;  if ($my_data->id) {    // if this is a new entry, specify tables primary key    drupal_write_record(my_table, $my_data, array(id));  }  else {    drupal_write_record(my_table, $my_data);  }?>
  • Serializing Objects• Automatically done in Schema API • Set ‘serialize’ property in field• Uses PHP serialize() and unserialize()• Best used for additional options• Simplifies database schema 25
  • Advanced Schema Example<?php  // use the following table schema  $schema[my_table] = array(    description => My table.,    fields => array(      id => array(type => serial, unsigned => TRUE, not null => TRUE,),      value => array(type => varchar, length => 255, not null => TRUE, default => ,),      options => array(type => text, serialize => TRUE, default => ,),    ),    primary key => array(id),  );?>
  • Example<?php  $my_data = new stdClass();  $my_data->id = 1;    // remove this to see INSERT behavior  $my_data->value = some value;  $my_data->options = array(    opt1 => value1,    opt2 => value2,  );  if ($my_data->id) {    // if this is a new entry, specify tables primary key    drupal_write_record(my_table, $my_data, array(id));  }  else {    drupal_write_record(my_table, $my_data);  }?>
  • Making the most of menus “271/365 Run Rabbit, Run Rabbit...” by sosij. http://www.flickr.com/photos/sharonkcooper/ 4693026005/28
  • Arguments• Title• Page• Access• Load 29
  • Title Arguments• Use with t() or custom callback• Dynamic titles from URL 30
  • Page Arguments 31
  • Access Arguments 32
  • Load Arguments 33
  • Queueing/BatchingLocking mechanisms Registry alters More tricks... Renderable arrays Theme fallbacks and suggestions
  • Further reading and code snippets athttp://erikwebb.net/blog/drupal-design- patterns
  • Questions? @erikwebbwww.erikwebb.net