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"),
19 * label_collection = @Translation("Aggregator feed items"),
20 * label_singular = @Translation("aggregator feed item"),
21 * label_plural = @Translation("aggregator feed items"),
22 * label_count = @PluralTranslation(
23 * singular = "@count aggregator feed item",
24 * plural = "@count aggregator feed items",
27 * "storage" = "Drupal\aggregator\ItemStorage",
28 * "storage_schema" = "Drupal\aggregator\ItemStorageSchema",
29 * "view_builder" = "Drupal\aggregator\ItemViewBuilder",
30 * "access" = "Drupal\aggregator\FeedAccessControlHandler",
31 * "views_data" = "Drupal\aggregator\AggregatorItemViewsData"
33 * uri_callback = "Drupal\aggregator\Entity\Item::buildUri",
34 * base_table = "aggregator_item",
35 * render_cache = FALSE,
36 * list_cache_tags = { "aggregator_feed_list" },
40 * "langcode" = "langcode",
44 class Item extends ContentEntityBase implements ItemInterface {
49 public function label() {
50 return $this->get('title')->value;
56 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
57 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
58 $fields = parent::baseFieldDefinitions($entity_type);
60 $fields['iid']->setLabel(t('Aggregator item ID'))
61 ->setDescription(t('The ID of the feed item.'));
63 $fields['langcode']->setLabel(t('Language code'))
64 ->setDescription(t('The feed item language code.'));
66 $fields['fid'] = BaseFieldDefinition::create('entity_reference')
67 ->setLabel(t('Source feed'))
69 ->setDescription(t('The aggregator feed entity associated with this item.'))
70 ->setSetting('target_type', 'aggregator_feed')
71 ->setDisplayOptions('view', [
73 'type' => 'entity_reference_label',
76 ->setDisplayConfigurable('form', TRUE);
78 $fields['title'] = BaseFieldDefinition::create('string')
79 ->setLabel(t('Title'))
80 ->setDescription(t('The title of the feed item.'));
82 $fields['link'] = BaseFieldDefinition::create('uri')
84 ->setDescription(t('The link of the feed item.'))
85 ->setDisplayOptions('view', [
88 ->setDisplayConfigurable('view', TRUE);
90 $fields['author'] = BaseFieldDefinition::create('string')
91 ->setLabel(t('Author'))
92 ->setDescription(t('The author of the feed item.'))
93 ->setDisplayOptions('view', [
97 ->setDisplayConfigurable('view', TRUE);
99 $fields['description'] = BaseFieldDefinition::create('string_long')
100 ->setLabel(t('Description'))
101 ->setDescription(t('The body of the feed item.'));
103 $fields['timestamp'] = BaseFieldDefinition::create('created')
104 ->setLabel(t('Posted on'))
105 ->setDescription(t('Posted date of the feed item, as a Unix timestamp.'))
106 ->setDisplayOptions('view', [
108 'type' => 'timestamp_ago',
111 ->setDisplayConfigurable('view', TRUE);
113 // @todo Convert to a real UUID field in
114 // https://www.drupal.org/node/2149851.
115 $fields['guid'] = BaseFieldDefinition::create('string_long')
116 ->setLabel(t('GUID'))
117 ->setDescription(t('Unique identifier for the feed item.'));
125 public function getFeedId() {
126 return $this->get('fid')->target_id;
132 public function setFeedId($fid) {
133 return $this->set('fid', $fid);
139 public function getTitle() {
140 return $this->get('title')->value;
146 public function setTitle($title) {
147 return $this->set('title', $title);
153 public function getLink() {
154 return $this->get('link')->value;
160 public function setLink($link) {
161 return $this->set('link', $link);
167 public function getAuthor() {
168 return $this->get('author')->value;
174 public function setAuthor($author) {
175 return $this->set('author', $author);
181 public function getDescription() {
182 return $this->get('description')->value;
188 public function setDescription($description) {
189 return $this->set('description', $description);
195 public function getPostedTime() {
196 return $this->get('timestamp')->value;
202 public function setPostedTime($timestamp) {
203 return $this->set('timestamp', $timestamp);
209 public function getGuid() {
210 return $this->get('guid')->value;
216 public function setGuid($guid) {
217 return $this->set('guid', $guid);
223 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
224 parent::postSave($storage, $update);
226 // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
227 // handles the regular cases. The Item entity has one special case: a newly
228 // created Item is *also* associated with a Feed, so we must invalidate the
229 // associated Feed's cache tag.
231 Cache::invalidateTags($this->getCacheTagsToInvalidate());
238 public function getCacheTagsToInvalidate() {
239 return Feed::load($this->getFeedId())->getCacheTags();
243 * Entity URI callback.
245 public static function buildUri(ItemInterface $item) {
246 return Url::fromUri($item->getLink());