I have the pleasure of attending the Lullabot Drupal Advanced API and Module Development workshop this week. I've enjoyed getting to know the Drupal platform and community over the past year and have based many successful projects on it. The team at Lullabot are known for their Dupal training and consulting work, and have some great sites and contributed modules under their belts to prove it. If any Drupal geeks happen to come across this, what follows are some tidbits I picked on day one.
views:
- the global arg() function is different from the views $arg. $arg is a placeholder you can place anywhere in the view URL. You then specify what values to replace the placeholder with either using the arguments UI or a PHP snippet
- Bonus Views allows for stuff like grids
- don't forget to use default views function to avoid having to use the views UI
Forms API
- form types are defined in the system module for examples and reference
- you can define your own form element via hook_element
- there are three areas at a minimum you need to define: process, theme, and validate
- when a key has a #, it's usually setting metadata; without a # refers to something that is actually a parent item that will be rendered
- to hide a label on a form element, set #title = false.
- had some major issues during presentation with a bug in the token module; the js files were getting inserted twice when using the field formatter
CCK: 3 key elements
- Fields - Storage
- Widgets - Editing
- Formatters - Display
- Formatters are a great way to add functionality and display features to a field. In the demo we used, we added note taking capabilities to an image content type, injecting the needed JS and CSS.
Performance and Scalability
-
Apache
- trim Apache modules not in use
- MaxClients: balance between knowing how many requests you can serve and conserving memory
- RAM dedicated to Apache / Avg Apache mem size = # max clients
- mod_expires: sets header information on requests (images, css files, js scripts) and instructs browse clients to cache the content locally
- .htaccess file controls the client caching for Drupal
- KeepAlive: tells Apache how long to reuse existing http connections. Too long and there are a bunch of a unused sessions hogging resources. Too short, and page loads are affected with a lag time. Recommendation is to set KeepAlive to On and Timeout to 2.
- MySql
- can be compiled to only use MyISAM and leave out InnoDB
- query caching: super easy win that has a huge difference.
- turned on in the configuration file
- query_cache_size=32M
- for a site with few logins, set it higher
- with many logins, keep it lower as the cache can't be used as much with all the updates taking place
- query optimizations
- find a slow query: you can turn on slow query logging in the config file
- debug the query using the EXPLAIN syntax
- "EXPLAIN select * from users;" will return how MySql is executing the query.
- save the query, save the world
- gave a great example where using EXPLAIN identified the need for an index because the query was referencing 3 fields in the same table. adding an index to the table changed the number of rows analyzed from 25k to 1.5k
- MySql tuning script
- PHP
- PHP Opcode cache
- parse PHP files
- compile to operation codes
- execute
- APC - http://pecl.php.net/package/APC
- php.ini
- resource limits
- set memory limit to something like 48M
- resource limits
- Best practices
- avoid repetitive function calls
- use most efficient approach available. E.g., limit results in an query and not in code
- the more modules you enable, the slower your site will run
- echo() is faster than print()!!!
- using unset on large variables frees up memory
- Emergency procedures.
- truncate watchdog, session, and cache tables
- Caching tweaks
- aggressive cache: bypasses module loading. Some modules like CCK will still work, but others like statistics will not
- minimum cache lifetime
- CSS PreProcessor
- Caching for developers
- cache_get() and cache_set()
- static variable caching: use a static variable in your functions rather than just going to the db every time
- mod_status
- Apache benchmark
- PHP Opcode cache
