Drupal 8:如何自定义表单小部件以显示实体字段值而不是实体标题?
Drupal 8: How do I customize a form widget to show an entity field value instead of the entity title?
我正在通过开发自定义表单小部件模块来了解 Drupal 8 的工作原理。我的目标是在单选按钮列表(在核心中可用)中显示引用节点的图像字段值,而不是其节点标题。这将允许网站管理员在为节点选择背景图像时 select 图片而不是文本。
这是我的表单在没有自定义工作的情况下的样子,使用 Drupal 8 的 built-in "Check boxes/radio buttons" 小部件:
这是我希望我的自定义小部件如何显示(至少开始)的 Photoshop 模型:
到目前为止,我已经能够创建一个起始模块来扩展 "Check boxes/radio buttons" 小部件,引用 Examples for Developers 模块和遍历核心。这至少帮助我更好地理解了 Drupal 8 的模块结构。
模块结构:
modules
custom
back_image_widget
back_image_widget.info.yml
back_image_widget.module
src
Plugin
Field
Field Widget
BackImageWidget.php
back_image_widget.info.yml:
name: Background Image Entity Widget
type: module
description: Used to list Background Image entities as images instead of text labels in the Text Message content type form.
package: Custom
core: 8.x
back_image_widget.module:
<?php
/**
* @file
* Used to list Background Image entities as images instead of text labels in the Text Message content type form.
*/
BackImageWidget.php:
<?php
/**
* @file
* Contains \Drupal\back_image_widget\Plugin\Field\FieldWidget.
*/
namespace Drupal\back_image_widget\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsButtonsWidget;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'field_back_image' widget.
*
* @FieldWidget(
* id = "field_back_image",
* module = "back_image_widget",
* label = @Translation("Background Image Entity"),
* field_types = {
* "entity_reference"
* },
* multiple_values = FALSE
* )
*/
class BackImageWidget extends OptionsButtonsWidget {
//Here we go!
}
这样,我就可以安装模块,select 新的小部件,并拥有核心提供的所有预期功能。
从这里开始,我很难从 parent 类 中找出最好的作品进行更改,以便我可以用其他实体值替换标题。最有用的功能似乎受到保护。生成的选项 return 受保护的标题(没有其他可用信息,例如可以使用的节点 ID)。我需要继承曾祖父parent 重新开始吗?我猜我需要进一步探索依赖注入?关于如何进行一般或详细的任何想法?只要能帮助我克服这个瓶颈,我都可以灵活回答。
您不需要创建自定义小部件。
编辑您的字段并将“参考方法”从“默认”设置为“视图:过滤依据实体参考视图”。然后它将告诉您以下内容(如果尚未定义实体引用视图):
No eligible views were found. Create a view with an Entity Reference
display, or add such a display to an existing view.
然后您继续创建该实体参考视图 (/admin/structure/views),返回您的字段并再次 select,现在您应该可以选择视图了。
创建实体引用视图时,您可以定义要显示的实体字段(而不是实体标题或附加到实体标题 - 这正是您想要的)。所以实际上你不需要为该功能编写任何代码,这都是配置。
编辑:
正如提问者所说,这不是开箱即用的。我找到了一个启用所需功能的模块(实体参考视图的表单小部件):
https://www.drupal.org/project/entity_reference_views_select
该模块似乎只为实体引用视图启用 select 和复选框小部件。
如果需要更复杂的配置,实体浏览器:
https://www.drupal.org/project/entity_browser
看起来也正在大力发展。 (均未经本人测试)
我正在通过开发自定义表单小部件模块来了解 Drupal 8 的工作原理。我的目标是在单选按钮列表(在核心中可用)中显示引用节点的图像字段值,而不是其节点标题。这将允许网站管理员在为节点选择背景图像时 select 图片而不是文本。
这是我的表单在没有自定义工作的情况下的样子,使用 Drupal 8 的 built-in "Check boxes/radio buttons" 小部件:
这是我希望我的自定义小部件如何显示(至少开始)的 Photoshop 模型:
到目前为止,我已经能够创建一个起始模块来扩展 "Check boxes/radio buttons" 小部件,引用 Examples for Developers 模块和遍历核心。这至少帮助我更好地理解了 Drupal 8 的模块结构。
模块结构:
modules
custom
back_image_widget
back_image_widget.info.yml
back_image_widget.module
src
Plugin
Field
Field Widget
BackImageWidget.php
back_image_widget.info.yml:
name: Background Image Entity Widget
type: module
description: Used to list Background Image entities as images instead of text labels in the Text Message content type form.
package: Custom
core: 8.x
back_image_widget.module:
<?php
/**
* @file
* Used to list Background Image entities as images instead of text labels in the Text Message content type form.
*/
BackImageWidget.php:
<?php
/**
* @file
* Contains \Drupal\back_image_widget\Plugin\Field\FieldWidget.
*/
namespace Drupal\back_image_widget\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsButtonsWidget;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'field_back_image' widget.
*
* @FieldWidget(
* id = "field_back_image",
* module = "back_image_widget",
* label = @Translation("Background Image Entity"),
* field_types = {
* "entity_reference"
* },
* multiple_values = FALSE
* )
*/
class BackImageWidget extends OptionsButtonsWidget {
//Here we go!
}
这样,我就可以安装模块,select 新的小部件,并拥有核心提供的所有预期功能。
从这里开始,我很难从 parent 类 中找出最好的作品进行更改,以便我可以用其他实体值替换标题。最有用的功能似乎受到保护。生成的选项 return 受保护的标题(没有其他可用信息,例如可以使用的节点 ID)。我需要继承曾祖父parent 重新开始吗?我猜我需要进一步探索依赖注入?关于如何进行一般或详细的任何想法?只要能帮助我克服这个瓶颈,我都可以灵活回答。
您不需要创建自定义小部件。
编辑您的字段并将“参考方法”从“默认”设置为“视图:过滤依据实体参考视图”。然后它将告诉您以下内容(如果尚未定义实体引用视图):
No eligible views were found. Create a view with an Entity Reference display, or add such a display to an existing view.
然后您继续创建该实体参考视图 (/admin/structure/views),返回您的字段并再次 select,现在您应该可以选择视图了。
创建实体引用视图时,您可以定义要显示的实体字段(而不是实体标题或附加到实体标题 - 这正是您想要的)。所以实际上你不需要为该功能编写任何代码,这都是配置。
编辑:
正如提问者所说,这不是开箱即用的。我找到了一个启用所需功能的模块(实体参考视图的表单小部件):
https://www.drupal.org/project/entity_reference_views_select
该模块似乎只为实体引用视图启用 select 和复选框小部件。
如果需要更复杂的配置,实体浏览器: https://www.drupal.org/project/entity_browser 看起来也正在大力发展。 (均未经本人测试)