私のDrupal 8アプリケーション用のカスタムフィールドを作成しました。フィールド名はオフィスアドレスと呼ばれ、完全な住所(住所1、住所2、市、州コード、郵便番号、国コード)を保持する必要があります。また、管理者がどの国を選択するかを決めるための設定フォームもあります。Drupal 8がカスタムフィールドのDefaultWidgetを見つけることができません
また、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 /プラグイン/フィールド/ 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 /プラグイン/フィールド/ 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
}
拡張子が問題とtなしでインストールされます彼はconfigフォームを使用することができ、値は正しく保存され、取得されます。既存のコンテンツタイプにOfficeAddressフィールドを追加しようとすると、この問題が発生します。
次のエラーが発行されます。
フィールドオフィスアドレスの作成に問題が発生しました:「OfficeAddressDefaultWidget」プラグインは存在しませんが。
私はどこでもスペルをチェックしていると思いますし、ファイルの配置も正しいはずです。私は同じファイル/ディレクトリ構造(名前はオフィスアドレスの代わりに場所であり、設定フォームも関与するサービスプロバイダもありませんでした)で動作する、これまでと同じモジュールを作成しました。私はこの時点で壁に当たっています...