Projet

Général

Profil

Paste
Télécharger (5,65 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_field / i18n_field.inc @ 76df55b7

1
<?php
2
/**
3
 * @file
4
 * Field and field instance object handlers
5
 */
6

    
7
/**
8
 * Base object for field and field instance
9
 */
10
class i18n_field_base extends i18n_string_object_wrapper {
11
  /**
12
   * Get base path for object
13
   */
14
  protected function get_base_path() {
15
    $info = entity_get_info($this->object['entity_type']);
16
    if (isset($info['bundles'][$this->object['bundle']]['admin'])) {
17
      $admin = $info['bundles'][$this->object['bundle']]['admin'];
18
      // Extract path information from the bundle.
19
      if (isset($admin['real path'])) {
20
        return $admin['real path'] . '/fields/' . $this->object['field_name'];
21
      }
22
      else {
23
        // We don't have real path, use path instead, may work or not.
24
        return $admin['path'] . '/fields/' . $this->object['field_name'];
25
      }
26
    }
27
  }
28
}
29

    
30
/**
31
 * Field object
32
 */
33
class i18n_field extends i18n_field_base {
34
  /**
35
   * Class constructor
36
   *
37
   * For convenience field objects can be built from field info and from field instance.
38
   */
39
  public function __construct($type, $key, $object) {
40
    parent::__construct($type, $key, $object);
41

    
42
    // If this is a field instance, get field info but add instance data too.
43
    // This instance data will be used to get the paths to get the edit/translate path.
44
    if (isset($this->object['bundle']) && isset($this->object['entity_type'])) {
45
      $this->object = field_info_field($this->object['field_name']) + array('bundle' => $this->object['bundle'], 'entity_type' => $object['entity_type']);
46
    }
47
  }
48

    
49
  /**
50
   * Get edit path for object
51
   */
52
  public function get_edit_path() {
53
    return $this->get_base_path() . '/field-settings';
54
  }
55

    
56
  /**
57
   * Get translate path for object
58
   */
59
  public function get_translate_path($langcode = NULL) {
60
    return $this->get_base_path() . '/translate/field' . ($langcode ? '/' . $langcode : '');
61
  }
62
  /**
63
   * Get string context
64
   */
65
  public function get_string_context() {
66
    return array($this->object['field_name'], '#field');
67
  }
68
  /**
69
   * Get translatable properties
70
   */
71
  protected function build_properties() {
72
    $properties = parent::build_properties();
73
    $object = $this->object;
74
    // For select fields field:field_name
75
    if (!empty($object['settings']['allowed_values']) && i18n_field_type_info($object['type'], 'translate_options')) {
76
      //return array('field', $field['field_name'], '#allowed_values');
77
      foreach ($object['settings']['allowed_values'] as $key => $value) {
78
        $properties[$this->get_textgroup()][$object['field_name']]['#allowed_values'][$key] = array(
79
          'title' => t('Option %name', array('%name' => $value)),
80
          'string' => $value,
81
        );
82
      }
83
    }
84
    return $properties;
85
  }
86

    
87
  /**
88
   * Context to be pre-loaded before translation.
89
   */
90
  protected function get_translate_context($langcode, $options) {
91
    return array(
92
      $this->object['field_name'],
93
      array('#field', '#allowed_values'),
94
      '*'
95
    );
96
  }
97

    
98
  /**
99
   * Set field translation for object.
100
   *
101
   * Mot often, this is a direct field set, but sometimes fields may have different formats.
102
   *
103
   * @param $object
104
   *   A clone of the object or array. Field instance.
105
   */
106
  protected function translate_field(&$object, $i18nstring, $langcode, $options) {
107
    if ($i18nstring->objectid == '#allowed_values') {
108
      $object['settings']['#allowed_values'][$i18nstring->key] = $i18nstring->format_translation($langcode, $options);
109
    }
110
    else {
111
      parent::translate_field($object, $i18nstring, $langcode, $options);
112
    }
113
  }
114
}
115

    
116
/**
117
 * Field instance object
118
 */
119
class i18n_field_instance extends i18n_field_base {
120
  /**
121
   * Get edit path for object
122
   */
123
  public function get_edit_path() {
124
    return $this->get_base_path();
125
  }
126
  /**
127
   * Get translate path for object
128
   */
129
  public function get_translate_path($langcode = NULL) {
130
    return $this->get_base_path() . '/translate' . ($langcode ? '/' . $langcode : '');
131
  }
132
  /**
133
   * Get string context
134
   */
135
  public function get_string_context() {
136
    return array($this->object['field_name'], $this->object['bundle']);
137
  }
138
  /**
139
   * Get translatable properties
140
   */
141
  protected function build_properties() {
142
    $properties = parent::build_properties();
143
    $object = $this->object;
144
    $field = field_info_field($object['field_name']);
145
    // Only for text field types
146
    if (!empty($object['default_value']) && i18n_field_type_info($field['type'], 'translate_default')) {
147
      $format = isset($object['default_value'][0]['format']) ? $object['default_value'][0]['format'] : NULL;
148
      $properties[$this->get_textgroup()][$object['field_name']][$object['bundle']]['default_value']['string'] = $object['default_value'][0]['value'];
149
      $properties[$this->get_textgroup()][$object['field_name']][$object['bundle']]['default_value']['format'] = $format;
150
    }
151
    return $properties;
152
  }
153

    
154
  /**
155
   * Set field translation for object.
156
   *
157
   * Mot often, this is a direct field set, but sometimes fields may have different formats.
158
   *
159
   * @param $object
160
   *   A clone of the object or array. Field instance.
161
   */
162
  protected function translate_field(&$object, $i18nstring, $langcode, $options) {
163
    if ($i18nstring->property == 'default_value') {
164
      // Render string without applying format
165
      $object['default_value'][0]['value'] = $i18nstring->format_translation($langcode, array('sanitize' => FALSE) + $options);
166
    }
167
    else {
168
      parent::translate_field($object, $i18nstring, $langcode, $options);
169
    }
170
  }
171

    
172
  /**
173
   * Context to be pre-loaded before translation.
174
   */
175
  protected function get_translate_context($langcode, $options) {
176
    return array(
177
      $this->object['field_name'],
178
      array('#field', '#allowed_values', $this->object['bundle']),
179
      '*'
180
    );
181
  }
182
}