Drupal 8:如何自定义表单小部件以显示实体字段值而不是实体标题? [英] Drupal 8: How do I customize a form widget to show an entity field value instead of the entity title?

查看:26
本文介绍了Drupal 8:如何自定义表单小部件以显示实体字段值而不是实体标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过开发自定义表单小部件模块来了解 Drupal 8 的工作原理.我的目标是在单选按钮列表(核心可用)中显示引用节点的图像字段值,而不是其节点标题.这将允许网站管理员在为节点选择背景图像时选择图片而不是文本.

I'm taking my first steps in understanding how Drupal 8 works under the hood by developing a custom form widget module. My goal is to show a referenced node's image field value, instead of its node title in a radio button list (available in core). This will allow website admins to select a picture instead of text when choosing a background image for a node.

使用 Drupal 8 的内置复选框/单选按钮"小部件,以下是我的表单在没有自定义工作的情况下的外观:

Here's how my form looks without a custom work, using Drupal 8's built-in "Check boxes/radio buttons" widget:

这是我希望我的自定义小部件如何显示(至少在开始时)的 Photoshop 模型:

Here's a Photoshop mockup of how I want my custom widget to appear (at least to start):

到目前为止,我已经能够创建一个扩展复选框/单选按钮"小部件的起始模块,引用 开发者示例 模块和遍历核心.这至少帮助我更好地理解了 Drupal 8 的模块结构.

So far I've been able to create a starting module that extends the "Check boxes/radio buttons" widget, referencing the Examples for Developers module and traversing core. This has at helped me understand Drupal 8's module structure a little better at the least.

模块结构:

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:

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:

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:

BackImageWidget.php:

<?php

/**
 * @file
 * Contains Drupalack_image_widgetPluginFieldFieldWidget.
 */

namespace Drupalack_image_widgetPluginFieldFieldWidget;

use DrupalCoreFieldFieldItemListInterface;
use DrupalCoreFieldWidgetBase;
use DrupalCoreFieldPluginFieldFieldWidgetOptionsButtonsWidget;
use DrupalCoreFormFormStateInterface;

/**
 * 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!

}

这样,我就可以安装模块,选择新的小部件,并启动核心提供的所有预期功能.

With that, I'm able to install the module, select the new widget, and have all expected functionality that core offers to start.

从这里开始,我无法从父类中识别出最好的部分进行更改,以便我可以用其他实体值替换标题.最有用的功能似乎受到保护.结果选项返回受保护的标题(没有其他可用信息,例如要使用的节点 ID).我是否需要继承曾祖父母并重新开始?我猜我需要进一步探索依赖注入?关于如何进行一般或详细的任何想法?只要能帮助我克服这个障碍,我就可以灵活地回答.

From here, I'm having troubles identifying the best pieces from parent classes to alter so that I can replace titles with other entity values. The most helpful functions seem to be protected. The resulting options return protected titles (without other information available such as node ids to play with). Do I need to inherit a great grandparent and start fresh? I'm guessing I'll need to further explore dependency injections? Any thoughts on how to proceed in general or in detail? I'm flexible with answers as long as it helps me overcome this stuck point.

推荐答案

您无需创建自定义小部件.

You don't need to create a custom widget.

编辑您的字段并将引用方法"从默认"设置为视图:按实体引用视图过滤".然后它会告诉你以下内容(如果还没有定义实体引用视图):

Edit your field and set the "reference method" from "default" to "Views: filter by an entity reference view". It will then tell you the following (if there are no entity reference views defined yet):

未找到符合条件的视图.使用实体引用创建视图显示,或将此类显示添加到现有视图中.

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),返回您的字段并再次选择它,现在您应该能够选择视图并瞧.

So then you go ahead and create that entity reference view (/admin/structure/views), go back to your field and select it again, now you should be able to pick the view and voilá.

当您创建实体参考视图时,您可以定义要显示的实体字段(而不是实体标题或附加于它 - 这正是您想要的).所以实际上您不需要为该功能编写任何代码,一切都是配置.

When you create the entity reference view, you can define which entity fields to display (instead of the entity title or additionally to it - which is exactly what you want). So actually you don't need to write any code for that functionality, it's all configuration.

正如提问者所理解的那样,这不是开箱即用的.我找到了一个启用所需功能的模块(实体参考视图的表单小部件):

As figured out by asker, this does not work out of the box. I found a module which enables the desired functionality (the form widget for the entity reference view):

https://www.drupal.org/project/entity_reference_views_select

这个模块似乎只为实体参考视图启用选择和复选框小部件.

This module seems only to enable the select and checkbox widget for the entity reference view.

如果需要更复杂的配置,实体浏览器:https://www.drupal.org/project/entity_browser看起来也正在大力发展.(均未经本人测试)

If a more sophisticated configuration is desired, the entity browser: https://www.drupal.org/project/entity_browser is also under heavy development, as it seems. (both untested by myself)

这篇关于Drupal 8:如何自定义表单小部件以显示实体字段值而不是实体标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆