3 namespace Drupal\entity\Form;
5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Entity\RevisionableEntityBundleInterface;
7 use Drupal\Core\Form\FormStateInterface;
9 @trigger_error('\Drupal\entity\Form\RevisionableContentEntityForm has been deprecated in favor of \Drupal\Core\Entity\ContentEntityForm. Use that instead.');
12 * Extends the base entity form with revision support in the UI.
14 * @deprecated in favor of \Drupal\Core\Entity\ContentEntityForm. Use that
17 class RevisionableContentEntityForm extends ContentEntityForm {
20 * The entity being used by this form.
22 * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface|\Drupal\entity\Revision\EntityRevisionLogInterface
29 protected function prepareEntity() {
30 parent::prepareEntity();
32 $bundle_entity = $this->getBundleEntity();
34 // Set up default values, if required.
35 if (!$this->entity->isNew()) {
36 $this->entity->setRevisionLogMessage(NULL);
39 if ($bundle_entity instanceof RevisionableEntityBundleInterface) {
40 // Always use the default revision setting.
41 $this->entity->setNewRevision($bundle_entity && $bundle_entity->shouldCreateNewRevision());
46 * Returns the bundle entity of the entity, or NULL if there is none.
48 * @return \Drupal\Core\Entity\EntityInterface|null
50 protected function getBundleEntity() {
51 if ($bundle_key = $this->entity->getEntityType()->getKey('bundle')) {
52 return $this->entity->{$bundle_key}->referencedEntities()[0];
60 public function form(array $form, FormStateInterface $form_state) {
61 $entity_type = $this->entity->getEntityType();
62 $bundle_entity = $this->getBundleEntity();
63 $account = $this->currentUser();
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(),
73 '#type' => 'vertical_tabs',
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'] = [
83 '#title' => $this->t('Revision information'),
84 // Open by default when "Create new revision" is checked.
85 '#open' => $this->entity->isNewRevision(),
86 '#group' => 'advanced',
88 '#access' => $this->entity->isNewRevision() || $account->hasPermission($entity_type->get('admin_permission')),
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')),
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'] = [
104 'textarea[name="revision_log"]' => ['empty' => FALSE],
109 $form['revision_information']['revision_log'] = [
110 '#type' => 'textarea',
111 '#title' => $this->t('Revision log message'),
113 '#default_value' => $this->entity->getRevisionLogMessage(),
114 '#description' => $this->t('Briefly describe the changes you have made.'),
117 return parent::form($form, $form_state);
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();
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()];
137 $logger->notice('@type: added %info.', $context);
138 drupal_set_message($this->t('@type %info has been created.', $t_args));
141 $logger->notice('@type: updated %info.', $context);
142 drupal_set_message($this->t('@type %info has been updated.', $t_args));
145 if ($this->entity->id()) {
146 $form_state->setValue('id', $this->entity->id());
147 $form_state->set('id', $this->entity->id());
149 if ($this->entity->getEntityType()->hasLinkTemplate('collection')) {
150 $form_state->setRedirectUrl($this->entity->toUrl('collection'));
153 $form_state->setRedirectUrl($this->entity->toUrl('canonical'));
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();