Pull merge.
[yaffs-website] / web / core / modules / aggregator / src / Entity / Feed.php
1 <?php
2
3 namespace Drupal\aggregator\Entity;
4
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Field\BaseFieldDefinition;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\aggregator\FeedInterface;
10
11 /**
12  * Defines the aggregator feed entity class.
13  *
14  * @ContentEntityType(
15  *   id = "aggregator_feed",
16  *   label = @Translation("Aggregator feed"),
17  *   label_collection = @Translation("Aggregator feeds"),
18  *   label_singular = @Translation("aggregator feed"),
19  *   label_plural = @Translation("aggregator feeds"),
20  *   label_count = @PluralTranslation(
21  *     singular = "@count aggregator feed",
22  *     plural = "@count aggregator feeds",
23  *   ),
24  *   handlers = {
25  *     "storage" = "Drupal\aggregator\FeedStorage",
26  *     "storage_schema" = "Drupal\aggregator\FeedStorageSchema",
27  *     "view_builder" = "Drupal\aggregator\FeedViewBuilder",
28  *     "access" = "Drupal\aggregator\FeedAccessControlHandler",
29  *     "views_data" = "Drupal\aggregator\AggregatorFeedViewsData",
30  *     "form" = {
31  *       "default" = "Drupal\aggregator\FeedForm",
32  *       "delete" = "Drupal\aggregator\Form\FeedDeleteForm",
33  *       "delete_items" = "Drupal\aggregator\Form\FeedItemsDeleteForm",
34  *     },
35  *     "route_provider" = {
36  *       "html" = "Drupal\aggregator\FeedHtmlRouteProvider",
37  *     },
38  *   },
39  *   links = {
40  *     "canonical" = "/aggregator/sources/{aggregator_feed}",
41  *     "edit-form" = "/aggregator/sources/{aggregator_feed}/configure",
42  *     "delete-form" = "/aggregator/sources/{aggregator_feed}/delete",
43  *   },
44  *   field_ui_base_route = "aggregator.admin_overview",
45  *   base_table = "aggregator_feed",
46  *   render_cache = FALSE,
47  *   entity_keys = {
48  *     "id" = "fid",
49  *     "label" = "title",
50  *     "langcode" = "langcode",
51  *     "uuid" = "uuid",
52  *   }
53  * )
54  */
55 class Feed extends ContentEntityBase implements FeedInterface {
56
57   /**
58    * {@inheritdoc}
59    */
60   public function label() {
61     return $this->get('title')->value;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function deleteItems() {
68     \Drupal::service('aggregator.items.importer')->delete($this);
69
70     // Reset feed.
71     $this->setLastCheckedTime(0);
72     $this->setHash('');
73     $this->setEtag('');
74     $this->setLastModified(0);
75     $this->save();
76
77     return $this;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function refreshItems() {
84     $success = \Drupal::service('aggregator.items.importer')->refresh($this);
85
86     // Regardless of successful or not, indicate that it has been checked.
87     $this->setLastCheckedTime(REQUEST_TIME);
88     $this->setQueuedTime(0);
89     $this->save();
90
91     return $success;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public static function preCreate(EntityStorageInterface $storage, array &$values) {
98     $values += [
99       'link' => '',
100       'description' => '',
101       'image' => '',
102     ];
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public static function preDelete(EntityStorageInterface $storage, array $entities) {
109     foreach ($entities as $entity) {
110       // Notify processors to delete stored items.
111       \Drupal::service('aggregator.items.importer')->delete($entity);
112     }
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public static function postDelete(EntityStorageInterface $storage, array $entities) {
119     parent::postDelete($storage, $entities);
120     if (\Drupal::moduleHandler()->moduleExists('block')) {
121       // Make sure there are no active blocks for these feeds.
122       $ids = \Drupal::entityQuery('block')
123         ->condition('plugin', 'aggregator_feed_block')
124         ->condition('settings.feed', array_keys($entities))
125         ->execute();
126       if ($ids) {
127         $block_storage = \Drupal::entityManager()->getStorage('block');
128         $block_storage->delete($block_storage->loadMultiple($ids));
129       }
130     }
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
137     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
138     $fields = parent::baseFieldDefinitions($entity_type);
139
140     $fields['fid']->setLabel(t('Feed ID'))
141       ->setDescription(t('The ID of the aggregator feed.'));
142
143     $fields['uuid']->setDescription(t('The aggregator feed UUID.'));
144
145     $fields['langcode']->setLabel(t('Language code'))
146       ->setDescription(t('The feed language code.'));
147
148     $fields['title'] = BaseFieldDefinition::create('string')
149       ->setLabel(t('Title'))
150       ->setDescription(t('The name of the feed (or the name of the website providing the feed).'))
151       ->setRequired(TRUE)
152       ->setSetting('max_length', 255)
153       ->setDisplayOptions('form', [
154         'type' => 'string_textfield',
155         'weight' => -5,
156       ])
157       ->setDisplayConfigurable('form', TRUE)
158       ->addConstraint('FeedTitle');
159
160     $fields['url'] = BaseFieldDefinition::create('uri')
161       ->setLabel(t('URL'))
162       ->setDescription(t('The fully-qualified URL of the feed.'))
163       ->setRequired(TRUE)
164       ->setDisplayOptions('form', [
165         'type' => 'uri',
166         'weight' => -3,
167       ])
168       ->setDisplayConfigurable('form', TRUE)
169       ->addConstraint('FeedUrl');
170
171     $intervals = [900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200];
172     $period = array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($intervals, $intervals));
173     $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
174
175     $fields['refresh'] = BaseFieldDefinition::create('list_integer')
176       ->setLabel(t('Update interval'))
177       ->setDescription(t('The length of time between feed updates. Requires a correctly configured cron maintenance task.'))
178       ->setDefaultValue(3600)
179       ->setSetting('unsigned', TRUE)
180       ->setRequired(TRUE)
181       ->setSetting('allowed_values', $period)
182       ->setDisplayOptions('form', [
183         'type' => 'options_select',
184         'weight' => -2,
185       ])
186       ->setDisplayConfigurable('form', TRUE);
187
188     $fields['checked'] = BaseFieldDefinition::create('timestamp')
189       ->setLabel(t('Checked'))
190       ->setDescription(t('Last time feed was checked for new items, as Unix timestamp.'))
191       ->setDefaultValue(0)
192       ->setDisplayOptions('view', [
193         'label' => 'inline',
194         'type' => 'timestamp_ago',
195         'weight' => 1,
196       ])
197       ->setDisplayConfigurable('view', TRUE);
198
199     $fields['queued'] = BaseFieldDefinition::create('timestamp')
200       ->setLabel(t('Queued'))
201       ->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.'))
202       ->setDefaultValue(0);
203
204     $fields['link'] = BaseFieldDefinition::create('uri')
205       ->setLabel(t('URL'))
206       ->setDescription(t('The link of the feed.'))
207       ->setDisplayOptions('view', [
208         'label' => 'inline',
209         'weight' => 4,
210       ])
211       ->setDisplayConfigurable('view', TRUE);
212
213     $fields['description'] = BaseFieldDefinition::create('string_long')
214       ->setLabel(t('Description'))
215       ->setDescription(t("The parent website's description that comes from the @description element in the feed.", ['@description' => '<description>']));
216
217     $fields['image'] = BaseFieldDefinition::create('uri')
218       ->setLabel(t('Image'))
219       ->setDescription(t('An image representing the feed.'));
220
221     $fields['hash'] = BaseFieldDefinition::create('string')
222       ->setLabel(t('Hash'))
223       ->setSetting('is_ascii', TRUE)
224       ->setDescription(t('Calculated hash of the feed data, used for validating cache.'));
225
226     $fields['etag'] = BaseFieldDefinition::create('string')
227       ->setLabel(t('Etag'))
228       ->setDescription(t('Entity tag HTTP response header, used for validating cache.'));
229
230     // This is updated by the fetcher and not when the feed is saved, therefore
231     // it's a timestamp and not a changed field.
232     $fields['modified'] = BaseFieldDefinition::create('timestamp')
233       ->setLabel(t('Modified'))
234       ->setDescription(t('When the feed was last modified, as a Unix timestamp.'));
235
236     return $fields;
237   }
238
239   /**
240    * {@inheritdoc}
241    */
242   public function getUrl() {
243     return $this->get('url')->value;
244   }
245
246   /**
247    * {@inheritdoc}
248    */
249   public function getRefreshRate() {
250     return $this->get('refresh')->value;
251   }
252
253   /**
254    * {@inheritdoc}
255    */
256   public function getLastCheckedTime() {
257     return $this->get('checked')->value;
258   }
259
260   /**
261    * {@inheritdoc}
262    */
263   public function getQueuedTime() {
264     return $this->get('queued')->value;
265   }
266
267   /**
268    * {@inheritdoc}
269    */
270   public function getWebsiteUrl() {
271     return $this->get('link')->value;
272   }
273
274   /**
275    * {@inheritdoc}
276    */
277   public function getDescription() {
278     return $this->get('description')->value;
279   }
280
281   /**
282    * {@inheritdoc}
283    */
284   public function getImage() {
285     return $this->get('image')->value;
286   }
287
288   /**
289    * {@inheritdoc}
290    */
291   public function getHash() {
292     return $this->get('hash')->value;
293   }
294
295   /**
296    * {@inheritdoc}
297    */
298   public function getEtag() {
299     return $this->get('etag')->value;
300   }
301
302   /**
303    * {@inheritdoc}
304    */
305   public function getLastModified() {
306     return $this->get('modified')->value;
307   }
308
309   /**
310    * {@inheritdoc}
311    */
312   public function setTitle($title) {
313     $this->set('title', $title);
314     return $this;
315   }
316
317   /**
318    * {@inheritdoc}
319    */
320   public function setUrl($url) {
321     $this->set('url', $url);
322     return $this;
323   }
324
325   /**
326    * {@inheritdoc}
327    */
328   public function setRefreshRate($refresh) {
329     $this->set('refresh', $refresh);
330     return $this;
331   }
332
333   /**
334    * {@inheritdoc}
335    */
336   public function setLastCheckedTime($checked) {
337     $this->set('checked', $checked);
338     return $this;
339   }
340
341   /**
342    * {@inheritdoc}
343    */
344   public function setQueuedTime($queued) {
345     $this->set('queued', $queued);
346     return $this;
347   }
348
349   /**
350    * {@inheritdoc}
351    */
352   public function setWebsiteUrl($link) {
353     $this->set('link', $link);
354     return $this;
355   }
356
357   /**
358    * {@inheritdoc}
359    */
360   public function setDescription($description) {
361     $this->set('description', $description);
362     return $this;
363   }
364
365   /**
366    * {@inheritdoc}
367    */
368   public function setImage($image) {
369     $this->set('image', $image);
370     return $this;
371   }
372
373   /**
374    * {@inheritdoc}
375    */
376   public function setHash($hash) {
377     $this->set('hash', $hash);
378     return $this;
379   }
380
381   /**
382    * {@inheritdoc}
383    */
384   public function setEtag($etag) {
385     $this->set('etag', $etag);
386     return $this;
387   }
388
389   /**
390    * {@inheritdoc}
391    */
392   public function setLastModified($modified) {
393     $this->set('modified', $modified);
394     return $this;
395   }
396
397 }