Projet

Général

Profil

Paste
Télécharger (6,04 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / language.inc @ 7e72b748

1
<?php
2
/**
3
 * @file
4
 * Ctools context type plugin to hold the current language context.
5
 */
6

    
7
/**
8
 * Plugins are described by creating a $plugin array which will be used
9
 * by the system that includes this file.
10
 */
11
$plugin = array(
12
  'title' => t('Language'),
13
  'description' => t('Language object.'),
14
  'context' => 'ctools_context_language_create',
15
  'context name' => 'language',
16
  'keyword' => 'language',
17

    
18
  // Provides a list of items which are exposed as keywords.
19
  'convert list' => 'ctools_language_context_convert_list',
20
  // Convert keywords into data.
21
  'convert' => 'ctools_language_context_convert',
22

    
23
  'placeholder form' => array(
24
    '#type' => 'textfield',
25
    '#description' => t('Enter a valid langcode.'),
26
    '#value' => $GLOBALS['language']->language,
27
  ),
28

    
29
  // Provide settings for the context.
30
  'edit form' => 'ctools_context_language_settings_form',
31
  'settings' => ctools_context_language_conf_defaults(),
32
);
33

    
34
/**
35
 * Ensures a full populated settings array with sane defaults.
36
 *
37
 * @param mixed $conf
38
 *   Array with the user defined settings, or a string identifying a language.
39
 *
40
 * @return array
41
 *   Array with all available settings.
42
 */
43
function ctools_context_language_conf_defaults( $conf = array()) {
44
  if ( ! is_array($conf) ) {
45
    $conf = array(
46
      'preset_langcode' => (string) $conf,
47
    );
48
  }
49

    
50
  return $conf + array(
51
    'enable_cache_argument' => TRUE,
52
    'language_type' => 'language',
53
    'preset_langcode' => $GLOBALS['language']->language,
54
  );
55
}
56

    
57
/**
58
 * Create a context, either from manual configuration or the current language.
59
 */
60
function ctools_context_language_create($empty, $data = NULL, $conf = FALSE) {
61
  $context = new ctools_context('language');
62
  $context->plugin = 'language';
63
  if ($empty) {
64
    return $context;
65
  }
66
  $context->title = t('Language');
67

    
68
  $settings = ctools_context_language_conf_defaults($data);
69
  if ($settings['language_type'] != 'preset') {
70
    $language_object = $GLOBALS[$settings['language_type']];
71
  }
72
  else {
73
    // Fetch the enabled language objects.
74
    $languages = language_list('enabled');
75
    $languages = $languages[1];
76

    
77
    // Set the custom language, but fallback to the interface language.
78
    $language_object = $GLOBALS['language'];
79
    if (isset($languages[$settings['preset_langcode']])) {
80
      $language_object = $languages[$settings['preset_langcode']];
81
    }
82
  }
83
  // If enabled set the argument ot use in the cid.
84
  if ($settings['enable_cache_argument']) {
85
    $context->argument = $language_object->language;
86
  }
87
  $context->data = $language_object;
88
  return $context;
89
}
90

    
91
/**
92
 * Provide a list of sub-keywords.
93
 *
94
 * This is used to provide keywords from the context for use in a content type,
95
 * pane, etc.
96
 */
97
function ctools_language_context_convert_list() {
98
  $context = new stdClass();
99
  $context->data = $GLOBALS['language'];
100
  return array(
101
    'language' => t('Langcode. E.g. !example', array('!example' => ctools_language_context_convert($context, 'language'))),
102
    'name' => t('Name. E.g. !example', array('!example' => ctools_language_context_convert($context, 'name'))),
103
    'native' => t('Native name of the language. E.g. !example', array('!example' => ctools_language_context_convert($context, 'native'))),
104
    'direction' => t('Text direction 0=LRT, 1=RTL. E.g. !example', array('!example' => ctools_language_context_convert($context, 'direction'))),
105
    'enabled' => t('Status. E.g. !example', array('!example' => ctools_language_context_convert($context, 'enabled'))),
106
    'plurals' => t('Number of plural forms. E.g. !example', array('!example' => ctools_language_context_convert($context, 'plurals'))),
107
    'formula' => t('Plural formula. E.g. !example', array('!example' => ctools_language_context_convert($context, 'formula'))),
108
    'domain' => t('Domain prefix. E.g. !example', array('!example' => ctools_language_context_convert($context, 'domain'))),
109
    'prefix' => t('Url prefix . E.g. !example', array('!example' => ctools_language_context_convert($context, 'prefix'))),
110
    'weight' => t('The weight. E.g. !example', array('!example' => ctools_language_context_convert($context, 'weight'))),
111
    'javascript' => t('Key of the javascript file with the translations. E.g. !example', array('!example' => ctools_language_context_convert($context, 'javascript'))),
112
    'provider' => t('Negotiation method that defined the language. E.g. !example', array('!example' => ctools_language_context_convert($context, 'provider'))),
113
  );
114
}
115

    
116
/**
117
 * Convert a context property into a string to be used as a keyword.
118
 */
119
function ctools_language_context_convert($context, $type) {
120
  if (isset($context->data->$type)) {
121
    return $context->data->$type;
122
  }
123
}
124

    
125
/**
126
 * Settings form.
127
 */
128
function ctools_context_language_settings_form($form, &$form_state) {
129
  $conf = ctools_context_language_conf_defaults($form_state['conf']);
130

    
131
  $form['enable_cache_argument'] = array(
132
    '#title' => t('Add language to cache id'),
133
    '#description' => t('If enabled the langcode will be part of context aware caches.'),
134
    '#type' => 'checkbox',
135
    '#default_value' => $conf['enable_cache_argument'],
136
  );
137

    
138
  // Prepare language type options.
139
  $language_type_options = drupal_map_assoc( language_types() );
140
  $language_type_options['preset'] = t('Custom');
141

    
142
  $form['language_type'] = array(
143
    '#title' => t('The language type to use'),
144
    '#type' => 'radios',
145
    '#required' => TRUE,
146
    '#options' => $language_type_options,
147
    '#default_value' => $conf['language_type'],
148
  );
149

    
150
  ctools_include('language');
151
  $language_options = ctools_language_list();
152
  $form['preset_langcode'] = array(
153
    '#title' => t('Language'),
154
    '#type' => 'select',
155
    '#options' => $language_options,
156
    '#default_value' => $conf['preset_langcode'],
157
    '#states' => array(
158
      'visible' => array(
159
        ':input[name="language_type"]' => array('value' => 'preset'),
160
      ),
161
    ),
162
  );
163

    
164
  if (!empty($conf['preset_langcode']) && !isset($language_options[$conf['preset_langcode']])) {
165
    drupal_set_message(t('The currently selected language %langcode is no longer available.', array('%langcode' => $conf['preset_langcode'])), 'error', FALSE);
166
  }
167
  return $form;
168
}