comparison core/modules/aggregator/src/Entity/Item.php @ 0:4c8ae668cc8c

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