Drupal 8文件值在自定义文件字段类型中为空

Drupal 8文件值在自定义文件字段类型中为空,drupal,drupal-8,Drupal,Drupal 8,我正在为包含文件表单的“文件上载”功能创建自定义字段类型。我是学习Drupal 8的新手 我的代码如下: /** * Plugin implementation of the 'AkamaiUpload' field type. * * @FieldType( * id = "AkamaiUpload", * label = @Translation("AkamaiUpload"), * description = @Translation("Akamai File"),

我正在为包含文件表单的“文件上载”功能创建自定义字段类型。我是学习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
/**
 * 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
我的Widget类如下所示:

/**
 * 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
/**
 * 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. $表单状态->getValue(“akamai文件上传”)在submitForm和validateForm方法中始终为空 2.在我构建的表单中,我有一个名为“保存和发布”的按钮。如何将submitForm方法链接到“保存和发布”按钮


任何提示都将非常感谢

您解决过这个问题吗?