annotate core/modules/aggregator/src/Entity/Feed.php @ 12:7a779792577d

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