I have a content type 'asset' witch contains a select list with a taxonomy reference 'category'
我有一个内容类型‘ASSET’,其中包含一个带有分类引用‘CATEGORY’的选择列表
In the taxonomy 'category' I have 2 fields: 'name' and 'photo'.
在分类“类别”中,我有两个字段:“姓名”和“照片”。
Now I want to have the photo of the taxonomy also visible in form display of the asset page.
现在,我希望在资产页面的表单显示中也能看到分类的照片。
Is this possible without coding?
没有编码,这是可能的吗?
更多回答
优秀答案推荐
No additional coding is needed - this can be added in a custom module using hook_form_alter()
.
不需要额外的编码--可以使用HOOK_FORM_ALTER()将其添加到定制模块中。
In order to dynamically display the taxonomy image in the node edit form when a category is selected, load the selected category term using its ID from the field value. Check if the term has an image set in the field_image field. If so, get the image file URI and add an image field to display it. Set the title to the category name.
为了在选择类别时在节点编辑表单中动态显示分类图像,请使用字段值中的ID加载所选类别术语。检查术语的field_Image字段中是否设置了图像。如果是,则获取图像文件URI并添加一个图像字段来显示它。将标题设置为类别名称。
Example:
示例:
function mymodule_form_asset_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
// Get the selected category term ID from the field value.
$category_tid = $form['field_category']['widget']['#default_value'][0]['target_id'];
// Load the category term entity.
$term = \Drupal\taxonomy\Entity\Term::load($category_tid);
// Check if the term has an image.
if (!$term->get('field_image')->isEmpty()) {
// Get the image URI.
$image_uri = $term->get('field_image')->entity->getFileUri();
// Add the image to the form.
$form['category_image'] = [
'#type' => 'image',
'#title' => $term->getName(),
'#uri' => $image_uri,
];
}
}
更多回答
我是一名优秀的程序员,十分优秀!