Skip to content

Commit 01c97ab

Browse files
berdirbojanz
authored andcommitted
Issue #2898258 by bojanz, Berdir: Sync the ProductForm UX with the 8.4.x NodeForm
1 parent f72e638 commit 01c97ab

8 files changed

Lines changed: 88 additions & 82 deletions

File tree

modules/product/commerce_product.post_update.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,35 @@ function commerce_product_post_update_3() {
100100

101101
return $message;
102102
}
103+
104+
/**
105+
* Revert the default product form display.
106+
*/
107+
function commerce_product_post_update_4() {
108+
/** @var \Drupal\commerce\Config\ConfigUpdaterInterface $config_updater */
109+
$config_updater = \Drupal::service('commerce.config_updater');
110+
111+
$views = [
112+
'core.entity_form_display.commerce_product.default.default',
113+
];
114+
$result = $config_updater->revert($views, TRUE);
115+
116+
$success_results = $result->getSucceeded();
117+
$failure_results = $result->getFailed();
118+
$message = '';
119+
if ($success_results) {
120+
$message = t('Succeeded:') . '<br>';
121+
foreach ($success_results as $success_message) {
122+
$message .= $success_message . '<br>';
123+
}
124+
$message .= '<br>';
125+
}
126+
if ($failure_results) {
127+
$message .= t('Failed:') . '<br>';
128+
foreach ($failure_results as $failure_message) {
129+
$message .= $failure_message . '<br>';
130+
}
131+
}
132+
133+
return $message;
134+
}

modules/product/config/install/core.entity_form_display.commerce_product.default.default.yml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,70 @@ content:
2424
summary_rows: 3
2525
placeholder: ''
2626
third_party_settings: { }
27+
region: content
2728
created:
2829
type: datetime_timestamp
29-
weight: 6
30+
weight: 5
31+
settings: { }
32+
third_party_settings: { }
33+
region: content
34+
langcode:
35+
type: language_select
36+
weight: 3
37+
region: content
3038
settings: { }
3139
third_party_settings: { }
3240
path:
3341
type: path
34-
weight: 4
42+
weight: 7
3543
settings: { }
3644
third_party_settings: { }
45+
region: content
46+
status:
47+
type: boolean_checkbox
48+
weight: 8
49+
region: content
50+
settings:
51+
display_label: true
52+
third_party_settings: { }
3753
stores:
3854
type: commerce_entity_select
3955
weight: 0
4056
settings:
57+
hide_single_entity: true
4158
autocomplete_threshold: 7
4259
autocomplete_size: 60
4360
autocomplete_placeholder: ''
4461
third_party_settings: { }
62+
region: content
4563
title:
4664
type: string_textfield
4765
weight: 1
4866
settings:
4967
size: 60
5068
placeholder: ''
5169
third_party_settings: { }
70+
region: content
5271
uid:
5372
type: entity_reference_autocomplete
54-
weight: 5
73+
weight: 4
5574
settings:
5675
match_operator: CONTAINS
5776
size: 60
5877
placeholder: ''
5978
third_party_settings: { }
79+
region: content
6080
variations:
6181
type: inline_entity_form_complex
62-
weight: 3
82+
weight: 6
6383
settings:
6484
match_operator: CONTAINS
6585
override_labels: false
6686
label_singular: ''
6787
label_plural: ''
6888
allow_new: true
6989
allow_existing: false
90+
form_mode: default
7091
third_party_settings: { }
71-
hidden:
72-
status: true
92+
region: content
93+
hidden: { }

modules/product/css/product.form.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
box-sizing: border-box;
1111
}
1212

13+
.layout-region-product-footer__content {
14+
border-top: 1px solid #bebfb9;
15+
padding-top: 0.5em;
16+
margin-top: 1.5em;
17+
}
18+
1319
/* Wide screens */
1420
@media
1521
screen and (min-width: 780px),
@@ -23,6 +29,10 @@
2329
box-sizing: border-box;
2430
}
2531

32+
.layout-region-product-footer__content {
33+
margin-top: 0.5em;
34+
}
35+
2636
[dir="rtl"] .layout-region-product-main,
2737
[dir="rtl"] .layout-region-product-footer {
2838
float: right;

modules/product/src/Entity/Product.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
382382
->setComputed(TRUE);
383383

384384
$fields['status']
385+
->setLabel(t('Published'))
385386
->setDisplayOptions('form', [
386387
'type' => 'boolean_checkbox',
387388
'settings' => [

modules/product/src/Form/ProductForm.php

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Drupal\commerce_product\Form;
44

5-
use Drupal\commerce_product\Entity\ProductInterface;
65
use Drupal\Core\Datetime\DateFormatterInterface;
76
use Drupal\Component\Datetime\TimeInterface;
87
use Drupal\Core\Entity\ContentEntityForm;
@@ -85,13 +84,21 @@ public function form(array $form, FormStateInterface $form_state) {
8584
$form['#tree'] = TRUE;
8685
$form['#theme'] = ['commerce_product_form'];
8786
$form['#attached']['library'][] = 'commerce_product/form';
88-
$form['#entity_builders']['update_status'] = [get_class($this), 'updateStatus'];
8987
// Changed must be sent to the client, for later overwrite error checking.
9088
$form['changed'] = [
9189
'#type' => 'hidden',
9290
'#default_value' => $product->getChangedTime(),
9391
];
9492

93+
$form['footer'] = [
94+
'#type' => 'container',
95+
'#weight' => 99,
96+
'#attributes' => [
97+
'class' => ['product-form-footer'],
98+
],
99+
];
100+
$form['status']['#group'] = 'footer';
101+
95102
$last_saved = t('Not saved yet');
96103
if (!$product->isNew()) {
97104
$last_saved = $this->dateFormatter->format($product->getChangedTime(), 'short');
@@ -208,74 +215,6 @@ public static function hideEmptyVisibilitySettings(array $form) {
208215
return $form;
209216
}
210217

211-
/**
212-
* Entity builder: updates the product status with the submitted value.
213-
*
214-
* @param string $entity_type
215-
* The entity type.
216-
* @param \Drupal\commerce_product\Entity\ProductInterface $product
217-
* The product updated with the submitted values.
218-
* @param array $form
219-
* The complete form array.
220-
* @param \Drupal\Core\Form\FormStateInterface $form_state
221-
* The current state of the form.
222-
*
223-
* @see \Drupal\node\NodeForm::form()
224-
*/
225-
public static function updateStatus($entity_type, ProductInterface $product, array $form, FormStateInterface $form_state) {
226-
$element = $form_state->getTriggeringElement();
227-
if (isset($element['#published_status'])) {
228-
$product->setPublished($element['#published_status']);
229-
}
230-
}
231-
232-
/**
233-
* {@inheritdoc}
234-
*/
235-
protected function actions(array $form, FormStateInterface $form_state) {
236-
$element = parent::actions($form, $form_state);
237-
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
238-
$product = $this->entity;
239-
240-
$element['delete']['#access'] = $product->access('delete');
241-
$element['delete']['#weight'] = 100;
242-
// Add a "Publish" button.
243-
$element['publish'] = $element['submit'];
244-
$element['publish']['#published_status'] = TRUE;
245-
$element['publish']['#dropbutton'] = 'save';
246-
$element['publish']['#weight'] = 0;
247-
// Add an "Unpublish" button.
248-
$element['unpublish'] = $element['submit'];
249-
$element['unpublish']['#published_status'] = FALSE;
250-
$element['unpublish']['#dropbutton'] = 'save';
251-
$element['unpublish']['#weight'] = 10;
252-
// isNew | prev status » primary & publish label & unpublish label
253-
// 1 | 1 » publish & Save and publish & Save as unpublished
254-
// 1 | 0 » unpublish & Save and publish & Save as unpublished
255-
// 0 | 1 » publish & Save and keep published & Save and unpublish
256-
// 0 | 0 » unpublish & Save and keep unpublished & Save and publish.
257-
if ($product->isNew()) {
258-
$element['publish']['#value'] = $this->t('Save and publish');
259-
$element['unpublish']['#value'] = $this->t('Save as unpublished');
260-
}
261-
else {
262-
$element['publish']['#value'] = $product->isPublished() ? $this->t('Save and keep published') : $this->t('Save and publish');
263-
$element['unpublish']['#value'] = !$product->isPublished() ? $this->t('Save and keep unpublished') : $this->t('Save and unpublish');
264-
}
265-
// Set the primary button based on the published status.
266-
if ($product->isPublished()) {
267-
unset($element['unpublish']['#button_type']);
268-
}
269-
else {
270-
unset($element['publish']['#button_type']);
271-
$element['unpublish']['#weight'] = -10;
272-
}
273-
// Hide the now unneeded "Save" button.
274-
$element['submit']['#access'] = FALSE;
275-
276-
return $element;
277-
}
278-
279218
/**
280219
* {@inheritdoc}
281220
*/

modules/product/templates/commerce-product-form.html.twig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
#}
1212
<div class="layout-product-form clearfix">
1313
<div class="layout-region layout-region-product-main">
14-
{{ form|without('advanced', 'actions') }}
14+
{{ form|without('advanced', 'footer', 'actions') }}
1515
</div>
1616
<div class="layout-region layout-region-product-secondary">
1717
{{ form.advanced }}
1818
</div>
1919
<div class="layout-region layout-region-product-footer">
20-
{{ form.actions }}
20+
<div class="layout-region-product-footer__content">
21+
{{ form.footer }}
22+
{{ form.actions }}
23+
</div>
2124
</div>
2225
</div>

modules/product/tests/src/Functional/ProductAdminTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testCreateProduct() {
4141
'variations[form][inline_entity_form][status][value]' => 1,
4242
];
4343
$this->submitForm($variations_edit, t('Create variation'));
44-
$this->submitForm($edit, t('Save and publish'));
44+
$this->submitForm($edit, t('Save'));
4545

4646
$result = \Drupal::entityQuery('commerce_product')
4747
->condition("title", $edit['title[0][value]'])
@@ -108,7 +108,7 @@ public function testEditProduct() {
108108
'variations[form][inline_entity_form][entities][0][form][status][value]' => 1,
109109
];
110110
$this->submitForm($variations_edit, 'Update variation');
111-
$this->submitForm($edit, 'Save and keep published');
111+
$this->submitForm($edit, 'Save');
112112

113113
\Drupal::service('entity_type.manager')->getStorage('commerce_product_variation')->resetCache([$variation->id()]);
114114
$variation = ProductVariation::load($variation->id());

tests/src/Functional/EntitySelectWidgetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function testWidget() {
102102
// Check store 1.
103103
$edit['stores[target_id][value][' . $store_ids[0] . ']'] = $store_ids[0];
104104
$edit['stores[target_id][value][' . $store_ids[1] . ']'] = FALSE;
105-
$this->submitForm($edit, t('Save and keep published'));
105+
$this->submitForm($edit, t('Save'));
106106
$this->assertSession()->statusCodeEquals(200);
107107
\Drupal::entityTypeManager()->getStorage('commerce_product')->resetCache();
108108
$this->product = Product::load($this->product->id());
@@ -130,7 +130,7 @@ public function testWidget() {
130130
// Reference both stores 1 and 2.
131131
$edit = [];
132132
$edit['stores[target_id][value]'] = $store_labels[0] . ', ' . $store_labels[1];
133-
$this->submitForm($edit, t('Save and keep published'));
133+
$this->submitForm($edit, t('Save'));
134134
$this->assertSession()->statusCodeEquals(200);
135135
\Drupal::entityTypeManager()->getStorage('commerce_product')->resetCache();
136136
$this->product = Product::load($this->product->id());

0 commit comments

Comments
 (0)