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