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"),
18 * "storage" = "Drupal\aggregator\FeedStorage",
19 * "storage_schema" = "Drupal\aggregator\FeedStorageSchema",
20 * "view_builder" = "Drupal\aggregator\FeedViewBuilder",
21 * "access" = "Drupal\aggregator\FeedAccessControlHandler",
22 * "views_data" = "Drupal\aggregator\AggregatorFeedViewsData",
24 * "default" = "Drupal\aggregator\FeedForm",
25 * "delete" = "Drupal\aggregator\Form\FeedDeleteForm",
26 * "delete_items" = "Drupal\aggregator\Form\FeedItemsDeleteForm",
28 * "route_provider" = {
29 * "html" = "Drupal\aggregator\FeedHtmlRouteProvider",
33 * "canonical" = "/aggregator/sources/{aggregator_feed}",
34 * "edit-form" = "/aggregator/sources/{aggregator_feed}/configure",
35 * "delete-form" = "/aggregator/sources/{aggregator_feed}/delete",
37 * field_ui_base_route = "aggregator.admin_overview",
38 * base_table = "aggregator_feed",
39 * render_cache = FALSE,
43 * "langcode" = "langcode",
48 class Feed extends ContentEntityBase implements FeedInterface {
53 public function label() {
54 return $this->get('title')->value;
60 public function deleteItems() {
61 \Drupal::service('aggregator.items.importer')->delete($this);
64 $this->setLastCheckedTime(0);
67 $this->setLastModified(0);
76 public function refreshItems() {
77 $success = \Drupal::service('aggregator.items.importer')->refresh($this);
79 // Regardless of successful or not, indicate that it has been checked.
80 $this->setLastCheckedTime(REQUEST_TIME);
81 $this->setQueuedTime(0);
90 public static function preCreate(EntityStorageInterface $storage, array &$values) {
101 public static function preDelete(EntityStorageInterface $storage, array $entities) {
102 foreach ($entities as $entity) {
103 // Notify processors to delete stored items.
104 \Drupal::service('aggregator.items.importer')->delete($entity);
111 public static function postDelete(EntityStorageInterface $storage, array $entities) {
112 parent::postDelete($storage, $entities);
113 if (\Drupal::moduleHandler()->moduleExists('block')) {
114 // Make sure there are no active blocks for these feeds.
115 $ids = \Drupal::entityQuery('block')
116 ->condition('plugin', 'aggregator_feed_block')
117 ->condition('settings.feed', array_keys($entities))
120 $block_storage = \Drupal::entityManager()->getStorage('block');
121 $block_storage->delete($block_storage->loadMultiple($ids));
129 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
130 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
131 $fields = parent::baseFieldDefinitions($entity_type);
133 $fields['fid']->setLabel(t('Feed ID'))
134 ->setDescription(t('The ID of the aggregator feed.'));
136 $fields['uuid']->setDescription(t('The aggregator feed UUID.'));
138 $fields['langcode']->setLabel(t('Language code'))
139 ->setDescription(t('The feed language code.'));
141 $fields['title'] = BaseFieldDefinition::create('string')
142 ->setLabel(t('Title'))
143 ->setDescription(t('The name of the feed (or the name of the website providing the feed).'))
145 ->setSetting('max_length', 255)
146 ->setDisplayOptions('form', [
147 'type' => 'string_textfield',
150 ->setDisplayConfigurable('form', TRUE)
151 ->addConstraint('FeedTitle');
153 $fields['url'] = BaseFieldDefinition::create('uri')
155 ->setDescription(t('The fully-qualified URL of the feed.'))
157 ->setDisplayOptions('form', [
161 ->setDisplayConfigurable('form', TRUE)
162 ->addConstraint('FeedUrl');
164 $intervals = [900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200];
165 $period = array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($intervals, $intervals));
166 $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
168 $fields['refresh'] = BaseFieldDefinition::create('list_integer')
169 ->setLabel(t('Update interval'))
170 ->setDescription(t('The length of time between feed updates. Requires a correctly configured cron maintenance task.'))
171 ->setSetting('unsigned', TRUE)
173 ->setSetting('allowed_values', $period)
174 ->setDisplayOptions('form', [
175 'type' => 'options_select',
178 ->setDisplayConfigurable('form', TRUE);
180 $fields['checked'] = BaseFieldDefinition::create('timestamp')
181 ->setLabel(t('Checked'))
182 ->setDescription(t('Last time feed was checked for new items, as Unix timestamp.'))
184 ->setDisplayOptions('view', [
186 'type' => 'timestamp_ago',
189 ->setDisplayConfigurable('view', TRUE);
191 $fields['queued'] = BaseFieldDefinition::create('timestamp')
192 ->setLabel(t('Queued'))
193 ->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.'))
194 ->setDefaultValue(0);
196 $fields['link'] = BaseFieldDefinition::create('uri')
198 ->setDescription(t('The link of the feed.'))
199 ->setDisplayOptions('view', [
203 ->setDisplayConfigurable('view', TRUE);
205 $fields['description'] = BaseFieldDefinition::create('string_long')
206 ->setLabel(t('Description'))
207 ->setDescription(t("The parent website's description that comes from the @description element in the feed.", ['@description' => '<description>']));
209 $fields['image'] = BaseFieldDefinition::create('uri')
210 ->setLabel(t('Image'))
211 ->setDescription(t('An image representing the feed.'));
213 $fields['hash'] = BaseFieldDefinition::create('string')
214 ->setLabel(t('Hash'))
215 ->setSetting('is_ascii', TRUE)
216 ->setDescription(t('Calculated hash of the feed data, used for validating cache.'));
218 $fields['etag'] = BaseFieldDefinition::create('string')
219 ->setLabel(t('Etag'))
220 ->setDescription(t('Entity tag HTTP response header, used for validating cache.'));
222 // This is updated by the fetcher and not when the feed is saved, therefore
223 // it's a timestamp and not a changed field.
224 $fields['modified'] = BaseFieldDefinition::create('timestamp')
225 ->setLabel(t('Modified'))
226 ->setDescription(t('When the feed was last modified, as a Unix timestamp.'));
234 public function getUrl() {
235 return $this->get('url')->value;
241 public function getRefreshRate() {
242 return $this->get('refresh')->value;
248 public function getLastCheckedTime() {
249 return $this->get('checked')->value;
255 public function getQueuedTime() {
256 return $this->get('queued')->value;
262 public function getWebsiteUrl() {
263 return $this->get('link')->value;
269 public function getDescription() {
270 return $this->get('description')->value;
276 public function getImage() {
277 return $this->get('image')->value;
283 public function getHash() {
284 return $this->get('hash')->value;
290 public function getEtag() {
291 return $this->get('etag')->value;
297 public function getLastModified() {
298 return $this->get('modified')->value;
304 public function setTitle($title) {
305 $this->set('title', $title);
312 public function setUrl($url) {
313 $this->set('url', $url);
320 public function setRefreshRate($refresh) {
321 $this->set('refresh', $refresh);
328 public function setLastCheckedTime($checked) {
329 $this->set('checked', $checked);
336 public function setQueuedTime($queued) {
337 $this->set('queued', $queued);
344 public function setWebsiteUrl($link) {
345 $this->set('link', $link);
352 public function setDescription($description) {
353 $this->set('description', $description);
360 public function setImage($image) {
361 $this->set('image', $image);
368 public function setHash($hash) {
369 $this->set('hash', $hash);
376 public function setEtag($etag) {
377 $this->set('etag', $etag);
384 public function setLastModified($modified) {
385 $this->set('modified', $modified);