3 namespace Drupal\aggregator\Entity;
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;
12 * Defines the aggregator feed entity class.
15 * id = "aggregator_feed",
16 * label = @Translation("Aggregator feed"),
17 * label_collection = @Translation("Aggregator feeds"),
18 * label_singular = @Translation("aggregator feed"),
19 * label_plural = @Translation("aggregator feeds"),
20 * label_count = @PluralTranslation(
21 * singular = "@count aggregator feed",
22 * plural = "@count aggregator feeds",
25 * "storage" = "Drupal\aggregator\FeedStorage",
26 * "storage_schema" = "Drupal\aggregator\FeedStorageSchema",
27 * "view_builder" = "Drupal\aggregator\FeedViewBuilder",
28 * "access" = "Drupal\aggregator\FeedAccessControlHandler",
29 * "views_data" = "Drupal\aggregator\AggregatorFeedViewsData",
31 * "default" = "Drupal\aggregator\FeedForm",
32 * "delete" = "Drupal\aggregator\Form\FeedDeleteForm",
33 * "delete_items" = "Drupal\aggregator\Form\FeedItemsDeleteForm",
35 * "route_provider" = {
36 * "html" = "Drupal\aggregator\FeedHtmlRouteProvider",
40 * "canonical" = "/aggregator/sources/{aggregator_feed}",
41 * "edit-form" = "/aggregator/sources/{aggregator_feed}/configure",
42 * "delete-form" = "/aggregator/sources/{aggregator_feed}/delete",
44 * field_ui_base_route = "aggregator.admin_overview",
45 * base_table = "aggregator_feed",
46 * render_cache = FALSE,
50 * "langcode" = "langcode",
55 class Feed extends ContentEntityBase implements FeedInterface {
60 public function label() {
61 return $this->get('title')->value;
67 public function deleteItems() {
68 \Drupal::service('aggregator.items.importer')->delete($this);
71 $this->setLastCheckedTime(0);
74 $this->setLastModified(0);
83 public function refreshItems() {
84 $success = \Drupal::service('aggregator.items.importer')->refresh($this);
86 // Regardless of successful or not, indicate that it has been checked.
87 $this->setLastCheckedTime(REQUEST_TIME);
88 $this->setQueuedTime(0);
97 public static function preCreate(EntityStorageInterface $storage, array &$values) {
108 public static function preDelete(EntityStorageInterface $storage, array $entities) {
109 foreach ($entities as $entity) {
110 // Notify processors to delete stored items.
111 \Drupal::service('aggregator.items.importer')->delete($entity);
118 public static function postDelete(EntityStorageInterface $storage, array $entities) {
119 parent::postDelete($storage, $entities);
120 if (\Drupal::moduleHandler()->moduleExists('block')) {
121 // Make sure there are no active blocks for these feeds.
122 $ids = \Drupal::entityQuery('block')
123 ->condition('plugin', 'aggregator_feed_block')
124 ->condition('settings.feed', array_keys($entities))
127 $block_storage = \Drupal::entityManager()->getStorage('block');
128 $block_storage->delete($block_storage->loadMultiple($ids));
136 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
137 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
138 $fields = parent::baseFieldDefinitions($entity_type);
140 $fields['fid']->setLabel(t('Feed ID'))
141 ->setDescription(t('The ID of the aggregator feed.'));
143 $fields['uuid']->setDescription(t('The aggregator feed UUID.'));
145 $fields['langcode']->setLabel(t('Language code'))
146 ->setDescription(t('The feed language code.'));
148 $fields['title'] = BaseFieldDefinition::create('string')
149 ->setLabel(t('Title'))
150 ->setDescription(t('The name of the feed (or the name of the website providing the feed).'))
152 ->setSetting('max_length', 255)
153 ->setDisplayOptions('form', [
154 'type' => 'string_textfield',
157 ->setDisplayConfigurable('form', TRUE)
158 ->addConstraint('FeedTitle');
160 $fields['url'] = BaseFieldDefinition::create('uri')
162 ->setDescription(t('The fully-qualified URL of the feed.'))
164 ->setDisplayOptions('form', [
168 ->setDisplayConfigurable('form', TRUE)
169 ->addConstraint('FeedUrl');
171 $intervals = [900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200];
172 $period = array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($intervals, $intervals));
173 $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
175 $fields['refresh'] = BaseFieldDefinition::create('list_integer')
176 ->setLabel(t('Update interval'))
177 ->setDescription(t('The length of time between feed updates. Requires a correctly configured cron maintenance task.'))
178 ->setDefaultValue(3600)
179 ->setSetting('unsigned', TRUE)
181 ->setSetting('allowed_values', $period)
182 ->setDisplayOptions('form', [
183 'type' => 'options_select',
186 ->setDisplayConfigurable('form', TRUE);
188 $fields['checked'] = BaseFieldDefinition::create('timestamp')
189 ->setLabel(t('Checked'))
190 ->setDescription(t('Last time feed was checked for new items, as Unix timestamp.'))
192 ->setDisplayOptions('view', [
194 'type' => 'timestamp_ago',
197 ->setDisplayConfigurable('view', TRUE);
199 $fields['queued'] = BaseFieldDefinition::create('timestamp')
200 ->setLabel(t('Queued'))
201 ->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.'))
202 ->setDefaultValue(0);
204 $fields['link'] = BaseFieldDefinition::create('uri')
206 ->setDescription(t('The link of the feed.'))
207 ->setDisplayOptions('view', [
211 ->setDisplayConfigurable('view', TRUE);
213 $fields['description'] = BaseFieldDefinition::create('string_long')
214 ->setLabel(t('Description'))
215 ->setDescription(t("The parent website's description that comes from the @description element in the feed.", ['@description' => '<description>']));
217 $fields['image'] = BaseFieldDefinition::create('uri')
218 ->setLabel(t('Image'))
219 ->setDescription(t('An image representing the feed.'));
221 $fields['hash'] = BaseFieldDefinition::create('string')
222 ->setLabel(t('Hash'))
223 ->setSetting('is_ascii', TRUE)
224 ->setDescription(t('Calculated hash of the feed data, used for validating cache.'));
226 $fields['etag'] = BaseFieldDefinition::create('string')
227 ->setLabel(t('Etag'))
228 ->setDescription(t('Entity tag HTTP response header, used for validating cache.'));
230 // This is updated by the fetcher and not when the feed is saved, therefore
231 // it's a timestamp and not a changed field.
232 $fields['modified'] = BaseFieldDefinition::create('timestamp')
233 ->setLabel(t('Modified'))
234 ->setDescription(t('When the feed was last modified, as a Unix timestamp.'));
242 public function getUrl() {
243 return $this->get('url')->value;
249 public function getRefreshRate() {
250 return $this->get('refresh')->value;
256 public function getLastCheckedTime() {
257 return $this->get('checked')->value;
263 public function getQueuedTime() {
264 return $this->get('queued')->value;
270 public function getWebsiteUrl() {
271 return $this->get('link')->value;
277 public function getDescription() {
278 return $this->get('description')->value;
284 public function getImage() {
285 return $this->get('image')->value;
291 public function getHash() {
292 return $this->get('hash')->value;
298 public function getEtag() {
299 return $this->get('etag')->value;
305 public function getLastModified() {
306 return $this->get('modified')->value;
312 public function setTitle($title) {
313 $this->set('title', $title);
320 public function setUrl($url) {
321 $this->set('url', $url);
328 public function setRefreshRate($refresh) {
329 $this->set('refresh', $refresh);
336 public function setLastCheckedTime($checked) {
337 $this->set('checked', $checked);
344 public function setQueuedTime($queued) {
345 $this->set('queued', $queued);
352 public function setWebsiteUrl($link) {
353 $this->set('link', $link);
360 public function setDescription($description) {
361 $this->set('description', $description);
368 public function setImage($image) {
369 $this->set('image', $image);
376 public function setHash($hash) {
377 $this->set('hash', $hash);
384 public function setEtag($etag) {
385 $this->set('etag', $etag);
392 public function setLastModified($modified) {
393 $this->set('modified', $modified);