t('Check a context\'s field'), 'description' => t('Control access by performing certain checks on a field in a context.'), 'callback' => 'ctools_context_field_ctools_access_check', 'settings form' => 'ctools_context_field_ctools_access_settings', 'summary' => 'ctools_context_field_ctools_access_summary', 'all contexts' => TRUE, ); } /** * Settings form for the 'by role' access plugin. */ function ctools_context_field_ctools_access_settings(&$form, &$form_state, $conf) { $fields = array(); foreach ($form_state['contexts'] as $key => $context) { foreach ($context->data as $k => $v) { $fields[$key . '.' . $k] = '(' . $context->identifier . ') ' . $k; } } $form['settings']['which_field'] = array( '#type' => 'select', '#title' => t('Which context and field?'), '#options' => $fields, '#default_value' => $conf['which_field'], ); $form['settings']['operator'] = array( '#type' => 'radios', '#title' => t('Operator to perform on the field'), '#options' => array( 'field length is greater than' => t('Field length is greater than'), 'field length is equal to' => t('Field length is equal to'), 'field length is less than' => t('Field length is less than'), 'field equals' => t('Field is set to'), 'field does not equal' => t('Field is NOT set to'), ), '#default_value' => $conf['operator'], ); $form['settings']['operator_arg'] = array( '#type' => 'textfield', '#title' => t('Argument to the operator'), '#description' => t('For example, greater than what? Set to what?'), '#default_value' => $conf['operator_arg'], ); } /** * Check for access. */ function ctools_context_field_ctools_access_check($conf, $contexts) { // As far as I know there should always be a context at this point, but this // is safe. if (empty($contexts)) { return FALSE; } list($context, $field) = explode('.', $conf['which_field']); switch ($conf['operator']) { case 'field length is greater than': if (drupal_strlen($contexts[$context]->data->$field) <= $conf['operator_arg']) { return FALSE; } break; case 'field length is equal to': if (drupal_strlen($contexts[$context]->data->$field) != $conf['operator_arg']) { return FALSE; } break; case 'field length is less than': if (drupal_strlen($contexts[$context]->data->$field) >= $conf['operator_arg']) { return FALSE; } break; case 'field equals': if ($contexts[$context]->data->$field != $conf['operator_arg']) { return FALSE; } break; case 'field does not equal': if ($contexts[$context]->data->$field == $conf['operator_arg']) { return FALSE; } break; default: break; } return TRUE; } /** * Provide a summary description based upon the checked roles. */ function ctools_context_field_ctools_access_summary($conf, $context) { list($context_id, $field) = explode('.', $conf['which_field']); $op_str = str_replace('field', 'field %field', $conf['operator']); return t('context %context ' . $op_str . ' "!operator_arg"', array('%context' => $context_id, '%field' => $field, '!operator_arg' => $conf['operator_arg']) ); }