annotate core/modules/aggregator/src/Entity/Feed.php @ 17:129ea1e6d783

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