3

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?

mzy
  • 1,754
  • 2
  • 20
  • 36
Basti
  • 666
  • 2
  • 11
  • 29
  • It look like it've something to do with that: $layout = $context->data->field_layout; Seems like it wont return a value?! – Basti Sep 26 '13 at 08:34

1 Answers1

1

@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)

lifecoder
  • 1,424
  • 1
  • 14
  • 29