Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / Form / RevisionableContentEntityForm.php
1 <?php
2
3 namespace Drupal\entity\Form;
4
5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Entity\RevisionableEntityBundleInterface;
7 use Drupal\Core\Form\FormStateInterface;
8
9 @trigger_error('\Drupal\entity\Form\RevisionableContentEntityForm has been deprecated in favor of \Drupal\Core\Entity\ContentEntityForm. Use that instead.');
10
11 /**
12  * Extends the base entity form with revision support in the UI.
13  *
14  * @deprecated Use \Drupal\Core\Entity\ContentEntityForm instead.
15  */
16 class RevisionableContentEntityForm extends ContentEntityForm {
17
18   /**
19    * The entity being used by this form.
20    *
21    * @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\RevisionLogInterface
22    */
23   protected $entity;
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function prepareEntity() {
29     parent::prepareEntity();
30
31     $bundle_entity = $this->getBundleEntity();
32
33     // Set up default values, if required.
34     if (!$this->entity->isNew()) {
35       $this->entity->setRevisionLogMessage(NULL);
36     }
37
38     if ($bundle_entity instanceof RevisionableEntityBundleInterface) {
39       // Always use the default revision setting.
40       $this->entity->setNewRevision($bundle_entity && $bundle_entity->shouldCreateNewRevision());
41     }
42   }
43
44   /**
45    * Gets the bundle entity of the current entity.
46    *
47    * @return \Drupal\Core\Entity\EntityInterface|null
48    *   The bundle entity, or NULL if there is none.
49    */
50   protected function getBundleEntity() {
51     if ($this->entity->getEntityType()->getBundleEntityType()) {
52       $bundle_key = $this->entity->getEntityType()->getKey('bundle');
53       return $this->entity->{$bundle_key}->referencedEntities()[0];
54     }
55     return NULL;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function form(array $form, FormStateInterface $form_state) {
62     $entity_type = $this->entity->getEntityType();
63     $bundle_entity = $this->getBundleEntity();
64     $account = $this->currentUser();
65
66     if ($this->operation == 'edit') {
67       $form['#title'] = $this->t('Edit %bundle_label @label', [
68         '%bundle_label' => $bundle_entity ? $bundle_entity->label() : '',
69         '@label' => $this->entity->label(),
70       ]);
71     }
72
73     $form['advanced'] = [
74       '#type' => 'vertical_tabs',
75       '#weight' => 99,
76     ];
77
78     // Add a log field if the "Create new revision" option is checked, or if the
79     // current user has the ability to check that option.
80     // @todo Could we autogenerate this form by using some widget on the
81     //   revision info field.
82     $form['revision_information'] = [
83       '#type' => 'details',
84       '#title' => $this->t('Revision information'),
85       // Open by default when "Create new revision" is checked.
86       '#open' => $this->entity->isNewRevision(),
87       '#group' => 'advanced',
88       '#weight' => 20,
89       '#access' => $this->entity->isNewRevision() || $account->hasPermission($entity_type->get('admin_permission')),
90     ];
91
92     $form['revision_information']['revision'] = [
93       '#type' => 'checkbox',
94       '#title' => $this->t('Create new revision'),
95       '#default_value' => $this->entity->isNewRevision(),
96       '#access' => $account->hasPermission($entity_type->get('admin_permission')),
97     ];
98
99     // Check the revision log checkbox when the log textarea is filled in.
100     // This must not happen if "Create new revision" is enabled by default,
101     // since the state would auto-disable the checkbox otherwise.
102     if (!$this->entity->isNewRevision()) {
103       $form['revision_information']['revision']['#states'] = [
104         'checked' => [
105           'textarea[name="revision_log"]' => ['empty' => FALSE],
106         ],
107       ];
108     }
109
110     $form['revision_information']['revision_log'] = [
111       '#type' => 'textarea',
112       '#title' => $this->t('Revision log message'),
113       '#rows' => 4,
114       '#default_value' => $this->entity->getRevisionLogMessage(),
115       '#description' => $this->t('Briefly describe the changes you have made.'),
116     ];
117
118     return parent::form($form, $form_state);
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function save(array $form, FormStateInterface $form_state) {
125     // Save as a new revision if requested to do so.
126     if (!$form_state->isValueEmpty('revision')) {
127       $this->entity->setNewRevision();
128     }
129
130     $insert = $this->entity->isNew();
131     $this->entity->save();
132     $context = ['@type' => $this->entity->bundle(), '%info' => $this->entity->label()];
133     $logger = $this->logger('content');
134     $bundle_entity = $this->getBundleEntity();
135     $t_args = ['@type' => $bundle_entity ? $bundle_entity->label() : 'None', '%info' => $this->entity->label()];
136
137     if ($insert) {
138       $logger->notice('@type: added %info.', $context);
139       drupal_set_message($this->t('@type %info has been created.', $t_args));
140     }
141     else {
142       $logger->notice('@type: updated %info.', $context);
143       drupal_set_message($this->t('@type %info has been updated.', $t_args));
144     }
145
146     if ($this->entity->id()) {
147       $form_state->setValue('id', $this->entity->id());
148       $form_state->set('id', $this->entity->id());
149
150       if ($this->entity->getEntityType()->hasLinkTemplate('collection')) {
151         $form_state->setRedirectUrl($this->entity->toUrl('collection'));
152       }
153       else {
154         $form_state->setRedirectUrl($this->entity->toUrl('canonical'));
155       }
156     }
157     else {
158       // In the unlikely case something went wrong on save, the entity will be
159       // rebuilt and entity form redisplayed.
160       drupal_set_message($this->t('The entity could not be saved.'), 'error');
161       $form_state->setRebuild();
162     }
163   }
164
165 }