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

查看:165
本文介绍了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):

到目前为止,我已经能够创建一个扩展复选框/单选按钮"小部件的启动模块,并引用

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 \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!

}

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

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天全站免登陆