annotate core/modules/aggregator/src/Entity/Feed.php @ 19:fa3358dc1485 tip

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