Same filename in this branch
- 10 core/modules/shortcut/src/Entity/Shortcut.php
- 10 core/modules/shortcut/src/Plugin/migrate/source/d7/Shortcut.php
Same filename and directory in other branches
- 8.9.x core/modules/shortcut/src/Entity/Shortcut.php
- 9 core/modules/shortcut/src/Entity/Shortcut.php
Namespace
Drupal\shortcut\Entity
File
core/modules/shortcut/src/Entity/Shortcut.php
View source
<?php
namespace Drupal\shortcut\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\link\LinkItemInterface;
use Drupal\shortcut\ShortcutInterface;
class Shortcut extends ContentEntityBase implements ShortcutInterface {
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($link_title) {
$this
->set('title', $link_title);
return $this;
}
public function getWeight() {
return $this
->get('weight')->value;
}
public function setWeight($weight) {
$this
->set('weight', $weight);
return $this;
}
public function getUrl() {
return $this->link
->first()
->getUrl();
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
Cache::invalidateTags($this
->getCacheTagsToInvalidate());
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['id']
->setDescription(t('The ID of the shortcut.'));
$fields['uuid']
->setDescription(t('The UUID of the shortcut.'));
$fields['shortcut_set']
->setLabel(t('Shortcut set'))
->setDescription(t('The bundle of the shortcut.'));
$fields['langcode']
->setDescription(t('The language code of the shortcut.'));
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the shortcut.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -10,
'settings' => [
'size' => 40,
],
]);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('Weight among shortcuts in the same shortcut set.'));
$fields['link'] = BaseFieldDefinition::create('link')
->setLabel(t('Path'))
->setDescription(t('The location this shortcut points to.'))
->setRequired(TRUE)
->setSettings([
'link_type' => LinkItemInterface::LINK_INTERNAL,
'title' => DRUPAL_DISABLED,
])
->setDisplayOptions('form', [
'type' => 'link_default',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
return $fields;
}
public function getCacheTagsToInvalidate() {
return $this->shortcut_set->entity
->getCacheTags();
}
public static function sort(ShortcutInterface $a, ShortcutInterface $b) {
$a_weight = $a
->getWeight();
$b_weight = $b
->getWeight();
if ($a_weight == $b_weight) {
return strnatcasecmp($a
->getTitle(), $b
->getTitle());
}
return $a_weight <=> $b_weight;
}
}
Classes
Name |
Description |
Shortcut |
Defines the shortcut entity class. |