Projet

Général

Profil

Paste
Télécharger (2,17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / ctools_plugin_example / plugins / access / arg_length.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Plugin to provide access control/visibility based on length of
6
 * simplecontext argument (in URL).
7
 */
8

    
9
/**
10
 * Plugins are described by creating a $plugin array which will be used
11
 * by the system that includes this file.
12
 */
13
$plugin = array(
14
  'title' => t("Arg length"),
15
  'description' => t('Control access by length of simplecontext argument.'),
16
  'callback' => 'ctools_plugin_example_arg_length_ctools_access_check',
17
  'settings form' => 'ctools_plugin_example_arg_length_ctools_access_settings',
18
  'summary' => 'ctools_plugin_example_arg_length_ctools_access_summary',
19
  'required context' => new ctools_context_required(t('Simplecontext'), 'simplecontext'),
20
);
21

    
22
/**
23
 * Settings form for the 'by role' access plugin.
24
 */
25
function ctools_plugin_example_arg_length_ctools_access_settings(&$form, &$form_state, $conf) {
26
  $form['settings']['greater_than'] = array(
27
    '#type' => 'radios',
28
    '#title' => t('Grant access if simplecontext argument length is'),
29
    '#options' => array(1 => t('Greater than'), 0 => t('Less than or equal to')),
30
    '#default_value' => $conf['greater_than'],
31
  );
32
  $form['settings']['arg_length'] = array(
33
    '#type' => 'textfield',
34
    '#title' => t('Length of simplecontext argument'),
35
    '#size' => 3,
36
    '#default_value' => $conf['arg_length'],
37
    '#description' => t('Access/visibility will be granted based on arg length.'),
38
  );
39
}
40

    
41
/**
42
 * Check for access.
43
 */
44
function ctools_plugin_example_arg_length_ctools_access_check($conf, $context) {
45
  // As far as I know there should always be a context at this point, but this
46
  // is safe.
47
  if (empty($context) || empty($context->data)) {
48
    return FALSE;
49
  }
50
  $compare = ($context->arg_length > $conf['arg_length']);
51
  if (($compare && $conf['greater_than']) || (!$compare && !$conf['greater_than'])) {
52
    return TRUE;
53
  }
54
  return FALSE;
55
}
56

    
57
/**
58
 * Provide a summary description based upon the checked roles.
59
 */
60
function ctools_plugin_example_arg_length_ctools_access_summary($conf, $context) {
61
  return t('Simpletext argument must be !comp @length characters',
62
    array(
63
      '!comp' => $conf['greater_than'] ? 'greater than' : 'less than or equal to',
64
      '@length' => $conf['arg_length'],
65
    ));
66
}