Pull merge.
[yaffs-website] / web / core / modules / aggregator / src / Entity / Item.php
1 <?php
2
3 namespace Drupal\aggregator\Entity;
4
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;
11 use Drupal\Core\Url;
12
13 /**
14  * Defines the aggregator item entity class.
15  *
16  * @ContentEntityType(
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",
25  *   ),
26  *   handlers = {
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"
32  *   },
33  *   uri_callback = "Drupal\aggregator\Entity\Item::buildUri",
34  *   base_table = "aggregator_item",
35  *   render_cache = FALSE,
36  *   list_cache_tags = { "aggregator_feed_list" },
37  *   entity_keys = {
38  *     "id" = "iid",
39  *     "label" = "title",
40  *     "langcode" = "langcode",
41  *   }
42  * )
43  */
44 class Item extends ContentEntityBase implements ItemInterface {
45
46   /**
47    * {@inheritdoc}
48    */
49   public function label() {
50     return $this->get('title')->value;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
57     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
58     $fields = parent::baseFieldDefinitions($entity_type);
59
60     $fields['iid']->setLabel(t('Aggregator item ID'))
61       ->setDescription(t('The ID of the feed item.'));
62
63     $fields['langcode']->setLabel(t('Language code'))
64       ->setDescription(t('The feed item language code.'));
65
66     $fields['fid'] = BaseFieldDefinition::create('entity_reference')
67       ->setLabel(t('Source feed'))
68       ->setRequired(TRUE)
69       ->setDescription(t('The aggregator feed entity associated with this item.'))
70       ->setSetting('target_type', 'aggregator_feed')
71       ->setDisplayOptions('view', [
72         'label' => 'hidden',
73         'type' => 'entity_reference_label',
74         'weight' => 0,
75       ])
76       ->setDisplayConfigurable('form', TRUE);
77
78     $fields['title'] = BaseFieldDefinition::create('string')
79       ->setLabel(t('Title'))
80       ->setDescription(t('The title of the feed item.'));
81
82     $fields['link'] = BaseFieldDefinition::create('uri')
83       ->setLabel(t('Link'))
84       ->setDescription(t('The link of the feed item.'))
85       ->setDisplayOptions('view', [
86         'region' => 'hidden',
87       ])
88       ->setDisplayConfigurable('view', TRUE);
89
90     $fields['author'] = BaseFieldDefinition::create('string')
91       ->setLabel(t('Author'))
92       ->setDescription(t('The author of the feed item.'))
93       ->setDisplayOptions('view', [
94         'label' => 'hidden',
95         'weight' => 3,
96       ])
97       ->setDisplayConfigurable('view', TRUE);
98
99     $fields['description'] = BaseFieldDefinition::create('string_long')
100       ->setLabel(t('Description'))
101       ->setDescription(t('The body of the feed item.'));
102
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', [
107         'label' => 'hidden',
108         'type' => 'timestamp_ago',
109         'weight' => 1,
110       ])
111       ->setDisplayConfigurable('view', TRUE);
112
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.'));
118
119     return $fields;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function getFeedId() {
126     return $this->get('fid')->target_id;
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function setFeedId($fid) {
133     return $this->set('fid', $fid);
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function getTitle() {
140     return $this->get('title')->value;
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   public function setTitle($title) {
147     return $this->set('title', $title);
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function getLink() {
154     return $this->get('link')->value;
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   public function setLink($link) {
161     return $this->set('link', $link);
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function getAuthor() {
168     return $this->get('author')->value;
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function setAuthor($author) {
175     return $this->set('author', $author);
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public function getDescription() {
182     return $this->get('description')->value;
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function setDescription($description) {
189     return $this->set('description', $description);
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public function getPostedTime() {
196     return $this->get('timestamp')->value;
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public function setPostedTime($timestamp) {
203     return $this->set('timestamp', $timestamp);
204   }
205
206   /**
207    * {@inheritdoc}
208    */
209   public function getGuid() {
210     return $this->get('guid')->value;
211   }
212
213   /**
214    * {@inheritdoc}
215    */
216   public function setGuid($guid) {
217     return $this->set('guid', $guid);
218   }
219
220   /**
221    * {@inheritdoc}
222    */
223   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
224     parent::postSave($storage, $update);
225
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.
230     if (!$update) {
231       Cache::invalidateTags($this->getCacheTagsToInvalidate());
232     }
233   }
234
235   /**
236    * {@inheritdoc}
237    */
238   public function getCacheTagsToInvalidate() {
239     return Feed::load($this->getFeedId())->getCacheTags();
240   }
241
242   /**
243    * Entity URI callback.
244    */
245   public static function buildUri(ItemInterface $item) {
246     return Url::fromUri($item->getLink());
247   }
248
249 }