Creating Custom Panels Layouts & Content Types

Jonathan Adams, Senior Developer
#Drupal | Posted

The Panels module provides a flexible and intuitive way to place content throughout your site. This blog post will show you how to use and customize some of the features in panels including layouts and content types.Layouts

As it sounds, layouts allow users to choose where each piece of content on a given panel can be rendered. There are two different types of layouts. Builders allow users to create a custom layout inside the GUI via the layout Designer, while predefined layouts allow you to write the actual html to be used within your layout. This blog post will cover the latter option.

When creating a layout you can create one either inside your theme or within your module. For this example we will be creating a two-column layout using the theme implementation. In your theme create a folder named “layouts” and in your theme_name.info file add the following lines.

  1. ; Panel Layouts
  2.  
  3. plugins[panels][layouts] = layouts

This line tells the theme to look for custom layouts within our layouts directory inside our theme. Inside of your Layout’s folder, create a folder called one_third_left and inside of that folder create three files, one_third_left.inc, one-third-left.tpl.php, one-third-left.css. Your directory structure should be as follows:

 

The .inc file controls the regions that are present as well as the theme and css to be used within the layout. Open the one_third_left.inc file and add the following lines.

  1. <?php
  2.  
  3. /**
  4. * Implements hook_panels_layouts()
  5. */
  6. function theme_name_one_third_left_panels_layouts() {
  7.  
  8. $items['one_third_left'] = array(
  9.     'title' => t('Theme Name: One Third Left'),
  10.     'category' => t('Theme Name'),
  11.     'icon' => 'one-third-left.png',
  12.     'theme' => 'one-third-left',
  13.     'css' => 'one-third-left.css',
  14.     'regions' => array(
  15.       'header' => t('Header'),
  16.       'content' => t('Content'),
  17.       'sidebar' => t('Sidebar'),
  18.     ),
  19.   );
  20.  
  21. return $items;
  22. }

This array defines all the needed items for a layout to appear. The title as it sounds, is the title of the panel we created. Category is used to group similar layouts together. When choosing a layout you first need to select the category and then the corresponding layouts will be shown. The icon is a visible representation to help users see what the layout looks like at a glance (The icon was omitted for this tutorial). The theme is the tpl file used to control the layout and html. Css controls the styling of the layout, it is also important to know that the css file is also used to control the layout shown inside the gui. Regions are used to define where content can be placed inside of the layout.

Open one-third-left.tpl.php and add the following lines.

  1. <div <?php !empty($css_id) ? print 'id="' . $css_id . '"' : ''; ?>>
  2. <div class="columns">
  3.     <div class="header">
  4.       <?php print $content['header'];?>
  5.     </div>
  6.   </div>
  7.   <div class="medium-4 columns left-sidebar">
  8.     <?php print $content['sidebar'];?>
  9.   </div>
  10.   <div class="medium-8 columns">
  11.     <?php print $content['content'];?>
  12. </div>
  13. </div>

In this tpl example, we are creating a 2 column layout with a header. Notice the $content variable. Each of these corresponds to a region that we created inside of our .inc file. To add more regions simply add them to the .inc file and place them in the tpl. I will not cover the css in this tutorial but once cache is cleared you should now see your new layout available for use.

Content Types

Ctools content types are like blocks in the sense that they both provide an easy way to create pieces of content that can be placed throughout a site. Where content types excel is in the configuration. While blocks allow you to configure them by means of a form, those settings are global, so every instance of a block will have the same settings no matter where it is placed. Content Types on the other hand allow you to create one content type and have different settings applied. This portion of the tutorial will discuss how a ctools content type can be created.

First add ctools as a dependency in your module_name.info file.

dependencies[] = ctools

Create a folder named “plugins” inside of your module directory. Next add a folder named “content_types” inside of the newly created plugins directory. Then add the following lines to your module_name.module file.

  1. <?php
  2.  
  3. /*
  4. * Implements hook_ctools_plugin_directory().
  5. */
  6. function module_name_ctools_plugin_directory($owner, $plugin_type) {
  7. if ($owner == 'ctools' && $plugin_type == 'content_types') {
  8.    return 'plugins/content_types';
  9. }
  10. }

This hook tells ctools where to look for its content types. In this example we will create a simple content type that displays the 5 newest published nodes and allows the user to choose whether or not the title is linkable.

Inside the content_type folder, create another folder called newest_node and inside that folder create a file called newest_nodes.inc. Add the following lines to the file.

  1. <?php
  2.  
  3. $plugin = array(
  4. 'title' => t('Newest Nodes'),
  5.   'description' => t('Display a list of the newest nodes'),
  6.   'single' => TRUE,
  7.   'render callback' => 'newest_nodes_content_type_render',
  8.   'defaults' => array(),
  9.   'edit form' => 'newest_nodes_edit_form',
  10.   'category' => array(t('My Module')),
  11. );

This array is used to define our settings for the content type. As it sounds, title is the title of the content type. Single is used to show that our content type has no subtypes. The render callback is the function that is used to generate the markup for our content type. Defaults is the default ctools context. In our case we do not have one. The edit form is the form function used to generate the content type settings. Finally category is used to group similar content types. To add the form add the following lines.

  1. /**
  2. * Content type settings form.
  3. */
  4. function newest_nodes_edit_form($form, &$form_state) {
  5.  
  6.   $conf = $form_state['conf'];
  7.  
  8.   $form['number'] = array(
  9. '#type' => 'select',
  10.     '#title' => t('Select the number of node to display'),
  11.     '#options' => array(5 => t('Five'), 10 => t('Ten'), 15 => t('Fifteen')),
  12.     '#required' => TRUE,
  13.     '#default_value' => isset($conf['number']) ? $conf['number'] : ''
  14.   );
  15.  
  16.   $form['link'] = array(
  17.     '#type' => 'checkbox',
  18.     '#title' => t('Link title to node'),
  19.     '#default_value' => isset($conf['link']) ? $conf['link'] : '',
  20.   );
  21.  
  22.   return $form;
  23. }
  24.  
  25. /**
  26. * Content type submit handler.
  27. */
  28. function newest_nodes_edit_form_submit($form, &$form_state) {
  29. foreach (array('number', 'link') as $key) {
  30.    $form_state['conf'][$key] = $form_state['values'][$key];
  31. }
  32. }

This simple form let users decide how many nodes to display and whether or not a link should be displayed. Now lets proceed with adding the actual render function.

  1. function newest_nodes_content_type_render($subtype, $conf, $args, $context) {
  2.  
  3. $results = db_select('node', 'n')
  4.     ->fields('n', array('nid', 'title'))
  5.     ->condition('status', 1)
  6.     ->range(0, $conf['number'])
  7.     ->execute()
  8.     ->fetchAll();
  9.  
  10.   $items = array();
  11.  
  12.   foreach ($results as $result) {
  13. if (isset($conf['link'])) {
  14. $items[] = l($result->title, 'node/' . $result->nid);
  15. }
  16.     else {
  17. $items[] = $result->title;
  18.     }
  19. }
  20.  
  21.   $block = new stdClass();
  22.   $block->title = t('Newest Nodes');
  23.   $block->content = theme('item_list', array('items' => $items));
  24.  
  25.   return $block;
  26. }

This function does a simple db_select using the variables we set in our form to limit the number of items displayed and to also decide whether or not to display a link. Clear your cache and you should now see your newly created content type available along with the settings form.

 

Finally here is what my Content Type looks like once styled.

While my previous example is a bit on the simplistic side it does highlight the power of ctools layouts and content types. When used with the appropriate CSS responsive layouts can be created and easily changed per node type. For more information on the Panels Module, check out some of our other Phase2 thoughts.

Jonathan Adams

Jonathan Adams

Senior Developer