diff --git a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php index 4f101a0..ad46e39 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php @@ -22,6 +22,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\field_ui\FieldUI; /** * Field UI display overview base class. @@ -126,6 +127,8 @@ public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entit $this->bundleEntityTypeId = $target_entity_type->getBundleEntityType(); $this->targetBundle = $route_paramateres[$this->bundleEntityTypeId]->id(); } + + return $this->getEntityDisplay($route_match->getParameter($this->displayContext . '_mode_name')); } /** @@ -586,7 +589,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { // it with those from the 'default' view mode, which were used so // far. if (!$this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->targetEntityTypeId . '.' . $this->targetBundle . '.' . $mode)) { - $display = $this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->targetEntityTypeId . '.' . $this->targetBundle . '.' . 'default')->createCopy($mode); + $display = $this->getEntityDisplay('default')->createCopy($mode); $display->save(); } @@ -606,6 +609,19 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /** * {@inheritdoc} */ + public function save(array $form, FormStateInterface $form_state) { + // Work around the fact that entity display are never guaranteed to be + // present, not even the 'default' one. + if (!$this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->entity->id())) { + $this->entity->enforceIsNew(); + } + + parent::save($form, $form_state); + } + + /** + * {@inheritdoc} + */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); @@ -896,6 +912,17 @@ protected function getExtraFields() { } /** + * Returns an entity display object to be used by this form. + * + * @param string $mode + * A view or form mode. + * + * @return \Drupal\Core\Entity\Display\EntityDisplayInterface + * An entity display. + */ + abstract protected function getEntityDisplay($mode); + + /** * Returns the widget or formatter plugin for a field. * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition diff --git a/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php index 9a6f57e..86864af 100644 --- a/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php +++ b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php @@ -39,15 +39,6 @@ public static function create(ContainerInterface $container) { /** * {@inheritdoc} */ - public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) { - parent::getEntityFromRouteMatch($route_match, $entity_type_id); - - return entity_get_form_display($this->targetEntityTypeId, $this->targetBundle, $route_match->getParameter('form_mode_name')); - } - - /** - * {@inheritdoc} - */ protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) { $field_row = parent::buildFieldRow($field_definition, $form, $form_state); @@ -66,6 +57,13 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, arr /** * {@inheritdoc} */ + protected function getEntityDisplay($mode) { + return entity_get_form_display($this->targetEntityTypeId, $this->targetBundle, $mode); + } + + /** + * {@inheritdoc} + */ protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration) { $plugin = NULL; diff --git a/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php index a441ad0..7b9ee1a 100644 --- a/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php +++ b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php @@ -39,15 +39,6 @@ public static function create(ContainerInterface $container) { /** * {@inheritdoc} */ - public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) { - parent::getEntityFromRouteMatch($route_match, $entity_type_id); - - return entity_get_display($this->targetEntityTypeId, $this->targetBundle, $route_match->getParameter('view_mode_name')); - } - - /** - * {@inheritdoc} - */ protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) { $field_row = parent::buildFieldRow($field_definition, $form, $form_state); @@ -99,6 +90,13 @@ protected function buildExtraFieldRow($field_id, $extra_field) { /** * {@inheritdoc} */ + protected function getEntityDisplay($mode) { + return entity_get_display($this->targetEntityTypeId, $this->targetBundle, $mode); + } + + /** + * {@inheritdoc} + */ protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration) { $plugin = NULL;