array( * 0 => array( * 'name' => 'name of access plugin', * 'settings' => array(), // These will be set by the form * ), * // ... as many as needed * ), * 'logic' => 'AND', // or 'OR', * ), * @endcode * * To add this widget to your UI, you need to do a little bit of setup. * * The form will utilize two callbacks, one to get the cached version * of the access settings, and one to store the cached version of the * access settings. These will be used from AJAX forms, so they will * be completely out of the context of this page load and will not have * knowledge of anything sent to this form (the 'module' and 'argument' * will be preserved through the URL only). * * The 'module' is used to determine the location of the callback. It * does not strictly need to be a module, so that if your module defines * multiple systems that use this callback, it can use anything within the * module's namespace it likes. * * When retrieving the cache, the cache may not have already been set up; * In order to efficiently use cache space, we want to cache the stored * settings *only* when they have changed. Therefore, the get access cache * callback should first look for cache, and if it finds nothing, return * the original settings. * * The callbacks: * - $module . _ctools_access_get($argument) -- get the 'access' settings * from cache. Must return array($access, $contexts); This callback can * perform access checking to make sure this URL is not being gamed. * - $module . _ctools_access_set($argument, $access) -- set the 'access' * settings in cache. * - $module . _ctools_access_clear($argument) -- clear the cache. * * The ctools_object_cache is recommended for this purpose, but you can use * any caching mechanism you like. An example: * * @code{ * ctools_include('object-cache'); * ctools_object_cache_set("$module:argument", $access); * } * * To utilize this form: * @code * ctools_include('context-access-admin'); * $form_state = array( * 'access' => $access, * 'module' => 'module name', * 'callback argument' => 'some string', * 'contexts' => $contexts, // an array of contexts. Optional if no contexts. * // 'logged-in-user' will be added if not present as the access system * // requires this context. * ), * $output = drupal_build_form('ctools_access_admin_form', $form_state); * if (!empty($form_state['executed'])) { * // save $form_state['access'] however you like. * } * @endcode * * Additionally, you may add 'no buttons' => TRUE if you wish to embed this * form into your own, and instead call * * @code{ * $form = ctools_access_admin_form($form, $form_state); * } * * You'll be responsible for adding a submit button. * * You may use ctools_access($access, $contexts) which will return * TRUE if access is passed or FALSE if access is not passed. */ /** * Administrative form for access control. */ function ctools_access_admin_form($form, &$form_state) { ctools_include('context'); $argument = isset($form_state['callback argument']) ? $form_state['callback argument'] : ''; $fragment = $form_state['module']; if ($argument) { $fragment .= '-' . $argument; } $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : array(); $form['access_table'] = array( '#markup' => ctools_access_admin_render_table($form_state['access'], $fragment, $contexts), ); $form['add-button'] = array( '#theme' => 'ctools_access_admin_add', ); // This sets up the URL for the add access modal. $form['add-button']['add-url'] = array( '#attributes' => array('class' => array("ctools-access-add-url")), '#type' => 'hidden', '#value' => url("ctools/context/ajax/access/add/$fragment", array('absolute' => TRUE)), ); $plugins = ctools_get_relevant_access_plugins($contexts); $options = array(); foreach ($plugins as $id => $plugin) { $options[$id] = $plugin['title']; } asort($options); $form['add-button']['type'] = array( // This ensures that the form item is added to the URL. '#attributes' => array('class' => array("ctools-access-add-url")), '#type' => 'select', '#options' => $options, '#required' => FALSE, ); $form['add-button']['add'] = array( '#type' => 'submit', '#attributes' => array('class' => array('ctools-use-modal')), '#id' => "ctools-access-add", '#value' => t('Add'), ); $form['logic'] = array( '#type' => 'radios', '#options' => array( 'and' => t('All criteria must pass.'), 'or' => t('Only one criteria must pass.'), ), '#default_value' => isset($form_state['access']['logic']) ? $form_state['access']['logic'] : 'and', ); if (empty($form_state['no buttons'])) { $form['buttons']['save'] = array( '#type' => 'submit', '#value' => t('Save'), '#submit' => array('ctools_access_admin_form_submit'), ); } return $form; } /** * Render the table. This is used both to render it initially and to rerender * it upon ajax response. */ function ctools_access_admin_render_table($access, $fragment, $contexts) { ctools_include('ajax'); ctools_include('modal'); $rows = array(); if (empty($access['plugins'])) { $access['plugins'] = array(); } foreach ($access['plugins'] as $id => $test) { $row = array(); $plugin = ctools_get_access_plugin($test['name']); $title = isset($plugin['title']) ? $plugin['title'] : t('Broken/missing access plugin %plugin', array('%plugin' => $test['name'])); $row[] = array('data' => $title, 'class' => array('ctools-access-title')); $description = ctools_access_summary($plugin, $contexts, $test); $row[] = array('data' => $description, 'class' => array('ctools-access-description')); $operations = ctools_modal_image_button(ctools_image_path('icon-configure.png'), "ctools/context/ajax/access/configure/$fragment/$id", t('Configure settings for this item.')); $operations .= ctools_ajax_image_button(ctools_image_path('icon-delete.png'), "ctools/context/ajax/access/delete/$fragment/$id", t('Remove this item.')); $row[] = array('data' => $operations, 'class' => array('ctools-access-operations'), 'align' => 'right'); $rows[] = $row; } $header = array( array('data' => t('Title'), 'class' => array('ctools-access-title')), array('data' => t('Description'), 'class' => array('ctools-access-description')), array('data' => '', 'class' => array('ctools-access-operations'), 'align' => 'right'), ); if (empty($rows)) { $rows[] = array(array('data' => t('No criteria selected, this test will pass.'), 'colspan' => count($header))); } ctools_modal_add_js(); return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'ctools-access-table'))); } /** * Theme the 'add' portion of the access form into a table. */ function theme_ctools_access_admin_add($vars) { $rows = array(array(drupal_render_children($vars['form']))); $output = '