Snippets

HFC Web Development Customizing Office Hours and Contact Info output from HFC Web Services Client.

Created by Micah Webner
<?php

/**
 * @file
 * This snippet shows some examples of how to use code from the Web Services Client to generate custom output for HFC Offices.
 *
 * @see https://bitbucket.org/hfccwebdev/module_hfccwsclient
 *
 * This snippet assumes you know how to create a Drupal module.
 *
 * This snippet assumes you have hfccwsclient module enabled and configured on your site.
 *
 * Devel module is your friend. I'll probably mention dpm() at least once, which is part of devel.
 * Comment it out or remove it when you're done; we don't like running devel on production sites.
 *
 * Unless otherwise noted, documentation references can be found at api.drupal.org.
 *
 * Whether you want to create a page or a block for your custom content, the methods will be pretty much the same.
 * The biggest difference is that a page callback usually calls drupal_set_title() and maybe drupal_set_breadcrumb() while
 * a block will not.
 *
 * For examples of how to set up a page, see hfccwsclient_menu() in the hfccwsclient.module file, and/or hook_menu() documentation.
 * For examples on how to set up a block, see hfccwsclient_block_info() and hfccwsclient_block_view(), and/or
 * review hook_block_info() and hook_block_view() documentation. In either case, these bits simply set up a place for the callback
 * function you're going to create. This function will (hopefully) return a render array rather than fully assembled HTML code,
 * because that's the Drupal way.
 *
 * To get started, we'll assume that hours_example_get_content() below is the function you're calling from either hook_menu() or
 * hook_block_view() as mentioned above. Remember, it can be called almost anything you want, as long as it starts with your module
 * name (hook_example_ in our case) and does not match an existing hook name (or super-weird things will happen and you will hate
 * Drupal until you figure out why.)
 */
 
 /**
  * Content callback for our example.
  *
  * @see hfccwsclient_contact_page()
  * @see hfccwsclient_hours_page()
  */
function hours_example_get_content() {

  // If you want your block or page to appear on the website while you're developing, make sure it returns *something*.
  // An empty array is enough for now. You will make things that go into it later. Note also the return at the end!

  $output = array();

  // For this project, the key element is hfccwsclient_get_offices(). Once you have this,
  // you have all the data you need, except the main campus number.

  $offices = hfccwsclient_get_offices();

  // If $offices is an array, you have data and are good to go.

  if (is_array($offices)) {

    // Use dpm() to look at the data you have.
    dpm($offices, 'offices');

    // At this point, you're on your own, but I'll leave you with some references.
    // The @see references above can be found in hfccwsclient.offices.inc, and provide
    // two complete page callbacks already.
    
    // For help building render arrays, take a look at my favorite snippets:
    // @see https://chacadwa.com/blog/2012/12/20/drupal-7-render-array-snippets

    // Here is another blog post I found that has some good info in it:
    // @see http://cocoate.com/ddbook/scary-render-array
    // I learned about #attached from this, which looks like a way to attach CSS or
    // Javascript files *only* when rendering the given array, which could be super-handy.

    // The documentation page for Render Arrays has some good info, but it's really
    // confusing, because it makes you guess why you would do some of what it does.
    // @see https://www.drupal.org/node/930760

  }

  return $output;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.