Skip to main content

How to Write a Custom Ctools Access Plugin Based on the Value of a Boolean Field

Filed Under: Drupal Planet, Drupal
I had the delightful task at work of determining the visibility of a view (in a node variant) based on the value of a boolean field. But it got better! Said boolean field was not on the node that was being viewed, it was on a node that referenced the node being viewed. But wait! It gets better! The referenced node was an organic group, and those references work differently then a usual entity reference! Confused yet? The actual purpose of the exercise was to allow a content manager to control the widget without having to touch the actual panel variant. This prevents confusion on their part, and also the possibility of the page layout being accidentally destroyed. If they want the widget on their page, they check a box.If they don't want it, leave the box unchecked. Simple enough. First things first: you have to let ctools know that there is a plugin to use. In your custom module (or in my case, in the relevant feature) .module, you need something like this: Then in your feature (or custom module), create the directory, so you have a my_feature/plugins/access folder structure. In the access folder, create a new .inc file. For the purposes of this exercise, the field we are using to determine visibility will be called field_widget, so I would call my file field_widget.inc. You have to start by defining the plugin, so: t('Node: Widget'), 'description' => t('Only displays this pane if the Widget field on the related Home Page for this Organic Group is set to On.'), 'callback' => 'my_feature_field_widget_ctools_access_check', 'default' => array('field_widget' => 1), 'summary' => 'my_feature_field_widget_ctools_access_summary', 'required context' => new ctools_context_required(t('Node'), 'node'), ); ?> Now write the callback: data)) { return FALSE; } ?> Because the field is *not* on the node that is currently being viewed, we have to identify the correct home page node. entityCondition('entity_type', 'node') ->entityCondition('bundle', 'division_home') ->fieldCondition('og_division_home_division_ref', 'target_id', $context->data->nid); $result = $query->execute(); ?> Now we have to load the node, to access the field_widget value. nid; $home_page_node = node_load($home_page_nid); ?> Here is where we check the value of field_widget. Being a boolean, if the value is 0, that means the boolean is not checked, and we want to hide the pane. field_widget[LANGUAGE_NONE][0]['value']) { } //Otherwise, show the pane. return TRUE; } ?> Here is the whole kit and caboodle: t('Node: Widget'), 'description' => t('Only displays this pane if the Widget field on the related Home Page for this Organic Group is set to On.'), 'callback' => 'my_feature_field_widget_ctools_access_check', 'default' => array('field_widget' => 1), 'summary' => 'my_feature_field_widget_ctools_access_summary', 'required context' => new ctools_context_required(t('Node'), 'node'), ); /** * Custom callback defined by 'callback' in the $plugin array. * * Check for access. */ function my_feature_field_widget_ctools_access_check($conf, $context) { // If for some unknown reason that $context isn't set, return false. if (empty($context) || empty($context->data)) { return FALSE; } // Identify the home page node for the current organic group. $query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'division_home') ->fieldCondition('og_division_home_division_ref', 'target_id', $context->data->nid); $result = $query->execute(); $home_page_nid = current($result['node'])->nid; $home_page_node = node_load($home_page_nid); // If the home page widget field is not checked, hide the pane. if (!$home_page_node->field_widget[LANGUAGE_NONE][0]['value']) { } // Otherwise, show the pane. return TRUE; } ?> And there you have it. :)

We'd love to partner with you on your next project!

Since we’re big on relationships, we’re all about finding the right fit. Will you take the next step with us to see if we’re a match?