3 namespace Drupal\shortcut\Entity;
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;
14 * Defines the shortcut entity class.
16 * @property \Drupal\link\LinkItemInterface link
20 * label = @Translation("Shortcut link"),
22 * "access" = "Drupal\shortcut\ShortcutAccessControlHandler",
24 * "default" = "Drupal\shortcut\ShortcutForm",
25 * "add" = "Drupal\shortcut\ShortcutForm",
26 * "edit" = "Drupal\shortcut\ShortcutForm",
27 * "delete" = "Drupal\shortcut\Form\ShortcutDeleteForm"
29 * "translation" = "Drupal\content_translation\ContentTranslationHandler"
31 * base_table = "shortcut",
32 * data_table = "shortcut_field_data",
33 * translatable = TRUE,
37 * "bundle" = "shortcut_set",
39 * "langcode" = "langcode",
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}",
46 * list_cache_tags = { "config:shortcut_set_list" },
47 * bundle_entity_type = "shortcut_set"
50 class Shortcut extends ContentEntityBase implements ShortcutInterface {
55 public function getTitle() {
56 return $this->get('title')->value;
62 public function setTitle($link_title) {
63 $this->set('title', $link_title);
70 public function getWeight() {
71 return $this->get('weight')->value;
77 public function setWeight($weight) {
78 $this->set('weight', $weight);
85 public function getUrl() {
86 return $this->link->first()->getUrl();
92 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
93 parent::postSave($storage, $update);
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.
100 Cache::invalidateTags($this->getCacheTagsToInvalidate());
107 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
108 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
109 $fields = parent::baseFieldDefinitions($entity_type);
111 $fields['id']->setDescription(t('The ID of the shortcut.'));
113 $fields['uuid']->setDescription(t('The UUID of the shortcut.'));
115 $fields['shortcut_set']->setLabel(t('Shortcut set'))
116 ->setDescription(t('The bundle of the shortcut.'));
118 $fields['langcode']->setDescription(t('The language code of the shortcut.'));
120 $fields['title'] = BaseFieldDefinition::create('string')
121 ->setLabel(t('Name'))
122 ->setDescription(t('The name of the shortcut.'))
124 ->setTranslatable(TRUE)
125 ->setSetting('max_length', 255)
126 ->setDisplayOptions('form', [
127 'type' => 'string_textfield',
134 $fields['weight'] = BaseFieldDefinition::create('integer')
135 ->setLabel(t('Weight'))
136 ->setDescription(t('Weight among shortcuts in the same shortcut set.'));
138 $fields['link'] = BaseFieldDefinition::create('link')
139 ->setLabel(t('Path'))
140 ->setDescription(t('The location this shortcut points to.'))
143 'link_type' => LinkItemInterface::LINK_INTERNAL,
144 'title' => DRUPAL_DISABLED,
146 ->setDisplayOptions('form', [
147 'type' => 'link_default',
150 ->setDisplayConfigurable('form', TRUE);
158 public function getCacheTagsToInvalidate() {
159 return $this->shortcut_set->entity->getCacheTags();
163 * Sort shortcut objects.
165 * Callback for uasort().
167 * @param \Drupal\shortcut\ShortcutInterface $a
168 * First item for comparison.
169 * @param \Drupal\shortcut\ShortcutInterface $b
170 * Second item for comparison.
173 * The comparison result for uasort().
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());
181 return ($a_weight < $b_weight) ? -1 : 1;