Projet

Général

Profil

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

root / drupal7 / sites / all / modules / flag / includes / flag / flag_entity.inc @ b08d2851

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
    $entity_view_modes = $entity_info['view modes'];
55
    foreach ($entity_view_modes as $name => $view_mode) {
56
      $options[$name] = t('Display on @name view mode', array('@name' => $view_mode['label']));
57
      $defaults[$name] = !empty($this->show_in_links[$name]) ? $name : 0;
58
    }
59
    // Select the first display option by default if this is a new flag.
60
    if (empty($this->fid)) {
61
      $first_view_mode = reset(array_keys($entity_view_modes));
62
      $defaults[$first_view_mode] = $first_view_mode;
63
    }
64

    
65
    $form['display']['show_in_links'] = array(
66
      '#type' => 'checkboxes',
67
      '#title' => t('Display in entity links'),
68
      '#description' => t('Show the flag link with the other links on the entity.'),
69
      '#options' => $options,
70
      '#default_value' => $defaults,
71
    );
72

    
73
    $form['display']['show_as_field'] = array(
74
      '#type' => 'checkbox',
75
      '#title' => t('Display link as field'),
76
      '#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.'),
77
      '#default_value' => isset($this->show_as_field) ? $this->show_as_field : TRUE,
78
    );
79
    if (empty($entity_info['fieldable'])) {
80
      $form['display']['show_as_field']['#disabled'] = TRUE;
81
      $form['display']['show_as_field']['#description'] = t("This entity type is not fieldable.");
82
    }
83

    
84
    $form['display']['show_on_form'] = array(
85
      '#type' => 'checkbox',
86
      '#title' => t('Display checkbox on entity edit form'),
87
      '#default_value' => $this->show_on_form,
88
      '#weight' => 5,
89
    );
90

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

    
109
  /**
110
   * Loads the entity object.
111
   */
112
  function _load_entity($entity_id) {
113
    if (is_numeric($entity_id)) {
114
      $entity = entity_load($this->entity_type, array($entity_id));
115
      return reset($entity);
116
    }
117
    return NULL;
118
  }
119

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

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

    
162
  /**
163
   * Returns the entity id, if it already exists.
164
   */
165
  function get_entity_id($entity) {
166
    $entity_info = entity_get_info($this->entity_type);
167
    if ($entity && isset($entity->{$entity_info['entity keys']['id']})) {
168
      return $entity->{$entity_info['entity keys']['id']};
169
    }
170
  }
171

    
172
  /**
173
   * Determine whether the flag should show a flag link in entity links.
174
   */
175
  function shows_in_entity_links($view_mode) {
176
    // Check for settings for the given view mode.
177
    if (isset($this->show_in_links[$view_mode])) {
178
      return (bool) $this->show_in_links[$view_mode];
179
    }
180
    return FALSE;
181
  }
182

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

    
198
  /**
199
   * Replaces tokens.
200
   */
201
  function replace_tokens($label, $contexts, $options, $entity_id) {
202
    if ($entity_id && ($entity = $this->fetch_entity($entity_id))) {
203
      $contexts[$this->entity_type] = $entity;
204
    }
205
    return parent::replace_tokens($label, $contexts, $options, $entity_id);
206
  }
207

    
208
  /**
209
   * Returns a 'flag action' object.
210
   */
211
  function get_flag_action($entity_id) {
212
    $flag_action = parent::get_flag_action($entity_id);
213
    $entity = $this->fetch_entity($entity_id);
214
    $flag_action->content_title = entity_label($this->entity_type, $entity);
215
    $flag_action->content_url = $this->_flag_url($this->entity_type . '/' . $this->get_entity_id($entity));
216
    return $flag_action;
217
  }
218

    
219
  /**
220
   * Returns objects the action may possible need.
221
   */
222
  function get_relevant_action_objects($entity_id) {
223
    return array(
224
      $this->entity_type => $this->fetch_entity($entity_id),
225
    );
226
  }
227

    
228
  /**
229
   * Returns information for the Views integration.
230
   */
231
  function get_views_info() {
232
    $entity_info = entity_get_info($this->entity_type);
233

    
234
    if (!isset($entity_info['base table'])) {
235
      return NULL;
236
    }
237

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