doupao1978 2013-09-26 07:50
浏览 19

Drupal CTools访问检查

I want to create a ctools access check for my panel selection rule. What I wanna do, is to check a field value in a content type. The field is named field_layout with the options 3,2,1.

I created the access check and settings and the rule is showing up in the selection rule options. I can add it without any problems and set it up as I want to.

The only problem I have is, that the rule wont take effect ... :-/

Here is the code I use:

<?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;
}

The code is based on this tutorial: http://ramlev.dk/blog/2012/03/30/create-a-ctools-access-plugin/

Someone got an idea why this wont work?

  • 写回答

1条回答 默认 最新

  • dongshang4984 2014-06-01 14:59
    关注

    @Basti's comment is correct, just one more step up:

    $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'),
    );
    

    It is ok if don't need the context for your plugin. But the $context argument in the access check receives exactly the context you mentioned, which means you always get null when you specify no required context.

    This way, you alway have false at the first check from this: if (empty($context)

    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了