c1
时。 complete complete conders cond.
my
c2
demo2c_entity_base_field_info()
文件文件
demo2c.info.yml
name: Demo With Two Column Field Type
type: module
description: Fails with hook_entity_base_field_info().
core_version_requirement: ^11.0
package: Demo
src/Plugin/Field/FieldType/Demo2CType.php
文件。
<?php
namespace Drupal\demo2c\Plugin\Field\FieldType ;
use Drupal\Core\Field\FieldItemBase ;
use Drupal\Core\Field\FieldStorageDefinitionInterface ;
use Drupal\Core\TypedData\DataDefinition ;
/**
* A field type with two integer columns.
*
* @FieldType(
* id = "demo2c_field_type",
* label = @Translation( "Demo Two Column Field Type" ),
* module = "demo2c",
* description = @Translation( "Fails when added with hook_entity_base_field_info()." ),
* default_widget = "demo2c_field_widget",
* default_formatter = "demo2c_field_formatter"
* )
*/
class Demo2CType extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema( FieldStorageDefinitionInterface $field_definition ) {
return [ 'columns' => [ 'c1' => [ 'type' => 'int',
'size' => 'normal',
'not null' => FALSE ],
'c2' => [ 'type' => 'int',
'size' => 'normal',
'not null' => FALSE ] ] ] ;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
return $this->get( 'c1' )->getValue() === NULL
&& $this->get( 'c2' )->getValue() === NULL ;
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions( FieldStorageDefinitionInterface $field_definition ) {
$prop[ 'c1' ] = DataDefinition::create( 'integer' )->setLabel( t( 'Column One' ) ) ;
$prop[ 'c2' ] = DataDefinition::create( 'integer' )->setLabel( t( 'Column Two' ) ) ;
return $prop ;
}
}
在这一点上,我能够手动将字段附加到用户,并使用Drupal的常规配置/编辑接口保存设置。,无论如何,当我在
src/Plugin/Field/FieldWidget/Demo2CWidget.php
中添加以下内容时,我会在“保存”上获得错误。
<?php
namespace Drupal\demo2c\Plugin\Field\FieldWidget ;
use Drupal\Core\Field\FieldItemListInterface ;
use Drupal\Core\Field\WidgetBase ;
use Drupal\Core\Form\FormStateInterface ;
/**
* A field widget with two integer columns.
*
* @FieldWidget(
* id = "demo2c_field_widget",
* label = @Translation( "Demo Two Column Field Widget" ),
* module = "demo2c",
* field_types = { "demo2c_field_type" }
* )
*/
class Demo2CWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement( FieldItemListInterface $items,
$delta,
array $element,
array &$form,
FormStateInterface $form_state ) {
$element += [ '#type' => 'fieldset' ] ;
$element[ 'c1' ] = [ '#type' => 'number',
'#default_value' => $items->get( $delta )->get( 'c1' )->getValue() ?? 0,
'#title' => $this->t( "Column One" ) ] ;
$element[ 'c2' ] = [ '#type' => 'number',
'#default_value' => $items->get( $delta )->get( 'c2' )->getValue() ?? 0,
'#title' => $this->t( "Column Two" ) ] ;
return $element ;
}
}
在
src/Plugin/Field/FieldFormatter/Demo2CFormatter.php
?中,我错过了什么
我至少部分地弄清楚了。 我需要实施
<?php
namespace Drupal\demo2c\Plugin\Field\FieldFormatter ;
use Drupal\Core\Field\FormatterBase ;
use Drupal\Core\Field\FieldItemListInterface ;
/**
* A field formatter with two integer columns.
*
* @FieldFormatter(
* id = "demo2c_field_formatter",
* module = "demo2c",
* label = @Translation( "Demo Two Column Field Formatter" ),
* field_types = { "demo2c_field_type" }
* )
*/
class Demo2CFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements( FieldItemListInterface $items, $langcade ) {
$elements = [] ;
foreach( $items as $delta => $item ) {
$c1 = $item->get( 'c1' )->getValue() ;
$c2 = $item->get( 'c2' )->getValue() ;
$elements[ $delta ] = [ '#markup' => "Column one: $c1, column two: $c2." ] ;
}
return $elements ;
}
}
中demo2c.module
班。 大概这告诉Drupal类型中有多个列。然后在
<?php
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
function demo2c_entity_base_field_info( EntityTypeInterface $entity_type ) {
if ( $entity_type->id() === 'user' ) {
$fields[ 'my_2c' ] = BaseFieldDefinition::create( 'demo2c_field_type' )
->setLabel( t( 'My Demo Two Column Field' ) )
->setDescription( t( "Doesn't work." ) )
->addConstraint( 'UniqueField' )
->setDisplayOptions( 'form', [ 'weight' => -1 ] ) ;
return $fields ;
}
}
文件中删除
hook_entity_base_field_info()
约束或表单验证失败。
/**
* {@inheritdoc}
*/
public static function mainPropertyName() { return NULL ; }