Version 1
[yaffs-website] / web / core / modules / shortcut / src / Entity / Shortcut.php
1 <?php
2
3 namespace Drupal\shortcut\Entity;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Entity\ContentEntityBase;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\link\LinkItemInterface;
11 use Drupal\shortcut\ShortcutInterface;
12
13 /**
14  * Defines the shortcut entity class.
15  *
16  * @property \Drupal\link\LinkItemInterface link
17  *
18  * @ContentEntityType(
19  *   id = "shortcut",
20  *   label = @Translation("Shortcut link"),
21  *   handlers = {
22  *     "access" = "Drupal\shortcut\ShortcutAccessControlHandler",
23  *     "form" = {
24  *       "default" = "Drupal\shortcut\ShortcutForm",
25  *       "add" = "Drupal\shortcut\ShortcutForm",
26  *       "edit" = "Drupal\shortcut\ShortcutForm",
27  *       "delete" = "Drupal\shortcut\Form\ShortcutDeleteForm"
28  *     },
29  *     "translation" = "Drupal\content_translation\ContentTranslationHandler"
30  *   },
31  *   base_table = "shortcut",
32  *   data_table = "shortcut_field_data",
33  *   translatable = TRUE,
34  *   entity_keys = {
35  *     "id" = "id",
36  *     "uuid" = "uuid",
37  *     "bundle" = "shortcut_set",
38  *     "label" = "title",
39  *     "langcode" = "langcode",
40  *   },
41  *   links = {
42  *     "canonical" = "/admin/config/user-interface/shortcut/link/{shortcut}",
43  *     "delete-form" = "/admin/config/user-interface/shortcut/link/{shortcut}/delete",
44  *     "edit-form" = "/admin/config/user-interface/shortcut/link/{shortcut}",
45  *   },
46  *   list_cache_tags = { "config:shortcut_set_list" },
47  *   bundle_entity_type = "shortcut_set"
48  * )
49  */
50 class Shortcut extends ContentEntityBase implements ShortcutInterface {
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getTitle() {
56     return $this->get('title')->value;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function setTitle($link_title) {
63     $this->set('title', $link_title);
64     return $this;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getWeight() {
71     return $this->get('weight')->value;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function setWeight($weight) {
78     $this->set('weight', $weight);
79     return $this;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getUrl() {
86     return $this->link->first()->getUrl();
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
93     parent::postSave($storage, $update);
94
95     // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
96     // handles the regular cases. The Shortcut entity has one special case: a
97     // newly created shortcut is *also* added to a shortcut set, so we must
98     // invalidate the associated shortcut set's cache tag.
99     if (!$update) {
100       Cache::invalidateTags($this->getCacheTagsToInvalidate());
101     }
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
108     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
109     $fields = parent::baseFieldDefinitions($entity_type);
110
111     $fields['id']->setDescription(t('The ID of the shortcut.'));
112
113     $fields['uuid']->setDescription(t('The UUID of the shortcut.'));
114
115     $fields['shortcut_set']->setLabel(t('Shortcut set'))
116       ->setDescription(t('The bundle of the shortcut.'));
117
118     $fields['langcode']->setDescription(t('The language code of the shortcut.'));
119
120     $fields['title'] = BaseFieldDefinition::create('string')
121       ->setLabel(t('Name'))
122       ->setDescription(t('The name of the shortcut.'))
123       ->setRequired(TRUE)
124       ->setTranslatable(TRUE)
125       ->setSetting('max_length', 255)
126       ->setDisplayOptions('form', [
127         'type' => 'string_textfield',
128         'weight' => -10,
129         'settings' => [
130           'size' => 40,
131         ],
132       ]);
133
134     $fields['weight'] = BaseFieldDefinition::create('integer')
135       ->setLabel(t('Weight'))
136       ->setDescription(t('Weight among shortcuts in the same shortcut set.'));
137
138     $fields['link'] = BaseFieldDefinition::create('link')
139       ->setLabel(t('Path'))
140       ->setDescription(t('The location this shortcut points to.'))
141       ->setRequired(TRUE)
142       ->setSettings([
143         'link_type' => LinkItemInterface::LINK_INTERNAL,
144         'title' => DRUPAL_DISABLED,
145       ])
146       ->setDisplayOptions('form', [
147         'type' => 'link_default',
148         'weight' => 0,
149       ])
150       ->setDisplayConfigurable('form', TRUE);
151
152     return $fields;
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function getCacheTagsToInvalidate() {
159     return $this->shortcut_set->entity->getCacheTags();
160   }
161
162   /**
163    * Sort shortcut objects.
164    *
165    * Callback for uasort().
166    *
167    * @param \Drupal\shortcut\ShortcutInterface $a
168    *   First item for comparison.
169    * @param \Drupal\shortcut\ShortcutInterface $b
170    *   Second item for comparison.
171    *
172    * @return int
173    *   The comparison result for uasort().
174    */
175   public static function sort(ShortcutInterface $a, ShortcutInterface $b) {
176     $a_weight = $a->getWeight();
177     $b_weight = $b->getWeight();
178     if ($a_weight == $b_weight) {
179       return strnatcasecmp($a->getTitle(), $b->getTitle());
180     }
181     return ($a_weight < $b_weight) ? -1 : 1;
182   }
183
184 }