Adding panels support to the Reply module

One of the major disadvantages of entities in Drupal 7, is the lack of support for built-in comments. This is due to the model of the core comments module, which is heavily tied to nodes -node comments are essentially settings of each content type-, and not available for other entity types. In Drupal 8 this has changed already, and comments are considered fields that can be attached to any entity type.

For Drupal 7, in the meantime, there’s been a similar solution for a while, which is the reply module. The reply module leverages the Entity API to define a “reply” entity type, and offers a UI to create “reply” bundles. Then, via the Field API, you can add a “Reply” field to any entity type, choosing the reply bundle that should be used by that particular field. The flexibility that gives is huge, since it means that you’re not restricted to just a comment type for your application.

I’ve been using it in a project for some time, and have to say the module works pretty well, although there’s not an official stable release yet. One of the problems I came across when I started to use it, was the lack of support for the panels module, so I decided to write a ctools “content_type” plugin to add that support myself. Here is the process I followed.

First, let ctools know where the ctools plugins live:

Then, create the plugin in the relevant folder. Let’s name it reply_add_form. Following the code above, in this case it would be under “{module_folder}/plugins/content_types/reply_add_form”. Let’s have a look at the $plugin declaration array:

No magic in there if you’re familiar with ctools (if you aren’t, you can install the advanced_help module, which has plenty of documentation on how to create your own ctools plugins). One point to highlight, is the fact that the plugin has two edit forms instead of one: in the first one, we’ll choose the entity type for which we’re adding the form, and in the second one, we’ll choose the reply field used, from the available within the selected entity type (could be more than one).

Also, note that the required context bit is accepting “any” context available within the panels page. I had to do it that way because, unlike with nodes, it’s impossible to know in advance all the entity types that will be available in the system how the user will name the arguments and contexts in panels and offer just the relevant options. Instead, all contexts are accepted, and the user (which will be usually a developer or a site-builder, anyway), is responsible for choosing the right one in the settings form.

Let’s have a look at the settings forms:

Pretty simple stuff. As mentioned, the first one allows to select the entity type for which the reply form will be added. The second one, gets a simple map containing all the fields in the system, filters them out to keep only reply fields, and then filters them out again to show only the ones available for the entity type selected in the first settings form. The forms look like this:







Finally, the render function, which simply takes care of loading the entity passed in the plugin config, and calls the appropriate function of the reply module, to let it create the reply form.

I’ve uploaded the plugin file as a gist to github, so you can download it from here. Also, it’s worth noting that there’s an issue in the issue queue of the reply module to get the panels support implemented, with this plugin supplied as a patch, so hopefully you won’t need to keep it as a separate plugin, as it should make it into the next alpha release soon, and in the dev branch in the next few days.

Leave a Reply

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