annotate 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
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@0 171 ->setSetting('unsigned', TRUE)
Chris@0 172 ->setRequired(TRUE)
Chris@0 173 ->setSetting('allowed_values', $period)
Chris@0 174 ->setDisplayOptions('form', [
Chris@0 175 'type' => 'options_select',
Chris@0 176 'weight' => -2,
Chris@0 177 ])
Chris@0 178 ->setDisplayConfigurable('form', TRUE);
Chris@0 179
Chris@0 180 $fields['checked'] = BaseFieldDefinition::create('timestamp')
Chris@0 181 ->setLabel(t('Checked'))
Chris@0 182 ->setDescription(t('Last time feed was checked for new items, as Unix timestamp.'))
Chris@0 183 ->setDefaultValue(0)
Chris@0 184 ->setDisplayOptions('view', [
Chris@0 185 'label' => 'inline',
Chris@0 186 'type' => 'timestamp_ago',
Chris@0 187 'weight' => 1,
Chris@0 188 ])
Chris@0 189 ->setDisplayConfigurable('view', TRUE);
Chris@0 190
Chris@0 191 $fields['queued'] = BaseFieldDefinition::create('timestamp')
Chris@0 192 ->setLabel(t('Queued'))
Chris@0 193 ->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.'))
Chris@0 194 ->setDefaultValue(0);
Chris@0 195
Chris@0 196 $fields['link'] = BaseFieldDefinition::create('uri')
Chris@0 197 ->setLabel(t('URL'))
Chris@0 198 ->setDescription(t('The link of the feed.'))
Chris@0 199 ->setDisplayOptions('view', [
Chris@0 200 'label' => 'inline',
Chris@0 201 'weight' => 4,
Chris@0 202 ])
Chris@0 203 ->setDisplayConfigurable('view', TRUE);
Chris@0 204
Chris@0 205 $fields['description'] = BaseFieldDefinition::create('string_long')
Chris@0 206 ->setLabel(t('Description'))
Chris@0 207 ->setDescription(t("The parent website's description that comes from the @description element in the feed.", ['@description' => '<description>']));
Chris@0 208
Chris@0 209 $fields['image'] = BaseFieldDefinition::create('uri')
Chris@0 210 ->setLabel(t('Image'))
Chris@0 211 ->setDescription(t('An image representing the feed.'));
Chris@0 212
Chris@0 213 $fields['hash'] = BaseFieldDefinition::create('string')
Chris@0 214 ->setLabel(t('Hash'))
Chris@0 215 ->setSetting('is_ascii', TRUE)
Chris@0 216 ->setDescription(t('Calculated hash of the feed data, used for validating cache.'));
Chris@0 217
Chris@0 218 $fields['etag'] = BaseFieldDefinition::create('string')
Chris@0 219 ->setLabel(t('Etag'))
Chris@0 220 ->setDescription(t('Entity tag HTTP response header, used for validating cache.'));
Chris@0 221
Chris@0 222 // This is updated by the fetcher and not when the feed is saved, therefore
Chris@0 223 // it's a timestamp and not a changed field.
Chris@0 224 $fields['modified'] = BaseFieldDefinition::create('timestamp')
Chris@0 225 ->setLabel(t('Modified'))
Chris@0 226 ->setDescription(t('When the feed was last modified, as a Unix timestamp.'));
Chris@0 227
Chris@0 228 return $fields;
Chris@0 229 }
Chris@0 230
Chris@0 231 /**
Chris@0 232 * {@inheritdoc}
Chris@0 233 */
Chris@0 234 public function getUrl() {
Chris@0 235 return $this->get('url')->value;
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * {@inheritdoc}
Chris@0 240 */
Chris@0 241 public function getRefreshRate() {
Chris@0 242 return $this->get('refresh')->value;
Chris@0 243 }
Chris@0 244
Chris@0 245 /**
Chris@0 246 * {@inheritdoc}
Chris@0 247 */
Chris@0 248 public function getLastCheckedTime() {
Chris@0 249 return $this->get('checked')->value;
Chris@0 250 }
Chris@0 251
Chris@0 252 /**
Chris@0 253 * {@inheritdoc}
Chris@0 254 */
Chris@0 255 public function getQueuedTime() {
Chris@0 256 return $this->get('queued')->value;
Chris@0 257 }
Chris@0 258
Chris@0 259 /**
Chris@0 260 * {@inheritdoc}
Chris@0 261 */
Chris@0 262 public function getWebsiteUrl() {
Chris@0 263 return $this->get('link')->value;
Chris@0 264 }
Chris@0 265
Chris@0 266 /**
Chris@0 267 * {@inheritdoc}
Chris@0 268 */
Chris@0 269 public function getDescription() {
Chris@0 270 return $this->get('description')->value;
Chris@0 271 }
Chris@0 272
Chris@0 273 /**
Chris@0 274 * {@inheritdoc}
Chris@0 275 */
Chris@0 276 public function getImage() {
Chris@0 277 return $this->get('image')->value;
Chris@0 278 }
Chris@0 279
Chris@0 280 /**
Chris@0 281 * {@inheritdoc}
Chris@0 282 */
Chris@0 283 public function getHash() {
Chris@0 284 return $this->get('hash')->value;
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * {@inheritdoc}
Chris@0 289 */
Chris@0 290 public function getEtag() {
Chris@0 291 return $this->get('etag')->value;
Chris@0 292 }
Chris@0 293
Chris@0 294 /**
Chris@0 295 * {@inheritdoc}
Chris@0 296 */
Chris@0 297 public function getLastModified() {
Chris@0 298 return $this->get('modified')->value;
Chris@0 299 }
Chris@0 300
Chris@0 301 /**
Chris@0 302 * {@inheritdoc}
Chris@0 303 */
Chris@0 304 public function setTitle($title) {
Chris@0 305 $this->set('title', $title);
Chris@0 306 return $this;
Chris@0 307 }
Chris@0 308
Chris@0 309 /**
Chris@0 310 * {@inheritdoc}
Chris@0 311 */
Chris@0 312 public function setUrl($url) {
Chris@0 313 $this->set('url', $url);
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 setRefreshRate($refresh) {
Chris@0 321 $this->set('refresh', $refresh);
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 setLastCheckedTime($checked) {
Chris@0 329 $this->set('checked', $checked);
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 setQueuedTime($queued) {
Chris@0 337 $this->set('queued', $queued);
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 setWebsiteUrl($link) {
Chris@0 345 $this->set('link', $link);
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 setDescription($description) {
Chris@0 353 $this->set('description', $description);
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 setImage($image) {
Chris@0 361 $this->set('image', $image);
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 setHash($hash) {
Chris@0 369 $this->set('hash', $hash);
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 setEtag($etag) {
Chris@0 377 $this->set('etag', $etag);
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 setLastModified($modified) {
Chris@0 385 $this->set('modified', $modified);
Chris@0 386 return $this;
Chris@0 387 }
Chris@0 388
Chris@0 389 }