3 namespace Drupal\aggregator\Entity;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Entity\ContentEntityBase;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\aggregator\ItemInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\BaseFieldDefinition;
14 * Defines the aggregator item entity class.
17 * id = "aggregator_item",
18 * label = @Translation("Aggregator feed item"),
20 * "storage" = "Drupal\aggregator\ItemStorage",
21 * "storage_schema" = "Drupal\aggregator\ItemStorageSchema",
22 * "view_builder" = "Drupal\aggregator\ItemViewBuilder",
23 * "access" = "Drupal\aggregator\FeedAccessControlHandler",
24 * "views_data" = "Drupal\aggregator\AggregatorItemViewsData"
26 * uri_callback = "Drupal\aggregator\Entity\Item::buildUri",
27 * base_table = "aggregator_item",
28 * render_cache = FALSE,
29 * list_cache_tags = { "aggregator_feed_list" },
33 * "langcode" = "langcode",
37 class Item extends ContentEntityBase implements ItemInterface {
42 public function label() {
43 return $this->get('title')->value;
49 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
50 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
51 $fields = parent::baseFieldDefinitions($entity_type);
53 $fields['iid']->setLabel(t('Aggregator item ID'))
54 ->setDescription(t('The ID of the feed item.'));
56 $fields['langcode']->setLabel(t('Language code'))
57 ->setDescription(t('The feed item language code.'));
59 $fields['fid'] = BaseFieldDefinition::create('entity_reference')
60 ->setLabel(t('Source feed'))
62 ->setDescription(t('The aggregator feed entity associated with this item.'))
63 ->setSetting('target_type', 'aggregator_feed')
64 ->setDisplayOptions('view', [
66 'type' => 'entity_reference_label',
69 ->setDisplayConfigurable('form', TRUE);
71 $fields['title'] = BaseFieldDefinition::create('string')
72 ->setLabel(t('Title'))
73 ->setDescription(t('The title of the feed item.'));
75 $fields['link'] = BaseFieldDefinition::create('uri')
77 ->setDescription(t('The link of the feed item.'))
78 ->setDisplayOptions('view', [
81 ->setDisplayConfigurable('view', TRUE);
83 $fields['author'] = BaseFieldDefinition::create('string')
84 ->setLabel(t('Author'))
85 ->setDescription(t('The author of the feed item.'))
86 ->setDisplayOptions('view', [
90 ->setDisplayConfigurable('view', TRUE);
92 $fields['description'] = BaseFieldDefinition::create('string_long')
93 ->setLabel(t('Description'))
94 ->setDescription(t('The body of the feed item.'));
96 $fields['timestamp'] = BaseFieldDefinition::create('created')
97 ->setLabel(t('Posted on'))
98 ->setDescription(t('Posted date of the feed item, as a Unix timestamp.'))
99 ->setDisplayOptions('view', [
101 'type' => 'timestamp_ago',
104 ->setDisplayConfigurable('view', TRUE);
106 // @todo Convert to a real UUID field in
107 // https://www.drupal.org/node/2149851.
108 $fields['guid'] = BaseFieldDefinition::create('string_long')
109 ->setLabel(t('GUID'))
110 ->setDescription(t('Unique identifier for the feed item.'));
118 public function getFeedId() {
119 return $this->get('fid')->target_id;
125 public function setFeedId($fid) {
126 return $this->set('fid', $fid);
132 public function getTitle() {
133 return $this->get('title')->value;
139 public function setTitle($title) {
140 return $this->set('title', $title);
146 public function getLink() {
147 return $this->get('link')->value;
153 public function setLink($link) {
154 return $this->set('link', $link);
160 public function getAuthor() {
161 return $this->get('author')->value;
167 public function setAuthor($author) {
168 return $this->set('author', $author);
174 public function getDescription() {
175 return $this->get('description')->value;
181 public function setDescription($description) {
182 return $this->set('description', $description);
188 public function getPostedTime() {
189 return $this->get('timestamp')->value;
195 public function setPostedTime($timestamp) {
196 return $this->set('timestamp', $timestamp);
202 public function getGuid() {
203 return $this->get('guid')->value;
209 public function setGuid($guid) {
210 return $this->set('guid', $guid);
216 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
217 parent::postSave($storage, $update);
219 // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
220 // handles the regular cases. The Item entity has one special case: a newly
221 // created Item is *also* associated with a Feed, so we must invalidate the
222 // associated Feed's cache tag.
224 Cache::invalidateTags($this->getCacheTagsToInvalidate());
231 public function getCacheTagsToInvalidate() {
232 return Feed::load($this->getFeedId())->getCacheTags();
237 * Entity URI callback.
239 public static function buildUri(ItemInterface $item) {
240 return Url::fromUri($item->getLink());