什么是Ctools插件(内容类型,访问权限等),如何创建它们?


Answers:


84

时不时地使用Ctools Page managerPanels时,添加自定义Ctools插件很有用。

Ctools插件有多种形式,其他模块(例如FeedsAddressfieldOpenlayers)都利用Ctools提供可被其他模块扩展的插件。不过,最常见的插件形式可能是“内容类型”和“访问”。第一个不得与实体“内容”及其捆绑(也称为内容类型)混淆。

首先,样板

对于任何提供ctools插件的模块,它们应该首先告诉Ctools在哪里寻找它们。下面的钩子表示我们提供了用于ctools的插件,类型为“ content_types”和“ access”。该功能可以简化,但是通过这种方式,我们可以确保仅向正确的模块告知插件,并且在我们实际提供所要求的插件类型时,仅使模块扫描磁盘中的文件。

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

以下是提供两个插件的模块的示例目录结构。一种内容类型和一种访问插件。

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

内容类型插件

Ctools词汇表中的内容类型通常称为“窗格”,例如“视图”所提供的。在这个问题中:有没有办法截取一个视​​图创建的NID列表并将其用作另一个视图的过滤器?,作者询问有关以编程方式向视图提供参数的问题。尽管它本身并不很困难,但后续问题很快变成了“如何显示结果?”。

一个答案是创建一个新的“内容类型”。

现在,再次使用上方的Views问题的实际内容类型插件可能看起来像这样:

$plugin = array(
  'title' => t('Render a View with arguments from another'),
  'single' => TRUE,
  'category' => 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' => 'module_content_type_edit_form',
  'render callback' => 'module_content_type_render',
);

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

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

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

  return $block;
}

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

启用此模块后,面板中现在应该有一个新类别“我的自定义类别”,在其中应该找到一个窗格,从上方渲染代码。

访问插件

下面的访问插件将提供根据从词汇根源开始的术语深度来过滤变体和/或窗格的功能。

<?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' => t("Taxonomy: term depth"),
  'description' => t('Control access by the depth of a term.'),
  'callback' => 'term_depth_term_depth_ctools_access_check',
  'default' => array('vid' => array(), 'depth' => 0),
  'settings form' => 'term_depth_term_depth_ctools_access_settings',
  'settings form validation' => 'term_depth_term_depth_ctools_access_settings_validate',
  'settings form submit' => 'term_depth_term_depth_ctools_access_settings_submit',
  'summary' => 'term_depth_term_depth_ctools_access_summary',
  'required context' => 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, &$form_state, $conf) {
  // If no configuration was saved before, set some defaults.
  if (empty($conf)) {
    $conf = array(
      'vid' => 0,
    );
  }
  if (!isset($conf['vid'])) {
    $conf['vid'] = 0;
  }

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

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

  $form['settings']['depth'] = array(
    '#title' => t('Depth'),
    '#type' => 'textfield',
    '#description' => t('Set the required depth of the term. If the term exists at the right depth, this access check will succeed.'),
    '#default_value' => $conf['depth'],
    '#required' => 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->data) || empty($context->data->vid) || empty($context->data->tid)) {
    return FALSE;
  }

  // Get the $vid.
  if (!isset($conf['vid'])) {
    return FALSE;
  }
  $depth = _term_depth($context->data->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' => $context->identifier,
    '@vocab' => $vocab->name,
    '@depth' => $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')
      ->fields('th', array('parent'))
      ->condition('tid', $tid)
      ->execute()->fetchField();

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

  return $depths[$tid];
}

这太棒了!但是,对此有任何“官方”文档吗?(谷歌发现很多博客文章,但没有任何“官方” ..)
donquixote13年

1
在ctools模块本身中有很多示例,主要是我在这里进行挑选的。我已经尝试过不止一次自己编写正式文档的任务,但总是筋疲力尽。
Letharion

当我遵循本教程时,我遇到一个问题,那就是配置表单不仅是空白的,而且缺少按钮。
2013年

我不知何故第一次错过了这个问题/答案,真棒!
克莱夫(Clive)

2
@beth问题是module_content_type_edit_form()中的$ form不应通过引用传递。
贾斯汀

1

CTools插件是小文件,可以作为扩展其功能的任何模块的一部分。它们可用于提供组件(窗格),向面板添加其他样式选项,等等。

请查看“ 无面板的CTools插件”页面,以获取分步文档。简而言之,它像:

  1. 您需要通过以下方式将CTools依赖项添加到.info文件中:

    dependencies[] = ctools
    dependencies[] = panels
  2. 告诉CTools插件位于何处:

    <?php
    function MYMODULE_ctools_plugin_directory($module, $plugin) {
      if (($module == 'ctools') && ($plugin == 'content_types')) {
        return 'plugins/content_types';
      }
    }
    ?>
  3. .inc文件中实现插件(默认为$module.$api.inc)。示例插件代码:

    <?php
    $plugin = array(
      'title' => t('Twitter feed'),
      'description' => t('Twitter feed'),
      'category' => 'Widgets',
      'icon' => '',
      'render callback' => 'twitter_block',
      'defaults' => array(),
    );
    
    // render callback
    function twitter_block() {
      // Add twitter widget javascript
      $url = TWITTER_USER
      $widget_id = TWITTER_WIDGET_ID;
      $data = array();
    
      $data['url'] = $url;
      $data['widget_id'] = $widget_id;
    
      $content = array(
        '#theme' => 'my_block',
        '#content' => $data,
      );
    
      $block = new stdClass();
      $block->content = $content;
      $block->title = '';
      $block->id = 'twitter_block';
    
      return $block;
    }
    ?>

插件的默认位置如下所示:

MYMODULE/
    plugins/
        content_types/
        templates/
    MYMODULE.info
    MYMODULE.module  

有关更多示例,请检查ctools_plugin_example作为CTools模块一部分的模块,或在启用该模块后在Drupal UI中签出帮助页面(CTools插件示例)。


在Drupal 8中,它现在是核心的一部分(请参阅:Drupal \ Component \ Plugin),它提供对象继承,对象接口和单个文件封装。请参阅:Drupal 8现在:Drupal 7中的面向对象的插件

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.