Drupalcamp Atlanta 2011 Think Like a Drupal Developer Erik Webb Technical Consultant, Acquia Drupalcamp Atlanta 2011
May 08, 2015
Drupalcamp Atlanta 2011
Think Like a Drupal Developer
Erik WebbTechnical Consultant, Acquia
Drupalcamp Atlanta 2011
Drupalcamp Atlanta 2011
Who am I?
• Joined Acquia in April 2010
• 30+ clients in almost 2 dozen states
• Systems administrator background (RHCE)
• LAMP development (10+ years PHP)
• Go Jackets!
Drupalcamp Atlanta 2011
Who are you?
Drupalcamp Atlanta 2011
Drupal is hard. It's not just a CMS, it's not just a framework, it has the power of both and the simplicity of neither. So where do you get started?
Drupalcamp Atlanta 2011
How Drupal works
1. Lookup URL in aliases, menu router
2. Check for cached page
3. Check permissions for page
4. Run page callback
5. Process render array
Drupalcamp Atlanta 2011
What Drupal Can Do
• Managing lots of structured data
• Strictly manage access to content and functionality
• Providing an organized administration section
• Allowing themers great flexibility
• Using common page layouts
Drupalcamp Atlanta 2011
Coders vs. Builders
• We’re all developers
• Good Drupal developers are “lazy”• Efficiency beats all
• Enjoy your weekends!
• Trust the community
• Rethink problems, not solutions
Drupalcamp Atlanta 2011
Building vs. Coding
• Building• Flatter learning curve
• Easier transition for “web developers”
• More maintainable*
• Dependent on community
• Book learnin’
• Faster development
• Reusable solutions
• Coding• Steep learning curve
• Easier transition for programmers
• Less maintainable*
• Customized to client
• Blood, sweat, & tears
• Faster performance*
• One-off advantages
Drupalcamp Atlanta 2011
What to code
Drupalcamp Atlanta 2011
Modifying Forms
• hook_form_alter(), hook_form_FORM_ID_alter()
• Administrative and user-facing forms
• Examples• Making individual fields required
• Changing order of fields
• Adding help text
• Additional validation or submission handlers
Drupalcamp Atlanta 2011
Changing Nodes
• Structure• hook_node_view()
• Add new data
• Modify existing data
• Change data in all places
• Presentation• template_preprocess_nod
e()
• Reformat data
• Combine into pseudo-fields
• NEVER alter data, treat as read-only
Drupalcamp Atlanta 2011
Menu Manipulation
• hook_menu_alter()
• Change menu properties• Hide links globally
• Change title
• Change permissions
• Override callbacks
• Efficient, runs only on cache clear
Drupalcamp Atlanta 2011
When to code or build
Drupalcamp Atlanta 2011
Content Aggregation
• Views• Graphical setup
• Community-driven
• Built-in caching
• Many display options
• Efficient prototyping
• EntityFieldQuery• Full control*
• Low overhead
• Extremely Drupal-specific
• No SQL!
• Combine with Entity Load Cache*
Drupalcamp Atlanta 2011
Contextual Changes
• Context module• Flexible API
• Easily exportable to code
• Extendable by non-coders
• hook_boot(), hook_init()
• Completely custom
• Performance-focused
• Handling condition and reactions
Drupalcamp Atlanta 2011
Examples
Drupalcamp Atlanta 2011
Client: Anytime an anonymous user visits a news article, I want to show them a modal with a subscription link.
Drupalcamp Atlanta 2011
Use Context
• Wrong way• template_preprocess_node() to add JS in a theme
• Right way• Create a modal reaction for Context
• Why not use hook_boot()?
• Right way• Use pure JavaScript to check for body.logged-in CSS class
Drupalcamp Atlanta 2011
Client: I don’t want anonymous users seeing any content tagged with the “Internal” keyword.
Drupalcamp Atlanta 2011
Nodes are everywhere!
• Wrong way• hook_node_access() doesn’t affect the menu
• Wrong way• Override ‘access callback’ for URL ‘node/%’
• Wrong way• hook_node_view() returns drupal_access_denied()
• Right way• hook_node_access_records() changes all node accessing
Drupalcamp Atlanta 2011
Client: I want all checkboxes to be those cool image buttons instead of the normal checks.
Drupalcamp Atlanta 2011
Form altering isn’t always the right way
• Wrong way• Use CSS/JS to change input[type=”checkbox”]
• Wrong way• Use hook_form_alter() to change the ‘#theme’ property for
all checkboxes to your theme function
• Right way• Simply create modulename_checkbox() to override all uses
of the form element
Drupalcamp Atlanta 2011
Client: I want to incorporate my company-‐wide header/masthead into my Drupal site.
Drupalcamp Atlanta 2011
iframe? What is this, 1999?
• Wrong way• Hard code into an include file in your theme
• Wrong way• Recreate the header using Drupal’s menu system and CSS
• Right way• Create a JS include somewhere else
• Right way• Use a web service to retrieve the HTML (and cache it!)
Drupalcamp Atlanta 2011
Questions?
Ask me - @erikwebb on Twitter