3

我想为我的面板选择规则创建一个 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/

有人知道为什么这不起作用?

4

1 回答 1

1

@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访问检查中的参数完全接收您提到的上下文,这意味着当您指定 no 时,您总是会得到 null required context

这样,您总是false可以从以下第一次检查中获得:if (empty($context)

于 2014-06-01T14:59:47.947 回答