Integrating Webforms with Open Atrium’s Panes by Creating a Custom CTools Plugin

Open Atrium (OA) core utilizes PanelizerFieldable Panels Panes, and CTools to give content managers (CM) a convenient UI for adding new and existing content to a node.  Open Atrium core also includes the Webform contrib module but, unfortunately, neither is it panelized nor is it ready as a CTools content type out-of the box.

This means that (a) core OA does not allow a CM a way to add other panes to a webform node and, thus, no way to add panes native display.  In this post, we will outline how to add a new content type to CTools and how to define that new content type.

First, we need to create a module.  I’m calling mine:

So in my modules/custom directory, I’ve created a directory:

and files:

Of course, you’ll need to write your info file but I’m sure you know that already!  Now we need to tell CTools where to find our plugin files so my module file looks like:

This is boilerplate code that implements hook_ctools_plugin().  It tells CTools to look in directory “plugins” for plugin files when looking for content types.

Within the tmbridge_webform_panes directory, next to the tmbridge_webform_panes.module, let’s create a “plugins” directory and inside that, let’s create a “content_types” directory.  My directory structure now looks like:

CTools knows to look for include files within the directory that is returned from the call to hook_ctools_plugin_directory() so let’s create an include file within the content_types directory.  We’ll call it tmbridge_webform_panes.inc.  Our directory structure now looks like:

The first thing we need in this file is a $plugin array definition that outlines what this plugin does:

Many of these elements are self-explanatory.  The first element, single, tells CTools that this plugin has 0 subtypes.  The next three elements are string definitions that will appear in the UI for CMs.  The latter four elements define the callback functions that will be called when this plugin’s code is run.  Next in this post, we’ll get to the meat-and-potatoes of writing a CTools plugin: callback functions.

We need to determine which types this plugin should be able to render.  Since we are concerned with webforms, we’ll query the database for all subtypes that have webform components and build an array containing that set:

Next, we’ll build the edit form that presents available options to the user by writing the callback function that we defined earlier in the plugin array.

We are only concerned with the webform to embed in this plugin, but this function can be extended to include any kind of user-defined settings you may need.

You may notice that in this callback function, we make a call to a helper function called _webform_panes_get_all_forms($subtype).  This is a custom auxiliary function defined as follows:

Something to note in this function is the line:

This is how we limit the select options on the edit form to include only nodes to which the user has access.  If our use case called for it, we could instead look here for the node_edit permission and only allow a user to include a webform to which they have edit access.  We’d like users to embed any forms to which they have access so all we are looking for is node_access.

Next, we define the form submit callback which saves the form state information:

Finally, we can talk about rendering.  The render function below creates a block from the selected webform and defines the view mode that will be used to render said node.

We check here if the pane has an override title defined and, if not, we use the webform node’s title as default.  A check for permission is made here as well.  We also take this opportunity to populate the $settings variable with a flag that will be used in the corresponding template to easily check if this block is a webform block.

We also define here the theme hook to use to build the block content.  This is completely dependent on the theme you are using, I’ve opted for a custom hook defined in my theme but use whatever suits your needs (we’ll talk more about creating a custom theme hook to use here in another post.).  You can find more information on the theme() function in drupal.org’s API documentation.

And, with one last function, we define the administrative title that will be used for a webform in a pane:

And voilà! We now have a new content type available to CTools to embed a webform within a pane on any Panelized node’s display.  The plugin pulls all accessible webforms into a select box and allows the user to select from that list:

Webform content type selection process -- step 1 and 2
We now have a Webform content type defined for use within Open Atrium.
Webform selection process -- step 3
And we can select from a list of all the webforms to which we have access

Of course, Open Atrium has native functionality to embed blocks and, at least with Webforms 3 and up, we can configure all webform nodes to automatically create an associated block — however, a custom CTools plugin yields a more comfortable user experience for CMs as Webforms are considered a distinct content type.  It also allows more customization of and during the rendering a webform.

Another alternative to a custom CTools plugin would be to “Panelize” the entire Webform content type.  This would allow CMs to add other panes to a webform node itself.  This comes with its own set of pros and cons.  I will cover this process along with its pros and cons in another post.

A corollary to creating a plugin for a new CTools content type is to create a new pane style plugin for CTools.  Combined together, a content type plugin and a style plugin can yield some great functionality.  I plan to write another post on that subject soon.

CTools plugins are incredibly powerful.  The above tutorial is merely one use case but the methodology and structure is consistent across the board.  Be sure to keep them in your toolchest for when a need may arise.

Leave a Reply

Your email address will not be published. Required fields are marked *