3d3815db7922f172b66b7e6c6e6deb764111dbae
[yaffs-website] / web / core / modules / shortcut / src / Entity / ShortcutSet.php
1 <?php
2
3 namespace Drupal\shortcut\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\shortcut\ShortcutSetInterface;
8
9 /**
10  * Defines the Shortcut set configuration entity.
11  *
12  * @ConfigEntityType(
13  *   id = "shortcut_set",
14  *   label = @Translation("Shortcut set"),
15  *   handlers = {
16  *     "storage" = "Drupal\shortcut\ShortcutSetStorage",
17  *     "access" = "Drupal\shortcut\ShortcutSetAccessControlHandler",
18  *     "list_builder" = "Drupal\shortcut\ShortcutSetListBuilder",
19  *     "form" = {
20  *       "default" = "Drupal\shortcut\ShortcutSetForm",
21  *       "add" = "Drupal\shortcut\ShortcutSetForm",
22  *       "edit" = "Drupal\shortcut\ShortcutSetForm",
23  *       "customize" = "Drupal\shortcut\Form\SetCustomize",
24  *       "delete" = "Drupal\shortcut\Form\ShortcutSetDeleteForm"
25  *     }
26  *   },
27  *   config_prefix = "set",
28  *   bundle_of = "shortcut",
29  *   entity_keys = {
30  *     "id" = "id",
31  *     "label" = "label"
32  *   },
33  *   links = {
34  *     "customize-form" = "/admin/config/user-interface/shortcut/manage/{shortcut_set}/customize",
35  *     "delete-form" = "/admin/config/user-interface/shortcut/manage/{shortcut_set}/delete",
36  *     "edit-form" = "/admin/config/user-interface/shortcut/manage/{shortcut_set}",
37  *     "collection" = "/admin/config/user-interface/shortcut",
38  *   },
39  *   config_export = {
40  *     "id",
41  *     "label",
42  *   }
43  * )
44  */
45 class ShortcutSet extends ConfigEntityBundleBase implements ShortcutSetInterface {
46
47   /**
48    * The machine name for the configuration entity.
49    *
50    * @var string
51    */
52   protected $id;
53
54   /**
55    * The human-readable name of the configuration entity.
56    *
57    * @var string
58    */
59   protected $label;
60
61   /**
62    * {@inheritdoc}
63    */
64   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
65     parent::postSave($storage, $update);
66
67     if (!$update && !$this->isSyncing()) {
68       // Save a new shortcut set with links copied from the user's default set.
69       $default_set = shortcut_default_set();
70       // This is the default set, do not copy shortcuts.
71       if ($default_set->id() != $this->id()) {
72         foreach ($default_set->getShortcuts() as $shortcut) {
73           $shortcut = $shortcut->createDuplicate();
74           $shortcut->enforceIsNew();
75           $shortcut->shortcut_set->target_id = $this->id();
76           $shortcut->save();
77         }
78       }
79     }
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public static function preDelete(EntityStorageInterface $storage, array $entities) {
86     parent::preDelete($storage, $entities);
87
88     foreach ($entities as $entity) {
89       $storage->deleteAssignedShortcutSets($entity);
90
91       // Next, delete the shortcuts for this set.
92       $shortcut_ids = \Drupal::entityQuery('shortcut')
93         ->condition('shortcut_set', $entity->id(), '=')
94         ->execute();
95
96       $controller = \Drupal::entityManager()->getStorage('shortcut');
97       $entities = $controller->loadMultiple($shortcut_ids);
98       $controller->delete($entities);
99     }
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function resetLinkWeights() {
106     $weight = -50;
107     foreach ($this->getShortcuts() as $shortcut) {
108       $shortcut->setWeight(++$weight);
109       $shortcut->save();
110     }
111
112     return $this;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function getShortcuts() {
119     $shortcuts = \Drupal::entityManager()->getStorage('shortcut')->loadByProperties(['shortcut_set' => $this->id()]);
120     uasort($shortcuts, ['\Drupal\shortcut\Entity\Shortcut', 'sort']);
121     return $shortcuts;
122   }
123
124 }