gpt4 book ai didi

php - Drupal CTools 访问检查

转载 作者:搜寻专家 更新时间:2023-10-31 21:36:09 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com