Item.php 5.27 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\FieldDefinition;
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
 *   controllers = {
25
 *     "storage" = "Drupal\aggregator\ItemStorage",
26 27
 *     "view_builder" = "Drupal\aggregator\ItemViewBuilder",
 *     "access" = "Drupal\aggregator\FeedAccessController",
28
 *   },
29
 *   uri_callback = "Drupal\aggregator\Entity\Item::buildUri",
30 31
 *   base_table = "aggregator_item",
 *   fieldable = TRUE,
32
 *   render_cache = FALSE,
33 34 35 36 37 38
 *   entity_keys = {
 *     "id" = "iid",
 *     "label" = "title",
 *   }
 * )
 */
39
class Item extends ContentEntityBase implements ItemInterface {
40 41 42 43

  /**
   * Implements Drupal\Core\Entity\EntityInterface::label().
   */
44
  public function label() {
45 46
    return $this->get('title')->value;
  }
47

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

    $fields['fid'] = FieldDefinition::create('entity_reference')
      ->setLabel(t('Aggregator feed ID'))
      ->setDescription(t('The ID of the aggregator feed.'))
61
      ->setSetting('target_type', 'aggregator_feed');
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

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

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

    $fields['link'] = FieldDefinition::create('uri')
      ->setLabel(t('Link'))
      ->setDescription(t('The link of the feed item.'));

    $fields['author'] = FieldDefinition::create('string')
      ->setLabel(t('Author'))
      ->setDescription(t('The author of the feed item.'));

79
    $fields['description'] = FieldDefinition::create('string_long')
80 81 82
      ->setLabel(t('Description'))
      ->setDescription(t('The body of the feed item.'));

83
    $fields['timestamp'] = FieldDefinition::create('created')
84 85 86 87
      ->setLabel(t('Posted timestamp'))
      ->setDescription(t('Posted date of the feed item, as a Unix timestamp.'));

    // @todo Convert to a real UUID field in https://drupal.org/node/2149851.
88
    $fields['guid'] = FieldDefinition::create('string_long')
89 90 91
      ->setLabel(t('GUID'))
      ->setDescription(t('Unique identifier for the feed item.'));

92 93
    return $fields;
  }
94

95
  /**
96
   * {@inheritdoc}
97 98
   */
  public function getFeedId() {
99
    return $this->get('fid')->target_id;
100 101 102
  }

  /**
103
   * {@inheritdoc}
104 105 106 107 108 109
   */
  public function setFeedId($fid) {
    return $this->set('fid', $fid);
  }

  /**
110
   * {@inheritdoc}
111 112 113 114 115 116
   */
  public function getTitle() {
    return $this->get('title')->value;
  }

  /**
117
   * {@inheritdoc}
118 119 120 121 122 123
   */
  public function setTitle($title) {
    return $this->set('title', $title);
  }

  /**
124
   * {@inheritdoc}
125 126 127 128 129 130
   */
  public function  getLink() {
    return $this->get('link')->value;
  }

  /**
131
   * {@inheritdoc}
132 133 134 135 136 137
   */
  public function setLink($link) {
    return $this->set('link', $link);
  }

  /**
138
   * {@inheritdoc}
139 140 141 142 143 144
   */
  public function getAuthor() {
    return $this->get('author')->value;
  }

  /**
145
   * {@inheritdoc}
146 147 148 149 150 151
   */
  public function setAuthor($author) {
    return $this->set('author', $author);
  }

  /**
152
   * {@inheritdoc}
153 154 155 156 157 158
   */
  public function getDescription() {
    return $this->get('description')->value;
  }

  /**
159
   * {@inheritdoc}
160 161 162 163 164 165
   */
  public function setDescription($description) {
    return $this->set('description', $description);
  }

  /**
166
   * {@inheritdoc}
167 168 169 170 171 172
   */
  public function getPostedTime() {
    return $this->get('timestamp')->value;
  }

  /**
173
   * {@inheritdoc}
174 175 176 177 178 179
   */
  public function setPostedTime($timestamp) {
    return $this->set('timestamp', $timestamp);
  }

  /**
180
   * {@inheritdoc}
181 182 183 184 185 186
   */
  public function getGuid() {
    return $this->get('guid')->value;
  }

  /**
187
   * {@inheritdoc}
188 189 190 191
   */
  public function setGuid($guid) {
    return $this->set('guid', $guid);
  }
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

  /**
   * {@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.
    Cache::invalidateTags($this->getCacheTag());
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTag() {
    return Feed::load($this->getFeedId())->getCacheTag();
  }

  /**
   * {@inheritdoc}
   */
  public function getListCacheTags() {
    return Feed::load($this->getFeedId())->getListCacheTags();
  }


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

228
}