3 namespace Drupal\entity\Form;
5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\entity\Entity\RevisionableEntityBundleInterface;
10 * Extends the base entity form with revision support in the UI.
12 class RevisionableContentEntityForm extends ContentEntityForm {
15 * The entity being used by this form.
17 * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface|\Drupal\entity\Revision\EntityRevisionLogInterface
24 protected function prepareEntity() {
25 parent::prepareEntity();
27 $bundle_entity = $this->getBundleEntity();
29 // Set up default values, if required.
30 if (!$this->entity->isNew()) {
31 $this->entity->setRevisionLogMessage(NULL);
34 if ($bundle_entity instanceof RevisionableEntityBundleInterface) {
35 // Always use the default revision setting.
36 $this->entity->setNewRevision($bundle_entity && $bundle_entity->shouldCreateNewRevision());
41 * Returns the bundle entity of the entity, or NULL if there is none.
43 * @return \Drupal\Core\Entity\EntityInterface|null
45 protected function getBundleEntity() {
46 if ($bundle_key = $this->entity->getEntityType()->getKey('bundle')) {
47 return $this->entity->{$bundle_key}->referencedEntities()[0];
55 public function form(array $form, FormStateInterface $form_state) {
56 $entity_type = $this->entity->getEntityType();
57 $bundle_entity = $this->getBundleEntity();
58 $account = $this->currentUser();
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(),
68 '#type' => 'vertical_tabs',
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'] = [
78 '#title' => $this->t('Revision information'),
79 // Open by default when "Create new revision" is checked.
80 '#open' => $this->entity->isNewRevision(),
81 '#group' => 'advanced',
83 '#access' => $this->entity->isNewRevision() || $account->hasPermission($entity_type->get('admin_permission')),
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')),
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'] = [
99 'textarea[name="revision_log"]' => ['empty' => FALSE],
104 $form['revision_information']['revision_log'] = [
105 '#type' => 'textarea',
106 '#title' => $this->t('Revision log message'),
108 '#default_value' => $this->entity->getRevisionLogMessage(),
109 '#description' => $this->t('Briefly describe the changes you have made.'),
112 return parent::form($form, $form_state);
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();
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()];
132 $logger->notice('@type: added %info.', $context);
133 drupal_set_message($this->t('@type %info has been created.', $t_args));
136 $logger->notice('@type: updated %info.', $context);
137 drupal_set_message($this->t('@type %info has been updated.', $t_args));
140 if ($this->entity->id()) {
141 $form_state->setValue('id', $this->entity->id());
142 $form_state->set('id', $this->entity->id());
144 if ($this->entity->getEntityType()->hasLinkTemplate('collection')) {
145 $form_state->setRedirectUrl($this->entity->toUrl('collection'));
148 $form_state->setRedirectUrl($this->entity->toUrl('canonical'));
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();