Drupalモジュールでのフィールドタイプの作成
フィールドタイプは、フィールドのプロパティと動作を定義します。フィールドタイプはプラグインとして定義されるため、新しいフィールドタイプの作成を始める前にプラグインAPIを確認することをお勧めします。
Drupal 8でフィールドタイプを作成するには、FieldTypeアノテーションを持つクラスが必要です。
配置場所フィールドタイプクラスはMODULE_NAME/src/Plugin/Field/FieldTypeに配置する必要があります
/modules/foo/src/Plugin/Field/FieldType/BazItem.php
名前空間このクラスの名前空間はDrupal\MODULE_NAME\Plugin\Field\FieldTypeである必要があります
<?php
namespace Drupal\MODULE_NAME\Plugin\Field\FieldType;
ドキュメントコメント内のクラス上のアノテーションには、一意のID、ラベル、およびデフォルトのフォーマッターを含める必要があります。デフォルトのフォーマッターは、フィールドフォーマッタークラスのアノテーションで使用されるIDになります。
/**
* Provides a field type of baz.
*
* @FieldType(
* id = "baz",
* label = @Translation("Baz field"),
* default_formatter = "baz_formatter",
* default_widget = "baz_widget",
* )
*/
クラスはFieldItemInterfaceインターフェースを実装する必要があります。また、インターフェースの一般的な実装のためにFieldItemBaseクラスを拡張する必要があります。
class BazItem extends FieldItemBase {
}
FieldItemInterface::schema()をオーバーライドして、フィールドの値をどのように保存するかをシステムに通知する必要があります
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
// columns contains the values that the field will store
'columns' => array(
// List the values that the field will save. This
// field will only save a single value, 'value'
'value' => array(
'type' => 'text',
'size' => 'tiny',
'not null' => FALSE,
),
),
);
}
このメソッドは、スキーマAPIの列仕様の配列を返します。
FieldItemInterface::propertyDefinitions()メソッドは、フィールドのプロパティに関する詳細情報をシステムに通知します
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = [];
$properties['value'] = DataDefinition::create('string');
return $properties;
}
Map::isEmpty(FieldItemBaseの祖先)をオーバーライドして、フィールドが空のときにそれをどのように判断するかをシステムに通知する必要があります。
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
フィールド設定
フィールド設定により、ユーザーはニーズに合わせてフィールドを構成できます。フィールドにフィールド設定がある場合は、3つの手順を実行する必要があります:
- FieldItemBase::defaultFieldSettings()をオーバーライドしてデフォルト値を設定します
- 作成した設定の設定スキーマを作成します
- ユーザーが設定を変更できるようにフォームを作成します
ステップ1:FieldItemBase::defaultFieldSettings()をオーバーライドします
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
return [
// Declare a single setting, 'size', with a default
// value of 'large'
'size' => 'large',
] + parent::defaultFieldSettings();
}
ステップ2:作成した設定の設定スキーマを作成します
設定スキーマは次のファイルにあります:
[MODULE ROOT]/config/schema/[MODULE_NAME].schema.yml
このファイルでは、defaultFieldSettings()で作成した設定データのタイプを記述します:
ステップ1は、文字列値を格納する「size」という名前の設定を作成しました。これのスキーマは次のようになります:
field.field_settings.[FIELD ID]:
type: mapping
label: 'FIELDNAME settings'
mapping:
size:
type: string
label: 'Size'
ステップ3:ユーザーが設定を変更できるようにフォームを作成します
ユーザーが設定値を変更できるようにするフォームは、FieldItemBase::fieldSettingsForm()をオーバーライドして作成されます
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
// The key of the element should be the setting name
$element['size'] = [
'#title' => $this->t('Size'),
'#type' => 'select',
'#options' => [
'small' => $this->t('Small'),
'medium' => $this->t('Medium'),
'large' => $this->t('Large'),
],
'#default_value' => $this->getSetting('size'),
];
return $element;
}
実際の例
examplesプロジェクトのfield_exampleモジュールのRgbItem:
namespace Drupal\field_example\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'field_example_rgb' field type.
*
* @FieldType(
* id = "field_example_rgb",
* label = @Translation("Example Color RGB"),
* module = "field_example",
* description = @Translation("Demonstrates a field composed of an RGB color."),
* default_widget = "field_example_text",
* default_formatter = "field_example_simple_text"
* )
*/
class RgbItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'value' => array(
'type' => 'text',
'size' => 'tiny',
'not null' => FALSE,
),
),
);
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Hex value'));
return $properties;
}
}