Projet

Général

Profil

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

root / drupal7 / sites / all / modules / entity / includes / entity.inc @ 503b3f7b

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides a base class for entities.
6
 */
7

    
8
/**
9
 * A common class for entities.
10
 *
11
 * It's suggested, but not required, to extend this class and to override
12
 * __construct() in order to specify a fixed entity type.
13
 *
14
 * For providing an entity label and URI it is suggested to override the
15
 * defaultLabel() and defaultUri() methods, and to specify the
16
 * entity_class_label() and entity_class_uri() as respective callbacks in
17
 * hook_entity_info(). That way modules are able to override your defaults
18
 * by altering the hook_entity_info() callbacks, while $entity->label() and
19
 * $entity->uri() reflect this changes as well.
20
 *
21
 * Defaults for entity properties can be easily defined by adding class
22
 * properties, e.g.:
23
 * @code
24
 *   public $name = '';
25
 *   public $count = 0;
26
 * @endcode
27
 */
28
class Entity {
29

    
30
  protected $entityType;
31
  protected $entityInfo;
32
  protected $idKey, $nameKey, $statusKey;
33
  protected $defaultLabel = FALSE;
34

    
35
  /**
36
   * Creates a new entity.
37
   *
38
   * @see entity_create()
39
   */
40
  public function __construct(array $values = array(), $entityType = NULL) {
41
    if (empty($entityType)) {
42
      throw new Exception('Cannot create an instance of Entity without a specified entity type.');
43
    }
44
    $this->entityType = $entityType;
45
    $this->setUp();
46
    // Set initial values.
47
    foreach ($values as $key => $value) {
48
      $this->$key = $value;
49
    }
50
  }
51

    
52
  /**
53
   * Set up the object instance on construction or unserializiation.
54
   */
55
  protected function setUp() {
56
    $this->entityInfo = entity_get_info($this->entityType);
57
    $this->idKey = $this->entityInfo['entity keys']['id'];
58
    $this->nameKey = isset($this->entityInfo['entity keys']['name']) ? $this->entityInfo['entity keys']['name'] : $this->idKey;
59
    $this->statusKey = empty($this->entityInfo['entity keys']['status']) ? 'status' : $this->entityInfo['entity keys']['status'];
60
  }
61

    
62
  /**
63
   * Returns the internal, numeric identifier.
64
   *
65
   * Returns the numeric identifier, even if the entity type has specified a
66
   * name key. In the latter case, the numeric identifier is supposed to be used
67
   * when dealing generically with entities or internally to refer to an entity,
68
   * i.e. in a relational database. If unsure, use Entity:identifier().
69
   */
70
  public function internalIdentifier() {
71
    return isset($this->{$this->idKey}) ? $this->{$this->idKey} : NULL;
72
  }
73

    
74
  /**
75
   * Returns the entity identifier, i.e. the entities name or numeric id.
76
   *
77
   * @return
78
   *   The identifier of the entity. If the entity type makes use of a name key,
79
   *   the name is returned, else the numeric id.
80
   *
81
   * @see entity_id()
82
   */
83
  public function identifier() {
84
    return isset($this->{$this->nameKey}) ? $this->{$this->nameKey} : NULL;
85
  }
86

    
87
  /**
88
   * Returns the info of the type of the entity.
89
   *
90
   * @see entity_get_info()
91
   */
92
  public function entityInfo() {
93
    return $this->entityInfo;
94
  }
95

    
96
  /**
97
   * Returns the type of the entity.
98
   */
99
  public function entityType() {
100
    return $this->entityType;
101
  }
102

    
103
  /**
104
   * Returns the bundle of the entity.
105
   *
106
   * @return
107
   *   The bundle of the entity. Defaults to the entity type if the entity type
108
   *   does not make use of different bundles.
109
   */
110
  public function bundle() {
111
    return !empty($this->entityInfo['entity keys']['bundle']) ? $this->{$this->entityInfo['entity keys']['bundle']} : $this->entityType;
112
  }
113

    
114
  /**
115
   * Returns the label of the entity.
116
   *
117
   * Modules may alter the label by specifying another 'label callback' using
118
   * hook_entity_info_alter().
119
   *
120
   * @see entity_label()
121
   */
122
  public function label() {
123
    // If the default label flag is enabled, this is being invoked recursively.
124
    // In this case we need to use our default label callback directly. This may
125
    // happen if a module provides a label callback implementation different
126
    // from ours, but then invokes Entity::label() or entity_class_label() from
127
    // there.
128
    if ($this->defaultLabel || (isset($this->entityInfo['label callback']) && $this->entityInfo['label callback'] == 'entity_class_label')) {
129
      return $this->defaultLabel();
130
    }
131
    $this->defaultLabel = TRUE;
132
    $label = entity_label($this->entityType, $this);
133
    $this->defaultLabel = FALSE;
134
    return $label;
135
  }
136

    
137
  /**
138
   * Defines the entity label if the 'entity_class_label' callback is used.
139
   *
140
   * Specify 'entity_class_label' as 'label callback' in hook_entity_info() to
141
   * let the entity label point to this method. Override this in order to
142
   * implement a custom default label.
143
   */
144
  protected function defaultLabel() {
145
    // Add in the translated specified label property.
146
    return $this->getTranslation($this->entityInfo['entity keys']['label']);
147
  }
148

    
149
  /**
150
   * Returns the uri of the entity just as entity_uri().
151
   *
152
   * Modules may alter the uri by specifying another 'uri callback' using
153
   * hook_entity_info_alter().
154
   *
155
   * @see entity_uri()
156
   */
157
  public function uri() {
158
    if (isset($this->entityInfo['uri callback']) && $this->entityInfo['uri callback'] == 'entity_class_uri') {
159
      return $this->defaultUri();
160
    }
161
    return entity_uri($this->entityType, $this);
162
  }
163

    
164
  /**
165
   * Override this in order to implement a custom default URI and specify
166
   * 'entity_class_uri' as 'uri callback' hook_entity_info().
167
   */
168
  protected function defaultUri() {
169
    return array('path' => 'default/' . $this->identifier());
170
  }
171

    
172
  /**
173
   * Checks if the entity has a certain exportable status.
174
   *
175
   * @param $status
176
   *   A status constant, i.e. one of ENTITY_CUSTOM, ENTITY_IN_CODE,
177
   *   ENTITY_OVERRIDDEN or ENTITY_FIXED.
178
   *
179
   * @return
180
   *   For exportable entities TRUE if the entity has the status, else FALSE.
181
   *   In case the entity is not exportable, NULL is returned.
182
   *
183
   * @see entity_has_status()
184
   */
185
  public function hasStatus($status) {
186
    if (!empty($this->entityInfo['exportable'])) {
187
      return isset($this->{$this->statusKey}) && ($this->{$this->statusKey} & $status) == $status;
188
    }
189
  }
190

    
191
  /**
192
   * Permanently saves the entity.
193
   *
194
   * @see entity_save()
195
   */
196
  public function save() {
197
    return entity_get_controller($this->entityType)->save($this);
198
  }
199

    
200
  /**
201
   * Permanently deletes the entity.
202
   *
203
   * @see entity_delete()
204
   */
205
  public function delete() {
206
    $id = $this->identifier();
207
    if (isset($id)) {
208
      entity_get_controller($this->entityType)->delete(array($id));
209
    }
210
  }
211

    
212
  /**
213
   * Exports the entity.
214
   *
215
   * @see entity_export()
216
   */
217
  public function export($prefix = '') {
218
    return entity_get_controller($this->entityType)->export($this, $prefix);
219
  }
220

    
221
  /**
222
   * Generate an array for rendering the entity.
223
   *
224
   * @see entity_view()
225
   */
226
  public function view($view_mode = 'full', $langcode = NULL, $page = NULL) {
227
    return entity_get_controller($this->entityType)->view(array($this), $view_mode, $langcode, $page);
228
  }
229

    
230
  /**
231
   * Builds a structured array representing the entity's content.
232
   *
233
   * @see entity_build_content()
234
   */
235
  public function buildContent($view_mode = 'full', $langcode = NULL) {
236
    return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode);
237
  }
238

    
239
  /**
240
   * Gets the raw, translated value of a property or field.
241
   *
242
   * Supports retrieving field translations as well as i18n string translations.
243
   *
244
   * Note that this returns raw data values, which might not reflect what
245
   * has been declared for hook_entity_property_info() as no 'getter callbacks'
246
   * are invoked or no referenced entities are loaded. For retrieving values
247
   * reflecting the property info make use of entity metadata wrappers, see
248
   * entity_metadata_wrapper().
249
   *
250
   * @param $property_name
251
   *   The name of the property to return; e.g., 'title'.
252
   * @param $langcode
253
   *   (optional) The language code of the language to which the value should
254
   *   be translated. If set to NULL, the default display language is being
255
   *   used.
256
   *
257
   * @return
258
   *   The raw, translated property value; or the raw, un-translated value if no
259
   *   translation is available.
260
   *
261
   * @todo Implement an analogous setTranslation() method for updating.
262
   */
263
  public function getTranslation($property, $langcode = NULL) {
264
    $all_info = entity_get_all_property_info($this->entityType);
265
    // Assign by reference to avoid triggering notices if metadata is missing.
266
    $property_info = &$all_info[$property];
267

    
268
    if (!empty($property_info['translatable'])) {
269
      if (!empty($property_info['field'])) {
270
        return field_get_items($this->entityType, $this, $property, $langcode);
271
      }
272
      elseif (!empty($property_info['i18n string'])) {
273
        $name = $this->entityInfo['module'] . ':' . $this->entityType . ':' . $this->identifier() . ':' . $property;
274
        return entity_i18n_string($name, $this->$property, $langcode);
275
      }
276
    }
277
    return $this->$property;
278
  }
279

    
280
  /**
281
   * Checks whether the entity is the default revision.
282
   *
283
   * @return Boolean
284
   *
285
   * @see entity_revision_is_default()
286
   */
287
  public function isDefaultRevision() {
288
    if (!empty($this->entityInfo['entity keys']['revision'])) {
289
      $key = !empty($this->entityInfo['entity keys']['default revision']) ? $this->entityInfo['entity keys']['default revision'] : 'default_revision';
290
      return !empty($this->$key);
291
    }
292
    return TRUE;
293
  }
294

    
295
  /**
296
   * Magic method to only serialize what's necessary.
297
   */
298
  public function __sleep() {
299
    $vars = get_object_vars($this);
300
    unset($vars['entityInfo'], $vars['idKey'], $vars['nameKey'], $vars['statusKey']);
301
    // Also key the returned array with the variable names so the method may
302
    // be easily overridden and customized.
303
    return drupal_map_assoc(array_keys($vars));
304
  }
305

    
306
  /**
307
   * Magic method to invoke setUp() on unserialization.
308
   */
309
  public function __wakeup() {
310
    $this->setUp();
311
  }
312
}
313

    
314
/**
315
 * These classes are deprecated by "Entity" and are only here for backward
316
 * compatibility reasons.
317
 */
318
class EntityDB extends Entity {}
319
class EntityExtendable extends Entity {}
320
class EntityDBExtendable extends Entity {}