英文:
Create custom field for shipping method in Drupal Commerce 2
问题
我尝试为commerce_shipping_method bundle添加一个名为"Opening Hours"的新字段。我在如何实现这一点方面遇到了困难,而且我尝试过的示例并没有真正对我起作用。事实上,甚至添加一个简单的数值字段对我也没有起作用。
我有2个YML文件在一个自定义模块中,我在其中定义了测试数值字段。
custom_module.field_instance.yml
langcode: en
status: true
dependencies:
config:
- field.storage.commerce_shipping_method.field_opening_hours_x
id: commerce_shipping_method.commerce_shipping_method.field_opening_hours_x
field_name: field_opening_hours_x
entity_type: commerce_shipping_method
bundle: commerce_shipping_method
label: 'Test Numeric Field'
description: 'A test field'
required: false
default_value: { }
default_value_callback: ''
settings: { }
third_party_settings: { }
deleted: false
custom_module.field_storage.yml
langcode: en
status: true
dependencies:
module:
- commerce_shipping
id: commerce_shipping_method.field_opening_hours_x
field_name: field_opening_hours_x
entity_type: commerce_shipping_method
type: decimal
module: number
settings:
precision: 2
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: true
custom_storage: false
当我安装这个模块并进入编辑我的运输方式(/admin/commerce/shipping-methods/manage/2?destination=/admin/commerce/shipping-methods),额外的字段不显示。
如果有任何帮助或指向如何执行此操作的正确文档的指针,将不胜感激。
英文:
I'm trying to add a new field called Opening Hours for commerce_shipping_method bundle. I'm having hard time finding examples how to do this and those I tried didn't really work for me. In fact, not even adding a simple numeric field worked for me.
I have 2 YML files in a custom module where I define the testing numeric field.
custom_module.field_instance.yml
langcode: en
status: true
dependencies:
config:
- field.storage.commerce_shipping_method.field_opening_hours_x
id: commerce_shipping_method.commerce_shipping_method.field_opening_hours_x
field_name: field_opening_hours_x
entity_type: commerce_shipping_method
bundle: commerce_shipping_method
label: 'Test Numeric Field'
description: 'A test field'
required: false
default_value: { }
default_value_callback: ''
settings: { }
third_party_settings: { }
deleted: false
custom_module.field_storage.yml
langcode: en
status: true
dependencies:
module:
- commerce_shipping
id: commerce_shipping_method.field_opening_hours_x
field_name: field_opening_hours_x
entity_type: commerce_shipping_method
type: decimal
module: number
settings:
precision: 2
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: true
custom_storage: false
When I install this module and go to edit my shipping method ( /admin/commerce/shipping-methods/manage/2?destination=/admin/commerce/shipping-methods ), the extra field is not present.
Any help or pointers to the right docs on how to do this would be appreciated.
答案1
得分: 0
以下是您要翻译的内容:
在从Slack商务社区获得一些帮助并查看了Drupal关于字段定义的文章之后,我提出了以下解决方案:
my_commerce.module
这是在表单页面上显示的定义位置。如果我仅在更新钩子中使用它(下面的第二个文件),那么它不起作用,所以我不得不在我的.module文件中复制定义。
/**
* 实现hook_entity_base_field_info()。
*/
function my_commerce_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = array();
// 将文本区域字段添加到商务运输方法
if ($entity_type->id() === 'commerce_shipping_method') {
$fields['opening_hours'] = BaseFieldDefinition::create('text_long')
->setLabel(t('营业时间'))
->setDescription(t('您可以为实际提货/付款点提供营业时间。'))
->setRevisionable(true)
->setTranslatable(true)
->setSetting('target_type', 'commerce_shipping_method')
->setSetting('handler', 'default')
->setSetting('handler', 'default')
->setDisplayOptions('form', array(
'type' => 'text_textarea',
'settings' => array(
'display_label' => true,
),
))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_long',
'weight' => 0,
])
->setCardinality(1)
->setDisplayConfigurable('form', true);
}
return $fields;
}
my_commerce.install
虽然.module文件将在营业时间表单上显示"营业时间"文本区域,但这部分处理将该字段的值保存到数据库中。
/**
* 将营业时间字段添加到所有运输方法。
*/
function my_commerce_update_9006() {
$field_storage_definition = BaseFieldDefinition::create('text_long')
->setLabel(t('营业时间'))
->setDescription(t('您可以为实际提货/付款点提供营业时间。'))
->setRevisionable(true)
->setTranslatable(true)
->setSetting('target_type', 'commerce_shipping_method')
->setSetting('handler', 'default')
->setDisplayOptions('form', array(
'type' => 'text_textarea',
'settings' => array(
'display_label' => true,
),
))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_long',
'weight' => 0,
])
->setCardinality(1)
->setDisplayConfigurable('form', true);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('opening_hours', 'commerce_shipping_method', 'commerce_shipping_method', $field_storage_definition);
}
英文:
After some help from the slack commerce community and reviewing a Drupal article on field definitions, I came up with the following resolution:
my_commerce.module
Here is where the definition that's shown on form page goes. It didn't work if I simply used it in the update hook (second file below), so I had to duplicate the definition in my .module file as well.
/**
* Implements hook_entity_base_field_info().
*/
function my_commerce_entity_base_field_info( EntityTypeInterface $entity_type ) {
$fields = array();
// add opening hours text area field to commerce shipping methods
if ($entity_type->id() === 'commerce_shipping_method') {
$fields['opening_hours'] = BaseFieldDefinition::create( 'text_long' )
->setLabel( t( 'Opening Hours' ) )
->setDescription( t( 'You can provide opening hours to physical pickup/payments points.' ) )
->setRevisionable(true )
->setTranslatable( true )
->setSetting( 'target_type', 'commerce_shipping_method' )
->setSetting( 'handler', 'default' )
->setSetting( 'handler', 'default' )
->setDisplayOptions('form', array(
'type' => 'text_textarea',
'settings' => array(
'display_label' => true,
),
))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_long',
'weight' => 0,
])
->setCardinality( 1 )
->setDisplayConfigurable( 'form', true );
}
return $fields;
}
my_commerce.install
While the .module file will show the Opening Hours textarea on the opening hours form, this part handles saving the value for that field into the database.
/**
* Add opening hours field to all shipping methods.
*/
function my_commerce_update_9006() {
$field_storage_definition = BaseFieldDefinition::create( 'text_long' )
->setLabel( t( 'Opening Hours' ) )
->setDescription( t( 'You can provide opening hours to physical pickup/payments points.' ) )
->setRevisionable(true )
->setTranslatable( true )
->setSetting( 'target_type', 'commerce_shipping_method' )
->setSetting( 'handler', 'default' )
->setDisplayOptions('form', array(
'type' => 'text_textarea',
'settings' => array(
'display_label' => true,
),
))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_long',
'weight' => 0,
])
->setCardinality( 1 )
->setDisplayConfigurable( 'form', true );
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('opening_hours', 'commerce_shipping_method', 'commerce_shipping_method', $field_storage_definition);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论