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

/**
 * @file
5
 * Contains \Drupal\shortcut\Entity\Shortcut.
6 7 8 9
 */

namespace Drupal\shortcut\Entity;

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

/**
 * Defines the shortcut entity class.
 *
21
 * @ContentEntityType(
22 23 24
 *   id = "shortcut",
 *   label = @Translation("Shortcut link"),
 *   controllers = {
25
 *     "access" = "Drupal\shortcut\ShortcutAccessControlHandler",
26
 *     "form" = {
27 28
 *       "default" = "Drupal\shortcut\ShortcutForm",
 *       "add" = "Drupal\shortcut\ShortcutForm",
29
 *       "edit" = "Drupal\shortcut\ShortcutForm",
30 31
 *       "delete" = "Drupal\shortcut\Form\ShortcutDeleteForm"
 *     },
32
 *     "translation" = "Drupal\content_translation\ContentTranslationHandler"
33 34 35 36 37 38 39 40 41 42 43
 *   },
 *   base_table = "shortcut",
 *   data_table = "shortcut_field_data",
 *   translatable = TRUE,
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "bundle" = "shortcut_set",
 *     "label" = "title"
 *   },
 *   links = {
44 45 46
 *     "canonical" = "entity.shortcut.canonical",
 *     "delete-form" = "entity.shortcut.delete_form",
 *     "edit-form" = "entity.shortcut.canonical",
47
 *   },
48 49
 *   bundle_entity_type = "shortcut_set",
 *   field_ui_base_route = "entity.shortcut.canonical",
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 * )
 */
class Shortcut extends ContentEntityBase implements ShortcutInterface {

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this->get('title')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setTitle($link_title) {
    $this->set('title', $link_title);
   return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getWeight() {
    return $this->get('weight')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setWeight($weight) {
    $this->set('weight', $weight);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteName() {
    return $this->get('route_name')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setRouteName($route_name) {
    $this->set('route_name', $route_name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteParams() {
103
    return $this->get('route_parameters')->first()->getValue();
104 105 106 107 108 109
  }

  /**
   * {@inheritdoc}
   */
  public function setRouteParams($route_parameters) {
110
    $this->set('route_parameters', array($route_parameters));
111 112 113 114 115 116
    return $this;
  }

  /**
   * {@inheritdoc}
   */
117 118
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    parent::preCreate($storage, $values);
119 120 121 122 123 124 125 126 127

    if (!isset($values['shortcut_set'])) {
      $values['shortcut_set'] = 'default';
    }
  }

  /**
   * {@inheritdoc}
   */
128 129
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
130

131 132 133
    $url = Url::createFromPath($this->path->value);
    $this->setRouteName($url->getRouteName());
    $this->setRouteParams($url->getRouteParameters());
134 135
  }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

    // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
    // handles the regular cases. The Shortcut entity has one special case: a
    // newly created shortcut is *also* added to a shortcut set, so we must
    // invalidate the associated shortcut set's cache tag.
    if (!$update) {
      Cache::invalidateTags($this->getCacheTag());
    }
  }

151 152 153
  /**
   * {@inheritdoc}
   */
154
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
155
    $fields['id'] = BaseFieldDefinition::create('integer')
156 157
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the shortcut.'))
158 159
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
160

161
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
162 163 164 165
      ->setLabel(t('UUID'))
      ->setDescription(t('The UUID of the shortcut.'))
      ->setReadOnly(TRUE);

166
    $fields['shortcut_set'] = BaseFieldDefinition::create('entity_reference')
167 168 169 170 171
      ->setLabel(t('Shortcut set'))
      ->setDescription(t('The bundle of the shortcut.'))
      ->setSetting('target_type', 'shortcut_set')
      ->setRequired(TRUE);

172
    $fields['title'] = BaseFieldDefinition::create('string')
173
      ->setLabel(t('Name'))
174
      ->setDescription(t('The name of the shortcut.'))
175 176
      ->setRequired(TRUE)
      ->setTranslatable(TRUE)
177
      ->setDefaultValue('')
178
      ->setSetting('max_length', 255)
179 180 181 182 183 184 185
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -10,
        'settings' => array(
          'size' => 40,
        ),
      ));
186

187
    $fields['weight'] = BaseFieldDefinition::create('integer')
188 189 190
      ->setLabel(t('Weight'))
      ->setDescription(t('Weight among shortcuts in the same shortcut set.'));

191
    $fields['route_name'] = BaseFieldDefinition::create('string')
192 193 194
      ->setLabel(t('Route name'))
      ->setDescription(t('The machine name of a defined Route this shortcut represents.'));

195
    $fields['route_parameters'] = BaseFieldDefinition::create('map')
196 197 198
      ->setLabel(t('Route parameters'))
      ->setDescription(t('A serialized array of route parameters of this shortcut.'));

199
    $fields['langcode'] = BaseFieldDefinition::create('language')
200 201 202
      ->setLabel(t('Language code'))
      ->setDescription(t('The language code of the shortcut.'));

203
    $fields['path'] = BaseFieldDefinition::create('string')
204 205
      ->setLabel(t('Path'))
      ->setDescription(t('The computed shortcut path.'))
206 207
      ->setComputed(TRUE)
      ->setCustomStorage(TRUE);
208 209

    $item_definition = $fields['path']->getItemDefinition();
210
    $item_definition->setClass('\Drupal\shortcut\ShortcutPathItem');
211 212 213 214 215
    $fields['path']->setItemDefinition($item_definition);

    return $fields;
  }

216 217 218 219 220 221 222 223 224 225 226 227 228 229
  /**
   * {@inheritdoc}
   */
  public function getCacheTag() {
    return $this->shortcut_set->entity->getCacheTag();
  }

  /**
   * {@inheritdoc}
   */
  public function getListCacheTags() {
    return $this->shortcut_set->entity->getListCacheTags();
  }

230
}