Problem/Motivation

When implementing custom logic, say you want to associate different delivery companies to your defined delivery methods (using a taxonomy) it would come in handy to be able to add fields to the shipping method.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

attisan created an issue. See original summary.

jsacksick’s picture

Shipping methods are fieldables, there's simply no UI for adding fields.

attisan’s picture

Title: Make shipping methods fieldable » Add field UI to shipping methods

yes, they are - my bad. What I actually meant was adding the "missing" UI.

attisan’s picture

Status: Active » Needs work

Started by removing unknown plugin "integer" in order to be able to add fields at all.

attisan’s picture

kaszarobert’s picture

And what do we need to do to add that missing UI?

Right now the only way I could add fields to shipping methods is by doing all sorts of update hooks which seem really counterproductive compared to clicking around a few Drupal field forms:

/**
 * Implements hook_install().
 */
function sap_b1_install() {
  $entity_type = \Drupal::entityTypeManager()->getDefinition('commerce_shipping_method');
  \Drupal::entityDefinitionUpdateManager()->updateEntityType($entity_type);
}

/**
 * Implements hook_uninstall().
 */
function sap_b1_uninstall() {
  $entity_type = \Drupal::entityTypeManager()->getDefinition('commerce_shipping_method');
  \Drupal::entityDefinitionUpdateManager()->updateEntityType($entity_type);
}

/**
 * Add sap_shipping_type field to commerce_shipping_method.
 */
function sap_b1_update_8001(&$sandbox) {
  $sap_shipping_type = BaseFieldDefinition::create('string')
      ->setLabel(t('SAP ShippingType Code'))
      ->setCardinality(1)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', FALSE)
      ->setDefaultValue(NULL);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition(
      'sap_shipping_type',
      'commerce_shipping_method',
      'sap_b1',
      $sap_shipping_type);
}

/**
 * Implements hook_entity_base_field_info().
 */
function sap_b1_entity_base_field_info(EntityTypeInterface $entity_type) {
 $fields = [];

 if ($entity_type->id() == 'commerce_shipping_method') {
   $fields['sap_shipping_type'] = BaseFieldDefinition::create('string')
     ->setLabel(t('SAP ShippingType Code'))
     ->setCardinality(1)
     ->setDisplayConfigurable('form', TRUE)
     ->setDisplayOptions('form', [
       'type' => 'string_textfield',
       'weight' => 4,
     ])
     ->setDisplayConfigurable('view', FALSE)
     ->setDefaultValue(NULL);
 }

 return $fields;
}
tbkot’s picture

Version: 8.x-2.x-dev » 3.x-dev

tbkot changed the visibility of the branch 3.x to hidden.

tbkot’s picture

Status: Needs work » Needs review

  • jsacksick committed 394cd589 on 3.x authored by tbkot
    feat: #3276238 Add field UI to shipping methods
    
    By: jsacksick
    By:...

jsacksick’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.