php - 德鲁巴 8 : How do I customize a form widget to show an entity field value instead of the entity title?

标签 php drupal module widget drupal-8

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

这是我的表单在没有自定义工作的情况下的样子,使用 Drupal 8 的内置“复选框/单选按钮”小部件:

Drupal 8's built-in "Check boxes/radio buttons" widget example

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

Photoshop mockup of how I want my custom widget to appear

到目前为止,我已经能够创建一个起始模块来扩展“复选框/单选按钮”小部件,引用 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!

}

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

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

最佳答案

您不需要创建自定义小部件。

编辑您的字段并将“引用方法”从“默认”设置为“ View :按实体引用 View 过滤”。然后它会告诉您以下内容(如果尚未定义实体引用 View ):

No eligible views were found. Create a view with an Entity Reference display, or add such a display to an existing view.

然后您继续创建该实体引用 View (/admin/structure/views),返回您的字段并再次选择它,现在您应该能够选择 View 并瞧瞧。

当您创建实体引用 View 时,您可以定义要显示的实体字段(而不是实体标题或附加到它 - 这正是您想要的)。所以实际上你不需要为该功能编写任何代码,这都是配置。

编辑:

正如提问者所指出的,这不是开箱即用的。我找到了一个启用所需功能的模块(实体引用 View 的表单小部件):

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

这个模块似乎只为实体引用 View 启用选择和复选框小部件。

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

关于php - 德鲁巴 8 : How do I customize a form widget to show an entity field value instead of the entity title?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37335237/

相关文章:

下拉发布请求时出现 PHP "Undefined index:"错误

php - 如何在没有 Drupal 容器的情况下安装 Drush

Drupal 7 - 使用 managed_file 类型上传文件后自动提交表单

python - Python目录名可以是关键字吗?例如。 'import' ?

Perl:当两者导入同一模块时模块发生冲突

php - 从 MIN(CAST(.. AS DATETIME)) 输出获取值到 php 错误

php - 无法安装具有要求的想象类(centos6.5/php5.4 上的供应商/autoload.php 错误

php - 如何删除重复的内容?

mysql - 找不到匹配的 SQL 类。 Drush 找不到您的数据库连接。错误

android - 从父 android 模块访问 Dagger 子组件依赖项