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