php - Drupal CTools 访问检查

标签 php drupal panels drupal-ctools

我想为我的面板选择规则创建一个 ctools 访问检查。 我想做的是检查内容类型中的字段值。该字段名为 field_layout,选项为 3,2,1。

我创建了访问检查和设置,规则显示在选择规则选项中。我可以毫无问题地添加它并根据需要进行设置。

我唯一的问题是,规则不会生效......:-/

这是我使用的代码:

<?php

/**
 * Plugins are described by creating a $plugin array which will
 * be used by the system that includes the file.
 */
$plugin = array(
    'title' => t('Node: field layout'),
    'description' => t('Controls access by field_layout'),
    'callback' => 'he_layout_field_layout_ctools_access_check',
    'settings form' => 'he_layout_field_layout_ctools_settings',
);

/**
 * Custom callback defined by 'callback' in the $plugin array.
 *
 * Check for access.
 */
function he_layout_field_layout_ctools_access_check($conf, $context) {
    // If for some unknown reason that $context isn't set, we just want to be sure.
    if (empty($context) || empty($context->data) || empty($context->data->field_layout)) {
        return FALSE;
    }

    // If the layout set in the panels visibility rule settings is different from the field_layout
    // access to the pane is denied.
    $layout = $context->data->field_layout;
    if ($layout !== $conf['field_layout'][$context->data->field_layout[field_language('node', $context->data, 'field_layout')][0]['value']]) {
        return FALSE;
    }
    return TRUE;
}

/**
 * Settings form for the 'field_layout' access plugin.
 */
function he_layout_field_layout_ctools_settings($form, &$form_state, $conf) {
    $form['settings']['field_layout'] = array(
        '#type' => 'radios',
        '#title' => t('Layout'),
         '#options' => array(
            0 => '3',
            1 => '2',
            2 => '1',
        ),
        '#default_value' => $conf['field_layout'],
    );
    return $form;
}

代码基于本教程: http://ramlev.dk/blog/2012/03/30/create-a-ctools-access-plugin/

有人知道为什么这行不通吗?

最佳答案

@Basti 的评论是正确的,只是更进一步:

$plugin = array(
    'title' => t('Node: field layout'),
    'description' => t('Controls access by field_layout'),
    'callback' => 'he_layout_field_layout_ctools_access_check',
    'settings form' => 'he_layout_field_layout_ctools_settings',
    // 'required context' => new ctools_context_required(t('Node'), 'node'),
);

如果您的插件不需要上下文也没关系。但是访问检查中的 $context 参数接收到您提到的上下文,这意味着当您没有指定 required context 时,您总是得到 null。

这样,您在第一次检查时总是有 false:if (empty($context)

关于php - Drupal CTools 访问检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19022368/

相关文章:

java - 在 GWT 中布局元素的最佳方式是什么?

html - CSS定位面板问题

php - mysql_connect() localhost和127.0.0.1的区别

php - 为访问者开发后清除缓存

php - 我应该为 mp3 使用哪种 mime 类型

javascript - jQuery UI 对话框默认选项

drupal block 显示

mysql - 如何从 drupal 7 中的自定义表中获取行?

WPF 文本框和滚动行为

php - ImageMagick thumbnailImage 超过 30 秒的最大执行时间