56efb8608dd126e2997c097f9c21676f35b6f1e8
[yaffs-website] / web / core / modules / node / src / Entity / NodeType.php
1 <?php
2
3 namespace Drupal\node\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\node\NodeTypeInterface;
8
9 /**
10  * Defines the Node type configuration entity.
11  *
12  * @ConfigEntityType(
13  *   id = "node_type",
14  *   label = @Translation("Content type"),
15  *   label_collection = @Translation("Content types"),
16  *   label_singular = @Translation("content type"),
17  *   label_plural = @Translation("content types"),
18  *   label_count = @PluralTranslation(
19  *     singular = "@count content type",
20  *     plural = "@count content types",
21  *   ),
22  *   handlers = {
23  *     "access" = "Drupal\node\NodeTypeAccessControlHandler",
24  *     "form" = {
25  *       "add" = "Drupal\node\NodeTypeForm",
26  *       "edit" = "Drupal\node\NodeTypeForm",
27  *       "delete" = "Drupal\node\Form\NodeTypeDeleteConfirm"
28  *     },
29  *     "list_builder" = "Drupal\node\NodeTypeListBuilder",
30  *   },
31  *   admin_permission = "administer content types",
32  *   config_prefix = "type",
33  *   bundle_of = "node",
34  *   entity_keys = {
35  *     "id" = "type",
36  *     "label" = "name"
37  *   },
38  *   links = {
39  *     "edit-form" = "/admin/structure/types/manage/{node_type}",
40  *     "delete-form" = "/admin/structure/types/manage/{node_type}/delete",
41  *     "collection" = "/admin/structure/types",
42  *   },
43  *   config_export = {
44  *     "name",
45  *     "type",
46  *     "description",
47  *     "help",
48  *     "new_revision",
49  *     "preview_mode",
50  *     "display_submitted",
51  *   }
52  * )
53  */
54 class NodeType extends ConfigEntityBundleBase implements NodeTypeInterface {
55
56   /**
57    * The machine name of this node type.
58    *
59    * @var string
60    *
61    * @todo Rename to $id.
62    */
63   protected $type;
64
65   /**
66    * The human-readable name of the node type.
67    *
68    * @var string
69    *
70    * @todo Rename to $label.
71    */
72   protected $name;
73
74   /**
75    * A brief description of this node type.
76    *
77    * @var string
78    */
79   protected $description;
80
81   /**
82    * Help information shown to the user when creating a Node of this type.
83    *
84    * @var string
85    */
86   protected $help;
87
88   /**
89    * Default value of the 'Create new revision' checkbox of this node type.
90    *
91    * @var bool
92    */
93   protected $new_revision = TRUE;
94
95   /**
96    * The preview mode.
97    *
98    * @var int
99    */
100   protected $preview_mode = DRUPAL_OPTIONAL;
101
102   /**
103    * Display setting for author and date Submitted by post information.
104    *
105    * @var bool
106    */
107   protected $display_submitted = TRUE;
108
109   /**
110    * {@inheritdoc}
111    */
112   public function id() {
113     return $this->type;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function isLocked() {
120     $locked = \Drupal::state()->get('node.type.locked');
121     return isset($locked[$this->id()]) ? $locked[$this->id()] : FALSE;
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function isNewRevision() {
128     return $this->new_revision;
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public function setNewRevision($new_revision) {
135     $this->new_revision = $new_revision;
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function displaySubmitted() {
142     return $this->display_submitted;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function setDisplaySubmitted($display_submitted) {
149     $this->display_submitted = $display_submitted;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function getPreviewMode() {
156     return $this->preview_mode;
157   }
158
159   /**
160    * {@inheritdoc}
161    */
162   public function setPreviewMode($preview_mode) {
163     $this->preview_mode = $preview_mode;
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function getHelp() {
170     return $this->help;
171   }
172
173   /**
174    * {@inheritdoc}
175    */
176   public function getDescription() {
177     return $this->description;
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
184     parent::postSave($storage, $update);
185
186     if ($update && $this->getOriginalId() != $this->id()) {
187       $update_count = node_type_update_nodes($this->getOriginalId(), $this->id());
188       if ($update_count) {
189         \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($update_count,
190           'Changed the content type of 1 post from %old-type to %type.',
191           'Changed the content type of @count posts from %old-type to %type.',
192           [
193             '%old-type' => $this->getOriginalId(),
194             '%type' => $this->id(),
195           ]));
196       }
197     }
198     if ($update) {
199       // Clear the cached field definitions as some settings affect the field
200       // definitions.
201       $this->entityManager()->clearCachedFieldDefinitions();
202     }
203   }
204
205   /**
206    * {@inheritdoc}
207    */
208   public static function postDelete(EntityStorageInterface $storage, array $entities) {
209     parent::postDelete($storage, $entities);
210
211     // Clear the node type cache to reflect the removal.
212     $storage->resetCache(array_keys($entities));
213   }
214
215   /**
216    * {@inheritdoc}
217    */
218   public function shouldCreateNewRevision() {
219     return $this->isNewRevision();
220   }
221
222 }