如何在drupal 8中的自定义实体的图像字段中添加alt和title字段

问题描述 投票:0回答:2

我创建了一个带有图像字段的自定义实体。但是我无法显示alt和title字段。

这是我的代码:

$fields['main_img'] = BaseFieldDefinition::create('image')
  ->setLabel(t('Main image of the hardware'))
  ->setSettings([
    'file_directory' => 'hardware',
    'file_extensions' => 'png jpg jpeg',
  ])
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'image',
    'weight' => -30,
  ))
  ->setDisplayOptions('form', array(
    'label' => 'above',
    'type' => 'image_image',
    'weight' => -30,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

你能告诉我如何显示我的图像的alt和title字段,也许有人知道文档的用途,因为我找不到它?

谢谢你们

image drupal-8 fieldtype
2个回答
0
投票

我用$ node-> getFieldDefinitions()加载了一个节点字段定义:

enter image description here

我相信你可以尝试这样的事情:

  ->setDisplayOptions('form', array(
    'label' => 'above',
    'type' => 'image_image',
    'weight' => -30,
    'settings' => [
      'alt_field' => TRUE,
      'alt_field_required' => TRUE, //optional
      'title_field' => TRUE,
      'title_field_required' => TRUE, //optional
    ],
  ))

0
投票

感谢Dmytro。

我觉得有点愚蠢,但这就是生活。

在setSettings中添加'alt_field_required'=> FALSE和'title_field'=> TRUE就足够了。

但是当标题和alt显示我们下载图像时我认为它不起作用。

失落的一天!

© www.soinside.com 2019 - 2024. All rights reserved.