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