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 Use \Drupal\Core\Entity\ContentEntityForm instead.
16 class RevisionableContentEntityForm extends ContentEntityForm {
19 * The entity being used by this form.
21 * @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\RevisionLogInterface
28 protected function prepareEntity() {
29 parent::prepareEntity();
31 $bundle_entity = $this->getBundleEntity();
33 // Set up default values, if required.
34 if (!$this->entity->isNew()) {
35 $this->entity->setRevisionLogMessage(NULL);
38 if ($bundle_entity instanceof RevisionableEntityBundleInterface) {
39 // Always use the default revision setting.
40 $this->entity->setNewRevision($bundle_entity && $bundle_entity->shouldCreateNewRevision());
45 * Gets the bundle entity of the current entity.
47 * @return \Drupal\Core\Entity\EntityInterface|null
48 * The bundle entity, or NULL if there is none.
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];
61 public function form(array $form, FormStateInterface $form_state) {
62 $entity_type = $this->entity->getEntityType();
63 $bundle_entity = $this->getBundleEntity();
64 $account = $this->currentUser();
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(),
74 '#type' => 'vertical_tabs',
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'] = [
84 '#title' => $this->t('Revision information'),
85 // Open by default when "Create new revision" is checked.
86 '#open' => $this->entity->isNewRevision(),
87 '#group' => 'advanced',
89 '#access' => $this->entity->isNewRevision() || $account->hasPermission($entity_type->get('admin_permission')),
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')),
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'] = [
105 'textarea[name="revision_log"]' => ['empty' => FALSE],
110 $form['revision_information']['revision_log'] = [
111 '#type' => 'textarea',
112 '#title' => $this->t('Revision log message'),
114 '#default_value' => $this->entity->getRevisionLogMessage(),
115 '#description' => $this->t('Briefly describe the changes you have made.'),
118 return parent::form($form, $form_state);
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();
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()];
138 $logger->notice('@type: added %info.', $context);
139 drupal_set_message($this->t('@type %info has been created.', $t_args));
142 $logger->notice('@type: updated %info.', $context);
143 drupal_set_message($this->t('@type %info has been updated.', $t_args));
146 if ($this->entity->id()) {
147 $form_state->setValue('id', $this->entity->id());
148 $form_state->set('id', $this->entity->id());
150 if ($this->entity->getEntityType()->hasLinkTemplate('collection')) {
151 $form_state->setRedirectUrl($this->entity->toUrl('collection'));
154 $form_state->setRedirectUrl($this->entity->toUrl('canonical'));
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();