【问题标题】:Drupal 8 is unable to find DefaultWidget for custom fieldDrupal 8 无法找到自定义字段的 DefaultWidget
【发布时间】:2016-11-13 12:50:50
【问题描述】:

我为我的 Drupal 8 应用创建了一个自定义字段。字段名称称为办公地址,应包含完整的地址(地址行 1、地址行 2、城市、州代码、邮政编码、国家代码)。它还有一个配置表单,允许管理员决定哪些国家可供选择。

此外,还有一个名为 CountryStateService 的服务提供者,它提供国家及其各自州的列表。

目录结构如下:

/modules
  /office_address
    /config
      /install
        office_address.settings.yml
      /schema
        office_address.settings.yml
    /src
      /Form
        ConfigForm.php
      /Plugin
        /Field
          /FieldFormatter
            OfficeAddressDefaultFormatter.php
          /FieldType
            OfficeAddress.php
          /FieldWidget
            OfficeAddressDefaultWidget.php
      CountryStateService.php
  office_address.info.yml
  office_address.links.menu.yml
  office_address.permissions.yml
  office_address.routing.yml
  office_address.services.yml

文件 src/Plugin/Field/FieldWidget/OfficeAddressDefaultWidget.php 看起来像:

<?php
namespace Drupal\office_address\Plugin\Field\FieldWidget;

use Drupal;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'OfficeAddressDefaultWidget' widget.
 *
 * @FieldWidget(
 *   id = "OfficeAddressDefaultWidget",
 *   label = @Translation("Office Address"),
 *   field_types = {
 *     "OfficeAddress"
 *   }
 * )
 */

class OfficeAddressDefaultWidget extends WidgetBase {
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    // my custom code here
  }
}

文件 src/Plugin/Field/FieldType/OfficeAddress.php 看起来像:

<?php
/**
 * @file
 * Contains \Drupal\office_address\Plugin\Field\FieldType\OfficeAddress
 */
namespace Drupal\office_address\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

/**
 * Plugin implementation of the 'Office Address' field type.
 *
 * @FieldType(
 *   id = "OfficeAddress",
 *   label = @Translation("Office Address"),
 *   description = @Translation("Stores an address."),
 *   category = @Translation("Custom"),
 *   default_widget = "OfficeAddressDefaultWidget",
 *   default_formatter = "OfficeAddressDefaultFormatter"
 * )
 */

class OfficeAddress extends FieldItemBase {
  // some code here
}

扩展安装没有问题,配置表单可用,值得到正确保存和检索。尝试将 OfficeAddress 字段添加到现有内容类型时会出现问题。

发出以下错误:

创建外业办公室地址时出现问题:“OfficeAddressDefaultWidget”插件不存在。

我相信我已经检查了所有地方的拼写,而且文件放置应该是正确的。我过去创建了一个类似的模块,它使用相同的文件/目录结构(名称是位置而不是办公室地址,并且没有配置表单,也没有涉及任何服务提供商)。在这一点上我已经碰壁了......

【问题讨论】:

    标签: php drupal-8


    【解决方案1】:

    这实际上是我在目录结构中的一个错字。所以这花了我一整天的时间。

    对于那些似乎正在与 Drupal 8 中明显的无意义错误作斗争的人的重要说明:四重检查您的拼写,因为目录/文件和命名空间都受到关注!

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2018-11-22
      相关资源
      最近更新 更多