Feed.php 8.37 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\aggregator\Entity\Feed.
6
7
 */

8
namespace Drupal\aggregator\Entity;
9

10
use Drupal\Core\Entity\ContentEntityBase;
11
use Drupal\Core\Entity\EntityTypeInterface;
12
use Drupal\Core\Field\FieldDefinition;
13
use Symfony\Component\DependencyInjection\Container;
14
use Drupal\Core\Entity\EntityStorageInterface;
15
use Drupal\aggregator\FeedInterface;
16
17
18
19

/**
 * Defines the aggregator feed entity class.
 *
20
 * @ContentEntityType(
21
22
 *   id = "aggregator_feed",
 *   label = @Translation("Aggregator feed"),
23
 *   controllers = {
24
 *     "storage" = "Drupal\aggregator\FeedStorage",
25
 *     "view_builder" = "Drupal\aggregator\FeedViewBuilder",
26
 *     "form" = {
27
28
 *       "default" = "Drupal\aggregator\FeedFormController",
 *       "delete" = "Drupal\aggregator\Form\FeedDeleteForm",
29
 *       "delete_items" = "Drupal\aggregator\Form\FeedItemsDeleteForm",
30
 *     }
31
 *   },
32
33
34
35
36
 *   links = {
 *     "canonical" = "aggregator.feed_view",
 *     "edit-form" = "aggregator.feed_configure",
 *     "delete-form" = "aggregator.feed_delete",
 *   },
37
38
39
40
41
 *   base_table = "aggregator_feed",
 *   fieldable = TRUE,
 *   entity_keys = {
 *     "id" = "fid",
 *     "label" = "title",
42
 *     "uuid" = "uuid",
43
44
45
 *   }
 * )
 */
46
class Feed extends ContentEntityBase implements FeedInterface {
47
48
49
50
51
52
53
54
55
56
57

  /**
   * Implements Drupal\Core\Entity\EntityInterface::id().
   */
  public function id() {
    return $this->get('fid')->value;
  }

  /**
   * Implements Drupal\Core\Entity\EntityInterface::label().
   */
58
  public function label() {
59
60
    return $this->get('title')->value;
  }
61

62
63
64
  /**
   * {@inheritdoc}
   */
65
  public function deleteItems() {
66
67
    \Drupal::service('aggregator.items.importer')->delete($this);

68
    // Reset feed.
69
70
71
72
    $this->setLastCheckedTime(0);
    $this->setHash('');
    $this->setEtag('');
    $this->setLastModified(0);
73
    $this->save();
74
75

    return $this;
76
77
  }

78
79
80
81
82
83
84
85
86
87
88
89
90
91
  /**
   * {@inheritdoc}
   */
  public function refreshItems() {
    $success = \Drupal::service('aggregator.items.importer')->refresh($this);

    // Regardless of successful or not, indicate that it has been checked.
    $this->setLastCheckedTime(REQUEST_TIME);
    $this->setQueuedTime(0);
    $this->save();

    return $success;
  }

92
93
94
  /**
   * {@inheritdoc}
   */
95
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
96
97
98
99
100
101
102
103
104
105
    $values += array(
      'link' => '',
      'description' => '',
      'image' => '',
    );
  }

  /**
   * {@inheritdoc}
   */
106
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
107
    foreach ($entities as $entity) {
108
      // Notify processors to delete stored items.
109
      \Drupal::service('aggregator.items.importer')->delete($entity);
110
111
112
113
114
115
    }
  }

  /**
   * {@inheritdoc}
   */
116
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
117
118
119
120
121
122
123
    if (\Drupal::moduleHandler()->moduleExists('block')) {
      // Make sure there are no active blocks for these feeds.
      $ids = \Drupal::entityQuery('block')
        ->condition('plugin', 'aggregator_feed_block')
        ->condition('settings.feed', array_keys($entities))
        ->execute();
      if ($ids) {
124
        $block_storage = \Drupal::entityManager()->getStorage('block');
125
        $block_storage->delete($block_storage->loadMultiple($ids));
126
127
128
129
      }
    }
  }

130
131
132
  /**
   * {@inheritdoc}
   */
133
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
134
135
136
    $fields['fid'] = FieldDefinition::create('integer')
      ->setLabel(t('Feed ID'))
      ->setDescription(t('The ID of the aggregator feed.'))
137
138
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
139

140
141
142
143
    $fields['uuid'] = FieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The aggregator feed UUID.'))
      ->setReadOnly(TRUE);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

    $fields['title'] = FieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setDescription(t('The title of the feed.'));

    $fields['langcode'] = FieldDefinition::create('language')
      ->setLabel(t('Language code'))
      ->setDescription(t('The feed language code.'));

    $fields['url'] = FieldDefinition::create('uri')
      ->setLabel(t('URL'))
      ->setDescription(t('The URL to the feed.'));

    $fields['refresh'] = FieldDefinition::create('integer')
      ->setLabel(t('Refresh'))
159
160
      ->setDescription(t('How often to check for new feed items, in seconds.'))
      ->setSetting('unsigned', TRUE);
161

162
    $fields['checked'] = FieldDefinition::create('timestamp')
163
164
165
      ->setLabel(t('Checked'))
      ->setDescription(t('Last time feed was checked for new items, as Unix timestamp.'));

166
    $fields['queued'] = FieldDefinition::create('timestamp')
167
168
169
170
171
172
173
      ->setLabel(t('Queued'))
      ->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.'));

    $fields['link'] = FieldDefinition::create('uri')
      ->setLabel(t('Link'))
      ->setDescription(t('The link of the feed.'));

174
    $fields['description'] = FieldDefinition::create('string_long')
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
      ->setLabel(t('Description'))
      ->setDescription(t("The parent website's description that comes from the !description element in the feed.", array('!description' => '<description>')));

    $fields['image'] = FieldDefinition::create('uri')
      ->setLabel(t('Image'))
      ->setDescription(t('An image representing the feed.'));

    $fields['hash'] = FieldDefinition::create('string')
      ->setLabel(t('Hash'))
      ->setDescription(t('Calculated hash of the feed data, used for validating cache.'));

    $fields['etag'] = FieldDefinition::create('string')
      ->setLabel(t('Etag'))
      ->setDescription(t('Entity tag HTTP response header, used for validating cache.'));

190
191
192
    // This is updated by the fetcher and not when the feed is saved, therefore
    // it's a timestamp and not a changed field.
    $fields['modified'] = FieldDefinition::create('timestamp')
193
194
195
      ->setLabel(t('Modified'))
      ->setDescription(t('When the feed was last modified, as a Unix timestamp.'));

196
197
198
    return $fields;
  }

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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  /**
   * {@inheritdoc}
   */
  public function getUrl() {
    return $this->get('url')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getRefreshRate() {
    return $this->get('refresh')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastCheckedTime() {
    return $this->get('checked')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getQueuedTime() {
    return $this->get('queued')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getWebsiteUrl() {
    return $this->get('link')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->get('description')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getImage() {
    return $this->get('image')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getHash() {
    return $this->get('hash')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getEtag() {
    return $this->get('etag')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastModified() {
    return $this->get('modified')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setTitle($title) {
    $this->set('title', $title);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setUrl($url) {
    $this->set('url', $url);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setRefreshRate($refresh) {
    $this->set('refresh', $refresh);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLastCheckedTime($checked) {
    $this->set('checked', $checked);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setQueuedTime($queued) {
    $this->set('queued', $queued);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setWebsiteUrl($link) {
    $this->set('link', $link);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setDescription($description) {
    $this->set('description', $description);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setImage($image) {
    $this->set('image', $image);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setHash($hash) {
    $this->set('hash', $hash);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setEtag($etag) {
    $this->set('etag', $etag);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLastModified($modified) {
    $this->set('modified', $modified);
    return $this;
  }

357
}