foldershare-8.x-1.2/foldershare.tokens.inc

foldershare.tokens.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
 
/**
 * @file
 * Implements token hooks for the module.
 */
 
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Utility\Token;
 
use Drupal\foldershare\Constants;
use Drupal\foldershare\Entity\FolderShare;
 
/**
 * Implements hook_token_info().
 *
 * This hook defines tokens in the 'foldershare' group that provide access
 * to useful public fields of the FolderShare entity. These include:
 * - 'id': the entity ID.
 * - 'kind': the entity kind, such as 'file' or 'folder'.
 * - 'owner': the owner's user ID.
 * - 'name': the name of the entity.
 * - 'description': the description of the entity.
 * - 'created': the creation date of the entity.
 * - 'changed': the last-modified date of the entity.
 * - 'size': the size, in bytes, of the entity.
 * - 'url': the URL to the entity's view page.
 */
function foldershare_token_info() {
  //
  // Define the 'foldershare' token type that replaces tokens
  // with values from a FolderShare entity.
  $folderShareType = [
    'name'        => t('Shared files and folders'),
    'description' => t('Tokens related to a shared file or folder.'),
    'needs-data'  => FolderShare::ENTITY_TYPE_ID,
  ];
 
  //
  // Entity ID as 'id'.
  $folderShareTokens['id'] = [
    'name'        => t('ID'),
    'description' => t('The unique ID of a file or folder.'),
  ];
 
  // Entity kind as 'kind'.
  $folderShareTokens['kind'] = [
    'name'        => t('Kind'),
    'description' => t('The kind of item, such as a file or folder.'),
  ];
 
  // User ID as 'owner'.
  $folderShareTokens['owner'] = [
    'name'        => t('Owner'),
    'description' => t('The user who owns the file or folder.'),
    'type'        => 'user',
  ];
 
  // Entity name as 'name'.
  $folderShareTokens['name'] = [
    'name'        => t('Name'),
    'description' => t('The name of the file or folder.'),
  ];
 
  // Entity description as 'description'.
  $folderShareTokens['description'] = [
    'name'        => t('Description'),
    'description' => t('The description of the file or folder.'),
  ];
 
  // Entity creation date as 'created'.
  $folderShareTokens['created'] = [
    'name'        => t('Created'),
    'description' => t('The date the file or folder was created.'),
    'type'        => 'date',
  ];
 
  // Entity modified date as 'changed'.
  $folderShareTokens['changed'] = [
    'name'        => t('Changed'),
    'description' => t('The date the file or folder was most recently changed.'),
    'type'        => 'date',
  ];
 
  // Entity size as 'size'.
  $folderShareTokens['size'] = [
    'name'        => t('Size'),
    'description' => t('The storage space used by the file or folder, including all folder content.'),
  ];
 
  // Entity URL as 'url'.
  $folderShareTokens['url'] = [
    'name'        => t('URL'),
    'description' => t('The URL of the file or folder.'),
  ];
 
  // Entity parent as 'parent'.
  $folderShareTokens['parent'] = [
    'name'        => t('Parent folder'),
    'description' => t('The name of the parent folder, if any.'),
  ];
 
  // Entity root as 'root'.
  $folderShareTokens['root'] = [
    'name'        => t('Top-level item'),
    'description' => t('The name of the top-level item, if any.'),
  ];
 
  return [
    'types' => [
      Constants::FOLDERSHARE_TOKENS => $folderShareType,
    ],
    'tokens' => [
      Constants::FOLDERSHARE_TOKENS => $folderShareTokens,
    ],
  ];
}
 
/**
 * Implements hook_tokens().
 */
function foldershare_tokens(
  string $group,
  array $tokens,
  array $data,
  array $options,
  BubbleableMetadata $metadata) {
  //
  // Given token group (a.k.a. type), and the list of tokens in use,
  // find replacements for the tokens based upon values extracted
  // from the given data.
  //
  // Ignore anything except for the token groups we define.
  $item = NULL;
  switch ($group) {
    case Constants::FOLDERSHARE_TOKENS:
      // For these tokens we need a FolderShare object.
      if (isset($data[FolderShare::ENTITY_TYPE_ID]) === FALSE) {
        // No object, so no token replacement.
        return [];
      }
 
      $item = $data[FolderShare::ENTITY_TYPE_ID];
      break;
 
    default:
      // Unrecognized token group.
      return [];
  }
 
  //
  // Replace tokens
  // --------------
  // For each, add the replacement value.
  $replacements = [];
  $dateFormatter = \Drupal::service('date.formatter');
 
  if ($group === Constants::FOLDERSHARE_TOKENS) {
    // These tokens use values from the FolderShare object.
    foreach ($tokens as $name => $original) {
      switch ($name) {
        // ID.
        case 'id':
          $replacements[$original] = $item->id();
          break;
 
        // Kind.
        case 'kind':
          $replacements[$original] = $item->getKind();
          break;
 
        // Owner ID.
        case 'owner':
          $owner = $item->getOwner();
          $metadata->addCacheableDependency($owner);
          $replacements[$original] = $owner->getDisplayName();
          break;
 
        // Creation date.
        case 'created':
          // Date formatting defaults to the time zone and language
          // of the page being displayed.
          $metadata->addCacheableDependency(DateFormat::load('medium'));
          $replacements[$original] = $dateFormatter->format(
            $item->getCreatedTime(),
            'medium',
            '',
            NULL,
            NULL);
          break;
 
        // Changed date.
        case 'changed':
          // Date formatting defaults to the time zone and language
          // of the page being displayed.
          $metadata->addCacheableDependency(DateFormat::load('medium'));
          $replacements[$original] = $dateFormatter->format(
            $item->getChangedTime(),
            'medium',
            '',
            NULL,
            NULL);
          break;
 
        // Size.
        case 'size':
          // Could be empty.
          $sz = $item->getSize();
          $replacements[$original] = ($sz === FALSE) ? '' : $sz;
          break;
 
        // Name.
        case 'name':
          $replacements[$original] = $item->getName();
          break;
 
        // Description.
        case 'description':
          $replacements[$original] = $item->getDescription();
          break;
 
        // URL.
        case 'url':
          $replacements[$original] = $item->toUrl(
            'canonical',
            ['absolute' => TRUE])->toUrl();
          break;
 
        // Parent folder.
        case 'parent':
          $parent = $item->getParentFolder();
          if ($parent === NULL) {
            // When there is no parent, the item is a root item.
            // Return an empty string for the parent, since there isn't one.
            $replacements[$original] = '';
          }
          else {
            $replacements[$original] = $parent->getName();
          }
          break;
 
        // Root folder.
        case 'root':
          $root = $item->getRootItem();
          if ($root === NULL) {
            // It should not be possible for there to be no root. When an item
            // is at the root level, it is it's own root. But if this
            // somehow happens, return an empty string.
            $replacements[$original] = '';
          }
          else {
            $replacements[$original] = $root->getName();
          }
          break;
 
        default:
          // Ignore all other unrecognized tokens.
          break;
      }
    }
  }
 
  //
  // Replace chained tokens
  // ----------------------
  // A chained token is something like [foldershare:created:format].
  $tokenService = \Drupal::token();
 
  $chained = $tokenService->findWithPrefix($tokens, 'created');
  if (empty($chained) === FALSE) {
    $replacements += $tokenService->generate(
      'date',
      $chained,
      ['date' => $item->getCreatedTime()],
      $options,
      $metadata);
  }
 
  $chained = $tokenService->findWithPrefix($tokens, 'changed');
  if (empty($chained) === FALSE) {
    $replacements += $tokenService->generate(
      'date',
      $chained,
      ['date' => $item->getChangedTime()],
      $options,
      $metadata);
  }
 
  $chained = $tokenService->findWithPrefix($tokens, 'owner');
  if (empty($chained) === FALSE) {
    $replacements += $tokenService->generate(
      'user',
      $chained,
      ['user' => $item->getOwner()],
      $options,
      $metadata);
  }
 
  return $replacements;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc