私は、引用、名前、会社、およびオプションのヘッドショットを含む新しいフィールドタイプを作成しています。Drupal 8フィールドプラグインフィールドタイプmanaged_file
私のコードは
modules/mymodule/src/FieldType/Testimonial.php
<?php
namespace Drupal\dods\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface as StorageDefinition;
/**
* Plugin implementation of the 'Testimonial' field type.
*
* @FieldType(
* id = "Testimonial",
* label = @Translation("Testimonial"),
* description = @Translation("Stores Testimonial Information."),
* category = @Translation("Custom"),
* default_widget = "TestimonialDefaultWidget",
* default_formatter = "TestimonialDefaultFormatter"
*)
*
* Class Testimonial
* @package Drupal\dods\Plugin\Field\FieldType
*/
class Testimonial extends FieldItemBase
{
/**
* Field type properties definition.
*
* Inside this method we defines all the fields (properties) that our
* custom field type will have.
*
* @param StorageDefinition $storage
* @return array
*/
public static function propertyDefinitions(StorageDefinition $storage)
{
$properties['testimonial_quote'] = DataDefinition::create('string')->setLabel(t('Quote'));
$properties['testimonial_name'] = DataDefinition::create('string')->setLabel(t('Name'));
$properties['testimonial_company'] = DataDefinition::create('string')->setLabel(t('Company'));
$properties['testimonial_headshot'] = DataDefinition::create('integer')->setLabel(t('Headshot'));
return $properties;
}
/**
* Field type schema definition.
*
* Inside this method we defines the database schema used to store data for
* our field type.
* @param StorageDefinition $storage
* @return array
*/
public static function schema(StorageDefinition $storage)
{
$schema['columns']['testimonial_quote'] = ['type' => 'text'];
$schema['columns']['testimonial_name'] = ['type' => 'char', 'length' => 100];
$schema['columns']['testimonial_company'] = ['type' => 'char', 'length' => 100];
$schema['columns']['testimonial_headshot'] = ['type' => 'int'];
return $schema;
}
/**
* Define when the field type is empty.
*
* This method is important and used internally by Drupal. Take a moment
* to define when the field type must be considered empty.
*
* @return bool
*/
public function isEmpty()
{
return empty($this->get('testimonial_quote')->getValue()) && empty($this->get('testimonial_name')->getValue()) && empty($this->get('testimonial_company')->getValue());
}
}
を下回っていると私のウィジェットは、この
modules/mymodule/src/Plugin/Field/FieldWidget/TestimonialDefaultWidget.php
<?php
namespace Drupal\dods\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'TestimonialDefaultWidget' widget.
*
* @FieldWidget(
* id = "TestimonialDefaultWidget",
* label = @Translation("Testimonial Information"),
* field_types = {
* "Testimonial"
* }
*)
*
* Class TestimonialDefaultWidget
* @package Drupal\dods\Plugin\Field\FieldWidget
*/
class TestimonialDefaultWidget extends WidgetBase
{
/**
* Define the form for the field type.
*
* Inside this method we can define the form used to edit the field type.
*
* @param FieldItemListInterface $items
* @param int $delta
* @param array $element
* @param array $form
* @param FormStateInterface $formState
* @return array
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $formState)
{
$element['testimonial_quote'] = [
'#type' => 'textarea',
'#title' => t('Quote'),
'#default_value' => isset($items[$delta]->testimonial_quote) ? $items[$delta]->testimonial_quote : null,
'#empty_value' => '',
'#placeholder' => t('Quote'),
];
$element['testimonial_name'] = [
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($items[$delta]->testimonial_name) ? $items[$delta]->testimonial_name : null,
'#empty_value' => '',
'#placeholder' => t('Name'),
];
$element['testimonial_company'] = [
'#type' => 'textfield',
'#title' => t('Company'),
'#default_value' => isset($items[$delta]->testimonial_company) ? $items[$delta]->testimonial_company : null,
'#empty_value' => '',
'#placeholder' => t('Company'),
];
$element['testimonial_headshot'] = array(
'#type' => 'managed_file',
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(25600000),
),
'#required' => false
);
return $element;
}
}
それはすべての罰金looksdが、私が何かを保存しようと、これまで私は、このエラー
にこの値を取得するときのように見えます正しいプリミティブ型でなければなりません。
すべてのアイデアは、私はレンガの壁に当たっていいただければ幸いです:(
UPDATE
は少しハック感じが、これはあまりにも、私は、次の
をしなければならなかった仕事を取得しますフォームに追加します。
$element['testimonial_headshot_revision'] = [
'#type' => 'hidden',
'#default_value' => isset($items[$delta]->testimonial_headshot) ? $items[$delta]->testimonial_headshot : null
];
新しい機能で追加するあなたはint
としてtestimonial_headshot
を定義しますが、ファイルのアップロードウィジェットを提供しているためであるので、
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state)
{
foreach ($values as &$value) {
if (count($value['testimonial_headshot'])) {
foreach ($value['testimonial_headshot'] as $fid) {
$value['testimonial_headshot'] = $fid;
}
} else {
$value['testimonial_headshot'] = $value['testimonial_headshot_revision'] !== '' ? $value['testimonial_headshot_revision'] : '0';
}
}
return $values;
}
同じ問題が発生しました。 – JayKandari