Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / plugins / contexts / language.inc @ 6e3ce7c2

1
<?php
2

    
3
/**
4
 * @file
5
 * Ctools context type plugin to hold the current language context.
6
 */
7

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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