Projet

Général

Profil

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

root / htmltest / sites / all / modules / flag / includes / flag / flag_entity.inc @ 018e218c

1
<?php
2

    
3
/**
4
 * @file
5
 *   Contains the flag_entity class.
6
 */
7

    
8
/**
9
 * Base entity flag handler.
10
 */
11
class flag_entity extends flag_flag {
12
  /**
13
   * Adds additional options that are common for all entity types.
14
   */
15
  function options() {
16
    $options = parent::options();
17
    $options += array(
18
      // Output the flag in the entity links.
19
      // This is empty for now and will get overriden for different
20
      // entities.
21
      // @see hook_entity_view().
22
      'show_in_links' => array(),
23
      // Output the flag as individual pseudofields.
24
      'show_as_field' => FALSE,
25
      // Add a checkbox for the flag in the entity form.
26
      // @see hook_field_attach_form().
27
      'show_on_form' => FALSE,
28
      'access_author' => '',
29
      'show_contextual_link' => FALSE,
30
    );
31
    return $options;
32
  }
33

    
34
  /**
35
   * Options form extras for the generic entity flag.
36
   */
37
  function options_form(&$form) {
38
    $bundles = array();
39
    $entity_info = entity_get_info($this->entity_type);
40
    foreach ($entity_info['bundles'] as $bundle_key => $bundle) {
41
      $bundles[$bundle_key] = check_plain($bundle['label']);
42
    }
43
    $form['access']['types'] = array(
44
      '#type' => 'checkboxes',
45
      '#title' => t('Bundles'),
46
      '#options' => $bundles,
47
      '#description' => t('Select the bundles that this flag may be used on. Leave blank to allow on all bundles for the entity type.'),
48
      '#default_value' => $this->types,
49
    );
50

    
51
    // Add checkboxes to show flag link on each entity view mode.
52
    $options = array();
53
    $defaults = array();
54
    foreach ($entity_info['view modes'] as $name => $view_mode) {
55
      $options[$name] = t('Display on @name view mode', array('@name' => $view_mode['label']));
56
      $defaults[$name] = !empty($this->show_in_links[$name]) ? $name : 0;
57
    }
58

    
59
    $form['display']['show_in_links'] = array(
60
      '#type' => 'checkboxes',
61
      '#title' => t('Display in entity links'),
62
      '#description' => t('Show the flag link with the other links on the entity.'),
63
      '#options' => $options,
64
      '#default_value' => $defaults,
65
    );
66

    
67
    $form['display']['show_as_field'] = array(
68
      '#type' => 'checkbox',
69
      '#title' => t('Display link as field'),
70
      '#description' => t('Show the flag link as a pseudofield, which can be ordered among other entity elements in the "Manage display" settings for the entity type.'),
71
      '#default_value' => isset($this->show_as_field) ? $this->show_as_field : TRUE,
72
    );
73
    if (empty($entity_info['fieldable'])) {
74
      $form['display']['show_as_field']['#disabled'] = TRUE;
75
      $form['display']['show_as_field']['#description'] = t("This entity type is not fieldable.");
76
    }
77

    
78
    $form['display']['show_on_form'] = array(
79
      '#type' => 'checkbox',
80
      '#title' => t('Display checkbox on entity edit form'),
81
      '#default_value' => $this->show_on_form,
82
      '#weight' => 5,
83
    );
84

    
85
    // We use FieldAPI to put the flag checkbox on the entity form, so therefore
86
    // require the entity to be fielable. Since this is a potential DX
87
    // headscratcher for a developer wondering where this option has gone,
88
    // we disable it and explain why.
89
    if (empty($entity_info['fieldable'])) {
90
      $form['display']['show_on_form']['#disabled'] = TRUE;
91
      $form['display']['show_on_form']['#description'] = t('This is only possible on entities which are fieldable.');
92
    }
93
    $form['display']['show_contextual_link'] = array(
94
      '#type' => 'checkbox',
95
      '#title' => t('Display in contextual links'),
96
      '#default_value' => $this->show_contextual_link,
97
      '#description' => t('Note that not all entity types support contextual links.'),
98
      '#access' => module_exists('contextual'),
99
      '#weight' => 10,
100
    );
101
  }
102

    
103
  /**
104
   * Loads the entity object.
105
   */
106
  function _load_entity($entity_id) {
107
    if (is_numeric($entity_id)) {
108
      $entity = entity_load($this->entity_type, array($entity_id));
109
      return reset($entity);
110
    }
111
    return NULL;
112
  }
113

    
114
  /**
115
   * Checks whether the flag applies for the current entity bundle.
116
   */
117
  function applies_to_entity($entity) {
118
    $entity_info = entity_get_info($this->entity_type);
119
    // The following conditions are applied:
120
    // - if the types array is empty, the flag applies to all bundles and thus
121
    //   to this entity.
122
    // - if the entity has no bundles, the flag applies to the entity.
123
    // - if the entity's bundle is in the list of types.
124
    if (empty($this->types) || empty($entity_info['entity keys']['bundle']) || in_array($entity->{$entity_info['entity keys']['bundle']}, $this->types)) {
125
      return TRUE;
126
    }
127
    return FALSE;
128
  }
129

    
130
  /**
131
   * Provides permissions for this flag.
132
   *
133
   * @return
134
   *  An array of permissions for hook_permission().
135
   */
136
  function get_permissions() {
137
    // For entity flags, use the human label of the entity.
138
    $entity_info = entity_get_info($this->entity_type);
139
    $entity_label = $entity_info['label'];
140
    return array(
141
      "flag $this->name" => array(
142
        'title' => t('Flag %entity entities as %flag_title', array(
143
          '%flag_title' => $this->title,
144
          '%entity' => $entity_label,
145
        )),
146
      ),
147
      "unflag $this->name" => array(
148
        'title' => t('Unflag %entity entities as %flag_title', array(
149
          '%flag_title' => $this->title,
150
          '%entity' => $entity_label,
151
        )),
152
      ),
153
    );
154
  }
155

    
156
  /**
157
   * Returns the entity id, if it already exists.
158
   */
159
  function get_entity_id($entity) {
160
    $entity_info = entity_get_info($this->entity_type);
161
    if ($entity && isset($entity->{$entity_info['entity keys']['id']})) {
162
      return $entity->{$entity_info['entity keys']['id']};
163
    }
164
  }
165

    
166
  /**
167
   * Determine whether the flag should show a flag link in entity links.
168
   */
169
  function shows_in_entity_links($view_mode) {
170
    // Check for settings for the given view mode.
171
    if (isset($this->show_in_links[$view_mode])) {
172
      return (bool) $this->show_in_links[$view_mode];
173
    }
174
    return FALSE;
175
  }
176

    
177
  /**
178
   * Returns token types for the current entity type.
179
   */
180
  function get_labels_token_types() {
181
    // The token type name might be different to the entity type name. If so,
182
    // an own flag entity handler can be used for overriding this.
183
    $entity_info = entity_get_info($this->entity_type);
184
    if (isset($entity_info['token type'])) {
185
      return array_merge(array($entity_info['token type']), parent::get_labels_token_types());
186
    }
187
    else {
188
      return array_merge(array($this->entity_type), parent::get_labels_token_types());
189
    }
190
  }
191

    
192
  /**
193
   * Replaces tokens.
194
   */
195
  function replace_tokens($label, $contexts, $options, $entity_id) {
196
    if ($entity_id && ($entity = $this->fetch_entity($entity_id))) {
197
      $contexts[$this->entity_type] = $entity;
198
    }
199
    return parent::replace_tokens($label, $contexts, $options, $entity_id);
200
  }
201

    
202
  /**
203
   * Returns a 'flag action' object.
204
   */
205
  function get_flag_action($entity_id) {
206
    $flag_action = parent::get_flag_action($entity_id);
207
    $entity = $this->fetch_entity($entity_id);
208
    $flag_action->content_title = entity_label($this->entity_type, $entity);
209
    $flag_action->content_url = $this->_flag_url($this->entity_type . '/' . $this->get_entity_id($entity));
210
    return $flag_action;
211
  }
212

    
213
  /**
214
   * Returns objects the action may possible need.
215
   */
216
  function get_relevant_action_objects($entity_id) {
217
    return array(
218
      $this->entity_type => $this->fetch_entity($entity_id),
219
    );
220
  }
221

    
222
  /**
223
   * Returns information for the Views integration.
224
   */
225
  function get_views_info() {
226
    $entity_info = entity_get_info($this->entity_type);
227

    
228
    if (!isset($entity_info['base table'])) {
229
      return NULL;
230
    }
231

    
232
    return array(
233
      'views table' => $entity_info['base table'],
234
      'join field' => $entity_info['entity keys']['id'],
235
      'title field' => isset($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : '',
236
      'title' => t('@entity_label flag', array('@entity_label' => $entity_info['label'])),
237
      'help' => t('Limit results to only those entity flagged by a certain flag; Or display information about the flag set on a entity.'),
238
      'counter title' => t('@entity_label flag counter', array('@entity_label' => $entity_info['label'])),
239
      'counter help' => t('Include this to gain access to the flag counter field.'),
240
    );
241
  }
242
}