我正在为"文件上传"创建自定义字段类型包含文件表单的功能。我是新手学习Drupal 8。
我的代码如下:
/**
* Plugin implementation of the 'AkamaiUpload' field type.
*
* @FieldType(
* id = "AkamaiUpload",
* label = @Translation("AkamaiUpload"),
* description = @Translation("Akamai File"),
* category = @Translation("Custom"),
* default_widget = "AkamaiUploadWidget",
* default_formatter = "AkamaiUploadFormatter"
* )
*/
class AkamaiUpload extends FileItem {
/**
* Field type properties definition.
*
*/
public static function propertyDefinitions(StorageDefinition $storage) {
$properties = [];
unset($properties['display']);
unset($properties['description']);
$properties['target_id'] = DataDefinition::create('string')
->setLabel(t('Target'));
return $properties;
}
/**
* Field type schema definition.
*
* Inside this method we defines the database schema used to store data for
* our field type.
*/
public static function schema(StorageDefinition $storage) {
$columns = [];
$columns['target_id'] = [
'description' => 'The ID of the file entity.',
'type' => 'int',
'unsigned' => TRUE,
];
return [
'columns' => $columns,
'indexes' => [
'target_id' => ['target_id'],
],
'foreign keys' => [
'target_id' => [
'table' => 'file_managed',
'columns' => ['target_id' => 'fid'],
],
],
];
}
/**
* Define when the field type is empty.
*
* This method is important and used internally by Drupal. Take a moment
* to define when the field fype must be considered empty.
*/
public function isEmpty() {
$isEmpty =
empty($this->get('target_id')->getValue()) ;
return $isEmpty;
}
} // class
我的Widget类如下:
/**
* Plugin implementation of the 'AkamaiUploadWidget' widget.
*
* @FieldWidget(
* id = "AkamaiUploadWidget",
* label = @Translation("AkamaiUpload"),
* field_types = {
* "AkamaiUpload"
* }
* )
*/
class AkamaiUploadWidget extends FileWidget {
/**
* Define the form for the field type.
*
* Inside this method we can define the form used to edit the field type.
*/
public function formElement(
FieldItemListInterface $items,
$delta,
Array $element,
Array &$form,
FormStateInterface $formState
) {
$class = get_class($this);
$validators = array(
'file_validate_extensions' => array('pdf'),
);
$element['akamai_file_upload'] = [
'#type' => 'managed_file',
'#name' => 'my_file',
'#title' => t('File *'),
'#size' => 20,
'#description' => t('PDF format only'),
'#upload_validators' => $validators,
'#upload_location' => 'public://my_files/',
];
return $element;
}
/*
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!$form_state->isValueEmpty('akamai_file_upload')) {
$file_value = $form_state->getValue('akamai_file_upload');
}
kint($form);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$akamai_file_upload = $form_state->getValue('akamai_file_upload');
if ($form_state->getValue('akamai_file_upload') == NULL) {
$form_state->setErrorByName('akamai_file_upload', $this->t('File.'));
}
else{
// add File upload logic here
}
}
} // class
我遇到的问题是 1. $ form_state-> getValue(' akamai_file_upload')在submitForm和validateForm方法中始终为NULL 2.在我构建的表单中,我有一个名为" Save and Publish"的按钮。 。如何将submitForm方法链接到"保存并发布"按钮?
任何指针都将受到高度赞赏