diff -u b/core/lib/Drupal/Core/Entity/ContentEntityForm.php b/core/lib/Drupal/Core/Entity/ContentEntityForm.php --- b/core/lib/Drupal/Core/Entity/ContentEntityForm.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityForm.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Entity; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Form\FormStateInterface; @@ -29,13 +30,34 @@ protected $entity; /** + * The entity type bundle info service. + * + * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface + */ + protected $entityTypeBundleInfo; + + /** + * The time service. + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; + + /** * Constructs a ContentEntityForm object. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info + * The entity type bundle service. + * @param \Drupal\Component\Datetime\TimeInterface $time + * The time service. */ - public function __construct(EntityManagerInterface $entity_manager) { + public function __construct(EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) { $this->entityManager = $entity_manager; + + $this->entityTypeBundleInfo = $entity_type_bundle_info ? $entity_type_bundle_info : \Drupal::service('entity_type.bundle.info'); + $this->time = $time ? $time : \Drupal::service('datetime.time'); } /** @@ -43,7 +65,9 @@ */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager') + $container->get('entity.manager'), + $container->get('entity_type.bundle.info'), + $container->get('datetime.time') ); } @@ -125,19 +149,14 @@ // Mark the entity as requiring validation. $entity->setValidationRequired(!$form_state->getTemporaryValue('entity_validated')); - if ($this->showRevisionUi()) { - // Save as a new revision if requested to do so. - if (!$form_state->isValueEmpty('revision')) { - $entity->setNewRevision(); - if ($entity instanceof RevisionLogInterface) { - // If a new revision is created, save the current user as - // revision author. - $entity->setRevisionUserId(\Drupal::currentUser()->id()); - $entity->setRevisionCreationTime(\Drupal::time()->getRequestTime()); - } - } - else { - $entity->setNewRevision(FALSE); + // Save as a new revision if requested to do so. + if ($this->showRevisionUi() && !$form_state->isValueEmpty('revision')) { + $entity->setNewRevision(); + if ($entity instanceof RevisionLogInterface) { + // If a new revision is created, save the current user as + // revision author. + $entity->setRevisionUserId(\Drupal::currentUser()->id()); + $entity->setRevisionCreationTime(\Drupal::time()->getRequestTime()); } } @@ -348,7 +367,7 @@ */ public function updateChangedTime(EntityInterface $entity) { if ($entity->getEntityType()->isSubclassOf(EntityChangedInterface::class)) { - $entity->setChangedTime(\Drupal::time()->getRequestTime()); + $entity->setChangedTime($this->time->getRequestTime()); } } @@ -361,8 +380,8 @@ public function addRevisionableFormFields(&$form) { $entity_type = $this->entity->getEntityType(); - if ($this->operation == 'edit') { - $bundle_info = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id()); + if (!$this->entity->isNew()) { + $bundle_info = $this->entityTypeBundleInfo->getBundleInfo($entity_type->id()); if ($bundle_info[$this->entity->bundle()]) { $form['#title'] = $this->t('Edit @bundle_label @label', [ '@bundle_label' => $bundle_info[$this->entity->bundle()]['label'], @@ -376,12 +395,7 @@ } } - $new_revision_default = FALSE; - $bundle_entity = $this->getBundleEntity(); - if ($bundle_entity instanceof RevisionableEntityBundleInterface) { - // Always use the default revision setting. - $new_revision_default = $bundle_entity->shouldCreateNewRevision(); - } + $new_revision_default = $this->getNewRevisionDefault(); // Add a log field if the "Create new revision" option is checked, or if the // current user has the ability to check that option. @@ -422,6 +436,21 @@ } } + /** + * Should new revisions created on default. + * + * @return bool + * New revision on default. + */ + protected function getNewRevisionDefault() { + $new_revision_default = FALSE; + $bundle_entity = $this->getBundleEntity(); + if ($bundle_entity instanceof RevisionableEntityBundleInterface) { + // Always use the default revision setting. + $new_revision_default = $bundle_entity->shouldCreateNewRevision(); + } + return $new_revision_default; + } /** * Checks whether the revision form fields should be added to the form. diff -u /dev/null b/core/modules/block_content/block_content.libraries.yml --- /dev/null +++ b/core/modules/block_content/block_content.libraries.yml @@ -0,0 +1,6 @@ +# Deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. +# This is just for BC and not used anymore. See 2669802 +drupal.block_content: + version: VERSION + dependencies: + - core/drupal.entity-form diff -u b/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php --- b/core/modules/block_content/src/BlockContentForm.php +++ b/core/modules/block_content/src/BlockContentForm.php @@ -41,40 +41,48 @@ * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { - $insert = $this->entity->isNew(); - $this->entity->save(); - $context = ['@type' => $this->entity->bundle(), '%info' => $this->entity->label()]; - $logger = $this->logger($this->entity->getEntityTypeId()); - $bundle_entity = $this->getBundleEntity(); - $t_args = ['@type' => $bundle_entity ? $bundle_entity->label() : $this->entity->getEntityType()->getLabel(), '%label' => $this->entity->label()]; + $block = $this->entity; + + $insert = $block->isNew(); + $block->save(); + $context = array('@type' => $block->bundle(), '%info' => $block->label()); + $logger = $this->logger('block_content'); + $block_type = $this->entityTypeManager->getStorage('block_content_type')->load($block->bundle()); + $t_args = array('@type' => $block_type->label(), '%info' => $block->label()); if ($insert) { $logger->notice('@type: added %info.', $context); - drupal_set_message($this->t('@type %label has been created.', $t_args)); + drupal_set_message($this->t('@type %info has been created.', $t_args)); } else { $logger->notice('@type: updated %info.', $context); - drupal_set_message($this->t('@type %label has been updated.', $t_args)); + drupal_set_message($this->t('@type %info has been updated.', $t_args)); } - if ($this->entity->getEntityType()->hasLinkTemplate('collection')) { - $form_state->setRedirectUrl($this->entity->toUrl('collection')); + if ($block->id()) { + $form_state->setValue('id', $block->id()); + $form_state->set('id', $block->id()); + if ($insert) { + if (!$theme = $block->getTheme()) { + $theme = $this->config('system.theme')->get('default'); + } + $form_state->setRedirect( + 'block.admin_add', + array( + 'plugin_id' => 'block_content:' . $block->uuid(), + 'theme' => $theme, + ) + ); + } + else { + $form_state->setRedirectUrl($block->urlInfo('collection')); + } } else { - $form_state->setRedirectUrl($this->entity->toUrl('canonical')); - } - - if ($insert) { - if (!$theme = $this->entity->getTheme()) { - $theme = $this->config('system.theme')->get('default'); - } - $form_state->setRedirect( - 'block.admin_add', - [ - 'plugin_id' => 'block_content:' . $this->entity->uuid(), - 'theme' => $theme, - ] - ); + // In the unlikely case something went wrong on save, the block will be + // rebuilt and block form redisplayed. + drupal_set_message($this->t('The block could not be saved.'), 'error'); + $form_state->setRebuild(); } } diff -u b/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php --- b/core/modules/node/src/NodeForm.php +++ b/core/modules/node/src/NodeForm.php @@ -4,7 +4,6 @@ use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\user\PrivateTempStoreFactory; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -12,7 +11,7 @@ /** * Form handler for the node edit forms. */ -class NodeForm extends ContentEntityForm { +class NodeForm extends ContentEntityForm { /** * The tempstore factory. @@ -86,8 +85,6 @@ /** @var \Drupal\node\NodeInterface $node */ $node = $this->entity; - $current_user = $this->currentUser(); - // Changed must be sent to the client, for later overwrite error checking. $form['changed'] = array( '#type' => 'hidden', @@ -99,6 +96,4 @@ $form['advanced']['#attributes']['class'][] = 'entity-meta'; - $form['revision']['#access'] = $current_user->hasPermission('administer nodes') && !$node->isNew(); - // Node author information for administrators. $form['author'] = array(