1. 生成Drupal 8的Config Entiy代码(可以用drupal console,gec命令)
2. 给Entity的class添加额外字段,举例如下:
class MySettingProfile extends ConfigEntityBase implements BaiduSettingProfileInterface {
/**
* The Baidu setting profile ID.
*
* @var string
*/
protected $id;
/**
* The Baidu setting profile label.
*
* @var string
*/
protected $label;
#新增的字段
protected $rate;
protected $code;
}
3. 给Entity的Form添加item,使其编辑的Form可以生成保存项,(在生成的EntityForm文件)举例如下:
class MySettingProfileForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {/* You will need additional form elements for your custom properties. */
....
/* You will need additional form elements for your custom properties. */
#新增的form项
$form['rate'] = [
'#type' => 'textfield',
'#title' => $this->t('Rate'),
'#maxlength' => 255,
'#default_value' => $my_settings_entity->get('rate'),
'#required' => TRUE,
];
}
}
默认的代码有一句提示
/* You will need additional form elements for your custom properties. */
。
4. 【最重要】添加Schema,在[module]/config/schema/your_entity_name.schema.yml添加新的字段定义,举例如下:
module_name.my_setting_profile.*:
type: config_entity
label: 'Baidu setting profile config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string
#新增的
rate:
type: string
label: 'Rate'
code:
type: string
label: 'Code'
定义这个Schema是关键,否则无法存储。
5. 最后清空缓存,添加新的Config Entity节点,就可以给Entity生成额外的字段或属性。