Answers:
使用entity_field.manager服务,您可以获取BaseFieldDefinition类的数组。该服务也可以注入,但是对于复制粘贴工作代码,您可以执行以下操作:
$bundle_fields = \Drupal::getContainer()->get('entity_field.manager')->getFieldDefinitions($entity_type, $bundle);
$field_definition = $bundle_fields[$field_name];
$catalog_id = $field_definition->getSetting($setting_name);
使用字段实例:
$settings = $field->getFieldDefinition()->getSettings();
例如,在字段小部件中:
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = [];
// Get the settings.
$settings = $items[$delta]->getFieldDefinition()->getSettings();
// ... build render array
return $element;
}
获取字段配置的最简单方法是使用EntityTypeManager。
$entity_type = 'node';// node, taxonomy_term, taxonomy_vocabulary, ...
$bundle = 'article';// the content type machine name
$field = 'field_article_tags';// the field machine name
$fieldConfig = \Drupal::entityTypeManager()
->getStorage('field_config')
->load($entity_type . '.' . $bundle . '.' . $field);