Skip to content

Commit 1ee736e

Browse files
bojanzbojanz
authored andcommitted
Issue #2820122 by itamair, bojanz: Allow collecting a less detailed billing address
1 parent d910579 commit 1ee736e

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

modules/order/commerce_order.module

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
use Drupal\commerce_order\Entity\OrderTypeInterface;
99
use Drupal\commerce_order\Plugin\Field\FieldFormatter\PriceCalculatedFormatter;
10+
use Drupal\Core\Access\AccessResult;
11+
use Drupal\Core\Entity\EntityInterface;
1012
use Drupal\Core\Form\FormStateInterface;
1113
use Drupal\Core\Render\Element;
1214
use Drupal\entity\BundleFieldDefinition;
15+
use Drupal\field\FieldStorageConfigInterface;
1316

1417
/**
1518
* Implements hook_theme().
@@ -194,3 +197,42 @@ function commerce_order_mail($key, &$message, $params) {
194197
$message['subject'] = $params['subject'];
195198
$message['body'][] = $params['body'];
196199
}
200+
201+
/**
202+
* Implements hook_ENTITY_TYPE_access().
203+
*
204+
* Forbids the profile 'address' field from being deletable.
205+
* This is an alternative to locking the field which still leaves
206+
* the field editable.
207+
*/
208+
function commerce_product_field_storage_config_access(FieldStorageConfigInterface $field_storage, $operation) {
209+
if ($field_storage->id() == 'profile.address' && $operation == 'delete') {
210+
return AccessResult::forbidden();
211+
}
212+
return AccessResult::neutral();
213+
}
214+
215+
/**
216+
* Implements hook_entity_operation_alter().
217+
*
218+
* Hides the "Storage settings" operation for the profile 'address' field.
219+
*/
220+
function commerce_product_entity_operation_alter(array &$operations, EntityInterface $entity) {
221+
if ($entity->getEntityTypeId() == 'field_config' && $entity->id() == 'profile.customer.address') {
222+
unset($operations['storage-settings']);
223+
}
224+
}
225+
226+
/**
227+
* Implements hook_form_FORM_ID_alter() for 'field_config_edit_form'.
228+
*
229+
* Hides the 'Available countries' setting for the profile 'address' field.
230+
* The setting is unused because in Commerce it is taken from the store.
231+
*/
232+
function commerce_product_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
233+
/** @var \Drupal\Core\Field\FieldConfigInterface $entity */
234+
$entity = $form_state->getFormObject()->getEntity();
235+
if ($entity->id() == 'profile.customer.address') {
236+
$form['settings']['available_countries']['#access'] = FALSE;
237+
}
238+
}

modules/order/commerce_order.post_update.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
use Drupal\Core\Entity\Entity\EntityFormDisplay;
9+
use Drupal\field\Entity\FieldStorageConfig;
910

1011
/**
1112
* Revert Order views to fix broken Price fields.
@@ -201,3 +202,12 @@ function commerce_order_post_update_7() {
201202

202203
return $message;
203204
}
205+
206+
/**
207+
* Unlock the profile 'address' field.
208+
*/
209+
function commerce_order_post_update_8() {
210+
$field = FieldStorageConfig::loadByName('profile', 'address');
211+
$field->setLocked(FALSE);
212+
$field->save();
213+
}

modules/order/config/install/field.storage.profile.address.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ entity_type: profile
1313
type: address
1414
settings: { }
1515
module: address
16-
locked: true
16+
locked: false
1717
cardinality: 1
1818
translatable: true
1919
indexes: { }

0 commit comments

Comments
 (0)