What are Ctools plugin (content type, access, etc), and how does one create them? What are Ctools plugins, and how does one create them?
Every now and then when working with Ctools Page manager, and Panels, it is useful to add custom Ctools plugins.

Ctools plugins comes in a large number of forms, and other modules, such as Feeds, Addressfield and Openlayers all make use of Ctools to provide plugins extensible by other modules. The most common forms of plugin though, are probably "content type", and "access". The first must not be confused with the entity "content" and its bundles, also called content types.

First, the boilerplate:

For any module to provide ctools plugins, they should first tell Ctools where to look for them. The hook below, says that we provide plugins for ctools, of the types "content_types", and "access". The function could be made simpler, but this way we ensure that only the right module gets told about the plugins, as well as only having it scan the disk for files when we actually provide the type of plugin being asked for.

function HOOK_ctools_plugin_directory($owner, $plugin_type) {
  // We'll be nice and limit scandir() calls.
  if ($owner == 'ctools' &amp;&amp; ($plugin_type == 'content_types' || $plugin_type == 'access')) {
    return 'plugins/' . $plugin_type;
  }
}


Below is an example directory structure for a module providing two plugins. One content type and one access plugin.

module/
module/module.info
module/module.module
module/plugins/
module/plugins/content_types/
module/plugins/content_types/two_views_in_one.inc
module/plugins/access/
module/plugins/access/term_depth.inc


Content type plugin

A content type in the Ctools vocabulary, is more often known as a "Pane", as provided by for example Views. In this question: Is there a way to intercept a list of NID&#39;s created by a view and use them as a filter for another view?, the author asks about programmatically feeding arguments to a view. While that in it self it not very hard, the follow up question quickly becomes, "How do I display the results?".

One answer, will be to create a new "content type".

Now, the actual content type plugin, again using the Views question from above, could look like this:

$plugin = array(
  'title' =&gt; t('Render a View with arguments from another'),
  'single' =&gt; TRUE,
  'category' =&gt; array(t('My custom category'), -9),
  // Despite having no "settings" we need this function to pass back a form, or we'll loose the context and title settings.
  'edit form' =&gt; 'module_content_type_edit_form',
  'render callback' =&gt; 'module_content_type_render',
);

function module_content_type_render($subtype, $conf, $args, $context = NULL) {
  $block = new stdClass;
  $block-&gt;title = 'My View';

  $view = views_get_view('get_nids');
  $view-&gt;preview('display_machine_name', array($arg1, $arg2));

  $nids = '';
  foreach($view-&gt;result as $node) {
    $nids += $node-&gt;nid . ',';
  }
  $nids = rtrim($nids, ',');
  $view = views_get_view('get_related');
  $view-&gt;execute_display('display_machine_name', array($nids));
  $block-&gt;content = $view-&gt;render();

  return $block;
}

/**
 * 'Edit form' callback for the content type.
 */
function module_content_type_edit_form($form, &amp;$form_state) {
  // No settings beyond context, which has already been handled.
  return $form;
}


With this module enabled, there should now be a new category in Panels, 'My custom category', where in one should find a single pane, rendering the code from above.

Access plugin

The access plugin below will provide the ability to filer out variants and/or panes based on the depth of a term as measured from the root of the vocabulary.

&lt;?php
/**
 * @file
 * Plugin to provide access control based upon a parent term.
 */

/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
$plugin = array(
  'title' =&gt; t("Taxonomy: term depth"),
  'description' =&gt; t('Control access by the depth of a term.'),
  'callback' =&gt; 'term_depth_term_depth_ctools_access_check',
  'default' =&gt; array('vid' =&gt; array(), 'depth' =&gt; 0),
  'settings form' =&gt; 'term_depth_term_depth_ctools_access_settings',
  'settings form validation' =&gt; 'term_depth_term_depth_ctools_access_settings_validate',
  'settings form submit' =&gt; 'term_depth_term_depth_ctools_access_settings_submit',
  'summary' =&gt; 'term_depth_term_depth_ctools_access_summary',
  'required context' =&gt; new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
);

/**
 * Settings form for the 'term depth' access plugin.
 */
function term_depth_term_depth_ctools_access_settings($form, &amp;$form_state, $conf) {
  // If no configuration was saved before, set some defaults.
  if (empty($conf)) {
    $conf = array(
      'vid' =&gt; 0,
    );
  }
  if (!isset($conf['vid'])) {
    $conf['vid'] = 0;
  }

  // Loop over each of the configured vocabularies.
  foreach (taxonomy_get_vocabularies() as $vid =&gt; $vocabulary) {
    $options[$vid] = $vocabulary-&gt;name;
  }

  $form['settings']['vid'] = array(
    '#title' =&gt; t('Vocabulary'),
    '#type' =&gt; 'select',
    '#options' =&gt; $options,
    '#description' =&gt; t('Select the vocabulary for this form. If there exists a parent term in that vocabulary, this access check will succeed.'),
    '#id' =&gt; 'ctools-select-vid',
    '#default_value' =&gt; $conf['vid'],
    '#required' =&gt; TRUE,
  );

  $form['settings']['depth'] = array(
    '#title' =&gt; t('Depth'),
    '#type' =&gt; 'textfield',
    '#description' =&gt; t('Set the required depth of the term. If the term exists at the right depth, this access check will succeed.'),
    '#default_value' =&gt; $conf['depth'],
    '#required' =&gt; TRUE,
  );

  return $form;
}

/**
 * Submit function for the access plugins settings.
 *
 * We cast all settings to numbers to ensure they can be safely handled.
 */
function term_depth_term_depth_ctools_access_settings_submit($form, $form_state) {
  foreach (array('depth', 'vid') as $key) {
    $form_state['conf'][$key] = (integer) $form_state['values']['settings'][$key];
  }
}

/**
 * Check for access.
 */
function term_depth_term_depth_ctools_access_check($conf, $context) {
  // As far as I know there should always be a context at this point, but this
  // is safe.
  if (empty($context) || empty($context-&gt;data) || empty($context-&gt;data-&gt;vid) || empty($context-&gt;data-&gt;tid)) {
    return FALSE;
  }

  // Get the $vid.
  if (!isset($conf['vid'])) {
    return FALSE;
  }
  $depth = _term_depth($context-&gt;data-&gt;tid);

  return ($depth == $conf['depth']);
}

/**
 * Provide a summary description based upon the checked terms.
 */
function term_depth_term_depth_ctools_access_summary($conf, $context) {
  $vocab = taxonomy_vocabulary_load($conf['vid']);

  return t('"@term" has parent in vocabulary "@vocab" at @depth', array(
    '@term' =&gt; $context-&gt;identifier,
    '@vocab' =&gt; $vocab-&gt;name,
    '@depth' =&gt; $conf['depth'],
  ));
}

/**
 * Find the depth of a term.
 */
function _term_depth($tid) {
  static $depths = array();

  if (!isset($depths[$tid])) {
    $parent = db_select('taxonomy_term_hierarchy', 'th')
      -&gt;fields('th', array('parent'))
      -&gt;condition('tid', $tid)
      -&gt;execute()-&gt;fetchField();

    if ($parent == 0) {
      $depths[$tid] = 1;
    }
    else {
      $depths[$tid] = 1 + _term_depth($parent);
    }
  }

  return $depths[$tid];
}