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