Writing a custom contenttype pane (ctools plugin).
In this little 'how-to' we will build a youtube-embed contentpane with some basic configuration options. Although working with panels and panelizer is not common practice for everyone, this tutorial asumes you know what they are and how to use them (at least a little bit, like myself :-) ).
If you don't, look here: https://groups.drupal.org/node/195888 and read this http://drupalwatchdog.com/2/1/panelizer-layouts
We'll start off with a regular custom module to serve our 'plugins'.
'MYMODULE_plugins'.
Since we are writing ctools / panels plugins, ctools and panels are required and therefor we need to set them as a dependency, so the .info looks something like this:
name = MYMODULE_plugins<br>description = Custom contenttype panes<br>core = 7.x<br>package = Custom<br>dependencies[] = ctools<br>dependencies[] = panels
In order to keep things organised we will create a directory inside our module folder called 'plugins'. Inside it lets also create a directory called 'contentypes', since that's what we're making. This folder will contain our plugin files (the files where the actual plugin functionality is defined).
The .module file is very basic. The only thing we need to do is tell ctools where to look for our plugin files. Ctools gives us a hook for this: hook_ctools_plugin_directory();
So the .module file looks something like this:
<?php <br><br>/**<br> * Implements hook_ctools_plugin_directory().<br>**/<br><br>function MYMODULE_plugins_ctools_plugin_directory($owner, $plugin_type) {<br> if ($owner == 'ctools' && $plugin_type == 'content_types') {<br> return 'plugins/' . $plugin_type;<br> }<br>}
Next, in the above defined directory, we'll create our plugin file.
PLUGIN_NAME.inc
In this tutorial we want to embed a youtube video, so we'll name our file:
youtube_embed.inc
What we'll need , in short, for this plugin to work is:
– a plugin definition; an array with settings.
– an edit form; a form.
– a submit handler; a form_state processor.
– a view (render); the html content.
– some administrators information; title and description (some help?)
<?php<br><br>/**<br> * Plugins are described by creating a $plugin array which will be used<br> * by the system that includes this file.<br> */<br>$plugin = array(<br> 'title' => t('Youtube embed'),<br> 'description' => t('Shows a youtube video.'),<br> 'category' => t('Custom'),<br> 'edit form' => 'youtube_embed_edit_form',<br> 'render callback' => 'youtube_embed_render',<br> 'admin info' => 'youtube_embed_admin_info',<br> 'defaults' => array(<br> 'description' => t('some working tunes.. '),<br> 'youtube_url' => '<a href="http://www.youtube.com/watch?v=bTMZDr9RGBo',%3Cbr%20/%3E">http://www.youtube.com/watch?v=bTMZDr9RGBo',<br></a> )<br>);<br><br><br>/**<br> * 'Edit form' callback for the content type.<br> */<br>function youtube_embed_edit_form($form, &$form_state) {<br> $conf = $form_state['conf'];<br><br> $form['description'] = array(<br> '#title' => t('Description'),<br> '#description' => t('Description (administrative)'),<br> '#type' => 'textfield',<br> '#required' => FALSE,<br> '#default_value' => $conf['description'],<br> );<br><br> $form['youtube_url'] = array(<br> '#title' => t('Youtube url'),<br> '#description' => t('The url of the youtube video you want to show'),<br> '#type' => 'textfield',<br> '#required' => TRUE,<br> '#default_value' => $conf['youtube_url'],<br> );<br><br> return $form;<br>}<br><br>/**<br> * The submit form stores the data in $conf.<br> */<br>function youtube_embed_edit_form_submit($form, &$form_state) {<br> foreach (array_keys($form_state['plugin']['defaults']) as $key) {<br> if (isset($form_state['values'][$key])) {<br> $form_state['conf'][$key] = $form_state['values'][$key];<br> }<br> }<br>}<br><br>/**<br>* implements <br>**/<br><br>function youtube_embed_render($subtype, $conf, $panel_args) {<br><br> $url = (!empty($conf['youtube_url'])) ? check_plain($conf['youtube_url']) : '';<br> $description = (!empty($conf['description'])) ? check_plain($conf['description']) : '';<br> $title = 'Youtube: ('.$description.')';<br><br> $output = "<iframe src='".$url."'></iframe>";//lalala<br><br> $block = new stdClass();<br> $block->module = 'youtube_embed';<br> $block->delta = $sub_type;<br> $block->title = $title;<br> $block->content = $output;<br><br> return $block;<br>}<br>?>
With only a little bit of extra effort we can add some descriptive information for site-administrators. This gives quick info on what the settings are of our particular pane-instance.
<?php<br><br>/**<br>* add some info to the administrators view. <br>**/<br>function youtube_embed_admin_info($subtype, $conf, $context = NULL) {<br><br> $block = youtube_embed_render($subtype, $conf, array("youtube") );//, $context);<br> $block->content = t('<strong>Showing:</strong><br> @description <br>@youtube_url', array(<br> '@description' => $conf['description'],<br> '@youtube_url' => $conf['youtube_url'],<br> ));<br> return $block;<br>}<br><br>function youtube_embed_admin_title($subtype, $conf, $context = NULL) {<br> $output = t('Youtube embed');<br> if ($conf['override_title'] && !empty($conf['override_title_text'])) {<br> $output = filter_xss_admin($conf['override_title_text']);<br> }<br> return $output;<br>}
That's it! This should work nicely.
Hopefully this is usefull to anyone.