Item.php 6.04 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\aggregator\Entity\Item.
6
7
 */

8
namespace Drupal\aggregator\Entity;
9

10
use Drupal\Core\Cache\Cache;
11
use Drupal\Core\Entity\ContentEntityBase;
12
use Drupal\Core\Entity\EntityStorageInterface;
13
use Drupal\aggregator\ItemInterface;
14
use Drupal\Core\Entity\EntityTypeInterface;
15
use Drupal\Core\Field\BaseFieldDefinition;
16
use Drupal\Core\Url;
17
18
19
20

/**
 * Defines the aggregator item entity class.
 *
21
 * @ContentEntityType(
22
23
 *   id = "aggregator_item",
 *   label = @Translation("Aggregator feed item"),
24
 *   handlers = {
25
 *     "storage" = "Drupal\aggregator\ItemStorage",
26
 *     "storage_schema" = "Drupal\aggregator\ItemStorageSchema",
27
 *     "view_builder" = "Drupal\aggregator\ItemViewBuilder",
28
 *     "access" = "Drupal\aggregator\FeedAccessControlHandler",
29
 *     "views_data" = "Drupal\aggregator\AggregatorItemViewsData"
30
 *   },
31
 *   uri_callback = "Drupal\aggregator\Entity\Item::buildUri",
32
 *   base_table = "aggregator_item",
33
 *   render_cache = FALSE,
34
 *   list_cache_tags = { "aggregator_feed_list" },
35
36
37
 *   entity_keys = {
 *     "id" = "iid",
 *     "label" = "title",
38
 *     "langcode" = "langcode",
39
40
41
 *   }
 * )
 */
42
class Item extends ContentEntityBase implements ItemInterface {
43
44

  /**
45
   * {@inheritdoc}
46
   */
47
  public function label() {
48
49
    return $this->get('title')->value;
  }
50

51
52
53
  /**
   * {@inheritdoc}
   */
54
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
55
    $fields['iid'] = BaseFieldDefinition::create('integer')
56
57
      ->setLabel(t('Aggregator item ID'))
      ->setDescription(t('The ID of the feed item.'))
58
59
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
60

61
    $fields['fid'] = BaseFieldDefinition::create('entity_reference')
62
      ->setLabel(t('Source feed'))
63
      ->setRequired(TRUE)
64
65
66
67
68
69
70
71
      ->setDescription(t('The aggregator feed entity associated with this item.'))
      ->setSetting('target_type', 'aggregator_feed')
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'entity_reference_label',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('form', TRUE);
72

73
    $fields['title'] = BaseFieldDefinition::create('string')
74
75
76
      ->setLabel(t('Title'))
      ->setDescription(t('The title of the feed item.'));

77
    $fields['langcode'] = BaseFieldDefinition::create('language')
78
79
80
      ->setLabel(t('Language code'))
      ->setDescription(t('The feed item language code.'));

81
    $fields['link'] = BaseFieldDefinition::create('uri')
82
      ->setLabel(t('Link'))
83
84
85
86
87
      ->setDescription(t('The link of the feed item.'))
      ->setDisplayOptions('view', array(
        'type' => 'hidden',
      ))
      ->setDisplayConfigurable('view', TRUE);
88

89
    $fields['author'] = BaseFieldDefinition::create('string')
90
      ->setLabel(t('Author'))
91
92
93
94
95
96
      ->setDescription(t('The author of the feed item.'))
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'weight' => 3,
      ))
      ->setDisplayConfigurable('view', TRUE);
97

98
    $fields['description'] = BaseFieldDefinition::create('string_long')
99
100
101
      ->setLabel(t('Description'))
      ->setDescription(t('The body of the feed item.'));

102
    $fields['timestamp'] = BaseFieldDefinition::create('created')
103
104
105
106
107
108
109
110
      ->setLabel(t('Posted on'))
      ->setDescription(t('Posted date of the feed item, as a Unix timestamp.'))
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'timestamp_ago',
        'weight' => 1,
      ))
      ->setDisplayConfigurable('view', TRUE);
111

112
113
    // @todo Convert to a real UUID field in
    //   https://www.drupal.org/node/2149851.
114
    $fields['guid'] = BaseFieldDefinition::create('string_long')
115
116
117
      ->setLabel(t('GUID'))
      ->setDescription(t('Unique identifier for the feed item.'));

118
119
    return $fields;
  }
120

121
  /**
122
   * {@inheritdoc}
123
124
   */
  public function getFeedId() {
125
    return $this->get('fid')->target_id;
126
127
128
  }

  /**
129
   * {@inheritdoc}
130
131
132
133
134
135
   */
  public function setFeedId($fid) {
    return $this->set('fid', $fid);
  }

  /**
136
   * {@inheritdoc}
137
138
139
140
141
142
   */
  public function getTitle() {
    return $this->get('title')->value;
  }

  /**
143
   * {@inheritdoc}
144
145
146
147
148
149
   */
  public function setTitle($title) {
    return $this->set('title', $title);
  }

  /**
150
   * {@inheritdoc}
151
   */
152
  public function getLink() {
153
154
155
156
    return $this->get('link')->value;
  }

  /**
157
   * {@inheritdoc}
158
159
160
161
162
163
   */
  public function setLink($link) {
    return $this->set('link', $link);
  }

  /**
164
   * {@inheritdoc}
165
166
167
168
169
170
   */
  public function getAuthor() {
    return $this->get('author')->value;
  }

  /**
171
   * {@inheritdoc}
172
173
174
175
176
177
   */
  public function setAuthor($author) {
    return $this->set('author', $author);
  }

  /**
178
   * {@inheritdoc}
179
180
181
182
183
184
   */
  public function getDescription() {
    return $this->get('description')->value;
  }

  /**
185
   * {@inheritdoc}
186
187
188
189
190
191
   */
  public function setDescription($description) {
    return $this->set('description', $description);
  }

  /**
192
   * {@inheritdoc}
193
194
195
196
197
198
   */
  public function getPostedTime() {
    return $this->get('timestamp')->value;
  }

  /**
199
   * {@inheritdoc}
200
201
202
203
204
205
   */
  public function setPostedTime($timestamp) {
    return $this->set('timestamp', $timestamp);
  }

  /**
206
   * {@inheritdoc}
207
208
209
210
211
212
   */
  public function getGuid() {
    return $this->get('guid')->value;
  }

  /**
213
   * {@inheritdoc}
214
215
216
217
   */
  public function setGuid($guid) {
    return $this->set('guid', $guid);
  }
218
219
220
221
222
223
224
225
226
227
228

  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

    // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
    // handles the regular cases. The Item entity has one special case: a newly
    // created Item is *also* associated with a Feed, so we must invalidate the
    // associated Feed's cache tag.
229
230
231
    if (!$update) {
      Cache::invalidateTags($this->getCacheTagsToInvalidate());
    }
232
233
234
235
236
  }

  /**
   * {@inheritdoc}
   */
237
  public function getCacheTagsToInvalidate() {
238
    return Feed::load($this->getFeedId())->getCacheTags();
239
240
241
242
243
244
245
  }


  /**
   * Entity URI callback.
   */
  public static function buildUri(ItemInterface $item) {
246
    return Url::fromUri($item->getLink());
247
248
  }

249
}