2013-09-26 35 views
3

我想爲我的面板選擇規則創建一個ctools訪問檢查。 我想要做的是檢查內容類型中的字段值。該字段被命名爲field_layout,其中的選項爲3,2,1。Drupal的CTools訪問檢查

我創建了訪問檢查和設置,規則顯示在選擇規則選項中。我可以添加它沒有任何問題,並將其設置爲我想要的。

我唯一的問題是,該規則不會生效...: -/

這裏是我的代碼使用方法:

<?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/

有人知道爲什麼這不行?

+0

它看起來像it've事做的是:$佈局= $上下文>數據 - > field_layout; 似乎不會返回一個值? – Basti

回答

1

@巴斯蒂的評論是正確的,只是多了一個步驟了:

$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時,您將始終爲空。

這樣,你同有false在第一次檢查從這個:if (empty($context)