Chris@0: get('title')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function deleteItems() { Chris@0: \Drupal::service('aggregator.items.importer')->delete($this); Chris@0: Chris@0: // Reset feed. Chris@0: $this->setLastCheckedTime(0); Chris@0: $this->setHash(''); Chris@0: $this->setEtag(''); Chris@0: $this->setLastModified(0); Chris@0: $this->save(); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function refreshItems() { Chris@0: $success = \Drupal::service('aggregator.items.importer')->refresh($this); Chris@0: Chris@0: // Regardless of successful or not, indicate that it has been checked. Chris@0: $this->setLastCheckedTime(REQUEST_TIME); Chris@0: $this->setQueuedTime(0); Chris@0: $this->save(); Chris@0: Chris@0: return $success; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function preCreate(EntityStorageInterface $storage, array &$values) { Chris@0: $values += [ Chris@0: 'link' => '', Chris@0: 'description' => '', Chris@0: 'image' => '', Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function preDelete(EntityStorageInterface $storage, array $entities) { Chris@0: foreach ($entities as $entity) { Chris@0: // Notify processors to delete stored items. Chris@0: \Drupal::service('aggregator.items.importer')->delete($entity); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function postDelete(EntityStorageInterface $storage, array $entities) { Chris@0: parent::postDelete($storage, $entities); Chris@0: if (\Drupal::moduleHandler()->moduleExists('block')) { Chris@0: // Make sure there are no active blocks for these feeds. Chris@0: $ids = \Drupal::entityQuery('block') Chris@0: ->condition('plugin', 'aggregator_feed_block') Chris@0: ->condition('settings.feed', array_keys($entities)) Chris@0: ->execute(); Chris@0: if ($ids) { Chris@0: $block_storage = \Drupal::entityManager()->getStorage('block'); Chris@0: $block_storage->delete($block_storage->loadMultiple($ids)); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { Chris@0: /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ Chris@0: $fields = parent::baseFieldDefinitions($entity_type); Chris@0: Chris@0: $fields['fid']->setLabel(t('Feed ID')) Chris@0: ->setDescription(t('The ID of the aggregator feed.')); Chris@0: Chris@0: $fields['uuid']->setDescription(t('The aggregator feed UUID.')); Chris@0: Chris@0: $fields['langcode']->setLabel(t('Language code')) Chris@0: ->setDescription(t('The feed language code.')); Chris@0: Chris@0: $fields['title'] = BaseFieldDefinition::create('string') Chris@0: ->setLabel(t('Title')) Chris@0: ->setDescription(t('The name of the feed (or the name of the website providing the feed).')) Chris@0: ->setRequired(TRUE) Chris@0: ->setSetting('max_length', 255) Chris@0: ->setDisplayOptions('form', [ Chris@0: 'type' => 'string_textfield', Chris@0: 'weight' => -5, Chris@0: ]) Chris@0: ->setDisplayConfigurable('form', TRUE) Chris@0: ->addConstraint('FeedTitle'); Chris@0: Chris@0: $fields['url'] = BaseFieldDefinition::create('uri') Chris@0: ->setLabel(t('URL')) Chris@0: ->setDescription(t('The fully-qualified URL of the feed.')) Chris@0: ->setRequired(TRUE) Chris@0: ->setDisplayOptions('form', [ Chris@0: 'type' => 'uri', Chris@0: 'weight' => -3, Chris@0: ]) Chris@0: ->setDisplayConfigurable('form', TRUE) Chris@0: ->addConstraint('FeedUrl'); Chris@0: Chris@0: $intervals = [900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200]; Chris@0: $period = array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($intervals, $intervals)); Chris@18: $period[FeedStorageInterface::CLEAR_NEVER] = t('Never'); Chris@0: Chris@0: $fields['refresh'] = BaseFieldDefinition::create('list_integer') Chris@0: ->setLabel(t('Update interval')) Chris@0: ->setDescription(t('The length of time between feed updates. Requires a correctly configured cron maintenance task.')) Chris@12: ->setDefaultValue(3600) Chris@0: ->setSetting('unsigned', TRUE) Chris@0: ->setRequired(TRUE) Chris@0: ->setSetting('allowed_values', $period) Chris@0: ->setDisplayOptions('form', [ Chris@0: 'type' => 'options_select', Chris@0: 'weight' => -2, Chris@0: ]) Chris@0: ->setDisplayConfigurable('form', TRUE); Chris@0: Chris@0: $fields['checked'] = BaseFieldDefinition::create('timestamp') Chris@18: ->setLabel(t('Checked', [], ['context' => 'Examined'])) Chris@0: ->setDescription(t('Last time feed was checked for new items, as Unix timestamp.')) Chris@0: ->setDefaultValue(0) Chris@0: ->setDisplayOptions('view', [ Chris@0: 'label' => 'inline', Chris@0: 'type' => 'timestamp_ago', Chris@0: 'weight' => 1, Chris@0: ]) Chris@0: ->setDisplayConfigurable('view', TRUE); Chris@0: Chris@0: $fields['queued'] = BaseFieldDefinition::create('timestamp') Chris@0: ->setLabel(t('Queued')) Chris@0: ->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.')) Chris@0: ->setDefaultValue(0); Chris@0: Chris@0: $fields['link'] = BaseFieldDefinition::create('uri') Chris@0: ->setLabel(t('URL')) Chris@0: ->setDescription(t('The link of the feed.')) Chris@0: ->setDisplayOptions('view', [ Chris@0: 'label' => 'inline', Chris@0: 'weight' => 4, Chris@0: ]) Chris@0: ->setDisplayConfigurable('view', TRUE); Chris@0: Chris@0: $fields['description'] = BaseFieldDefinition::create('string_long') Chris@0: ->setLabel(t('Description')) Chris@0: ->setDescription(t("The parent website's description that comes from the @description element in the feed.", ['@description' => ''])); Chris@0: Chris@0: $fields['image'] = BaseFieldDefinition::create('uri') Chris@0: ->setLabel(t('Image')) Chris@0: ->setDescription(t('An image representing the feed.')); Chris@0: Chris@0: $fields['hash'] = BaseFieldDefinition::create('string') Chris@0: ->setLabel(t('Hash')) Chris@0: ->setSetting('is_ascii', TRUE) Chris@0: ->setDescription(t('Calculated hash of the feed data, used for validating cache.')); Chris@0: Chris@0: $fields['etag'] = BaseFieldDefinition::create('string') Chris@0: ->setLabel(t('Etag')) Chris@0: ->setDescription(t('Entity tag HTTP response header, used for validating cache.')); Chris@0: Chris@0: // This is updated by the fetcher and not when the feed is saved, therefore Chris@0: // it's a timestamp and not a changed field. Chris@0: $fields['modified'] = BaseFieldDefinition::create('timestamp') Chris@0: ->setLabel(t('Modified')) Chris@0: ->setDescription(t('When the feed was last modified, as a Unix timestamp.')); Chris@0: Chris@0: return $fields; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getUrl() { Chris@0: return $this->get('url')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getRefreshRate() { Chris@0: return $this->get('refresh')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getLastCheckedTime() { Chris@0: return $this->get('checked')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getQueuedTime() { Chris@0: return $this->get('queued')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getWebsiteUrl() { Chris@0: return $this->get('link')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDescription() { Chris@0: return $this->get('description')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getImage() { Chris@0: return $this->get('image')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getHash() { Chris@0: return $this->get('hash')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getEtag() { Chris@0: return $this->get('etag')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getLastModified() { Chris@0: return $this->get('modified')->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setTitle($title) { Chris@0: $this->set('title', $title); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setUrl($url) { Chris@0: $this->set('url', $url); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setRefreshRate($refresh) { Chris@0: $this->set('refresh', $refresh); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setLastCheckedTime($checked) { Chris@0: $this->set('checked', $checked); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setQueuedTime($queued) { Chris@0: $this->set('queued', $queued); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setWebsiteUrl($link) { Chris@0: $this->set('link', $link); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setDescription($description) { Chris@0: $this->set('description', $description); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setImage($image) { Chris@0: $this->set('image', $image); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setHash($hash) { Chris@0: $this->set('hash', $hash); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setEtag($etag) { Chris@0: $this->set('etag', $etag); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setLastModified($modified) { Chris@0: $this->set('modified', $modified); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: }