Git Product home page Git Product logo

drupalcamp's Introduction

DrupalCamp Denver – Developing Modules

First Module

  • Info file demo.info
  • Module file demo.module
name = demo
description = Just a simple demo module
core = 7.x

// Hooking in to nodeapi
function demo_nodeapi(&$node, $op_) {
  global $user;
  if ($op == 'view') {
    if ($node->uid == $user->uid {
      $node->title . ' [YOUR ARTICLE]';
    }
  }
}
  • Anytime someone says hook_something you replace hook with your module name
  • You might also involve an install file
    • Where you hold logic that only runs during install or update
function tricky_install() {
  drupal_set_message('You just installed Tricky!');
}

Routing a Request

  • The .htaccess file routes all requests through index.php
    • index.php send the request through a bootstrap process and then looks for a menu.inc
  • Your module can implement menu and then provide urls
    • function mymodule_menu()
    • return "Some text!" would just render some text to the screen
  • Based on the request from the browser and what Drupal found it generates the best match
    • Is welcome.html an alias?
    • Is it a path?
    • Essentially use hook_menu to map paths to functions
    • The menu system is the router

Hook System

  • The hook system is an event listener pattern that utilizes specially named functions
  • Create hooks with module_invoke_all('foo')
    • Ask all modules if they implement foo and then invoke it
    • yourmodule_foo() would use it
    • Namespace your hook with you module name to prevent collisions
  • Hook Node View Examples
// Appends [YOUR ARTICLE] to the node title if the logged in user is the owner of the node
function demo_node_view($node, $view_node, $language) {
  global $user;
  if ($node->uid == $user->uid) {
    $node->title . ' [YOUR ARTICLE]';
  }
}
  • The order of hooks getting called is based on weight and alphabetical order
  • Objects are implictely passed by reference in PHP 5.2+ which is what Drupal 7 uses
  • PHP tip: var .= ' TEXT' will concatenate and update the var

Captain Module

  • Load in to d7/sites/all/modules/captain
    • Don’t load in to the d7/modules folder as it is for core only
  • Enable through Module tab in Drupal UI
  • t is a Drupal Core function for translating user facing text
    • t can also take placeholders so the strings can be dynamic
    • t runs text through check_plain which does some string escaping for security
  • Nodes have url’s and Blocks do not
    • Nodes are primary content
    • Blocks are secondary content
      • They can contain content
      • They can be placed on many nodes
  • $view_mode is the context in which a piece of content is being viewed
    • Viewing a full page vs a list or RSS feed
  • $_SESSION is a super global for storing stuff related to session
  • devel Module provides a number of functions for development
    • dsm or dpm will print out in a nicely formatted way the object
  • A good tip is to prefix all data being added with your module name for debugging
  • $account is the user viewing the page
  • $user is the logged in user
  • global pulls in a global variable to be used locally
  • user_load will load a user object from the database

Menu Magic

  • Info files only require
    • name
    • description
    • core
  • Info files can list dependencies with dependencies
  • menu_magic_menu
    • The key ‘magic’ is the page path
    • The page callback is the function that will be called
    • The access callback is the function called when someone tries to view a page (permissions)
    • The file defines the file to include, a performance optimization
    • The type MENU_LOCAL_TASK is for the tabs at the top of a node
  • Advanced Feature – 'user/%user/magic'
    • Drupal automatically calls user_load($wildcard) based on the special loader %user
    • You can also use %node and %file
    • It looks for a user_load or node_load or any function you define hooked to load
  • Debug Tool – dsm(func_get_args())
    • Prints out the arguments passed to the function

Database Queries in Drupal 7

  • DB:TNG is a wrapper around PDO
  • Static queries take an associative array of the values to sub in to the query
  • Awesome Select Queries
    • Build an object up using function calls
    • $select = db_select('node', 'n')->fields('n', array('nid'))->condition('promote', 1)->orderBy('sticky', 'DESC')->execute();
    • hook_query_alter can be used by modules to alter the built up query object
    • Calling execute() is what returns results
    • addTag is used to add tags that core looks for when querying
  • More Queries
    • db_insert
    • db_update
    • db_deleted

Databasics

  • Drupal 7 Database API
  • Converting d6 queries to d7 SQL Tool
  • Nodes have full and teaser $view_mode
  • After you ->execute() you can call ->fetch() for one result or ->fetchAll() for all matches
  • Merge Queries db_merge are the equivalent of insert or update

Forms

  • Write forms in Drupal, not static HTML
  • In the $form[] array any key that does start with a # is a form element
  • drupal_map_assoc will turn an array of string in to an associative array with the string as the key and value
    • Caveat if you t() the values of the array in a drupal_map_assoc the keys would be translated also, which is likely wrong
  • You can use form_name_form_validate to setup server side validations
  • You can use form_name_form_submit to specify what happens when the form is going to be saved after validation
  • Use hook_element_info to create a new type and then you could write validations that could always execute as a preprocess
  • Use hook_form_alter to alter forms before they are displayed to the screen
    • You could use hook_form_alter to add a validator to user signup form that requires a long password
  • The name of the function that defines the form is the form_id
  • You can hook_form_id_alter to limit the scope of the hook to the specific form

drupalcamp's People

Contributors

bhauff avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.