0

我从 Drupal 8 开始,直到现在我对所有新功能印象深刻。但是,我一直在尝试编写自己的实体,但遇到了麻烦:

这是实体定义:

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Drupal\entitytest\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Defines the Candidate Entity
 * 
 * @ingroup entitytest
 * 
 * @ContentEntityType(
 *    id="entitytest_AuditionCandidate",
 *    label=@Translation("Candidate"),
 *    base_table="candidate",
 *    
 *    handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\entitytest\Entity\Controller\CandidateListBuilder",
 *     "form" = {
 *       "add" = "Drupal\Core\Entity\ContentEntityForm",
 *       "edit" = "Drupal\Core\Entity\ContentEntityForm",      
 *       "delete" = "Drupal\EntityTest\Form\CandidateDeleteForm",
 *        },   
 *    },
 *    admin_permission="administer candidates",
 *    entity_keys={
 *      "id"="id",
 *      "label"="lastname",   
 *   },
 *    links = {
 *     "canonical" = "/AuditionCandidate/view/{entitytest_AuditionCandidate}",
 *     "edit-form" = "/AuditionCandidate/edit/{entitytest_AuditionCandidate}",
 *   },
 * )
 */
class Candidate extends ContentEntityBase {
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
     $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the Contact entity.'))
      ->setReadOnly(TRUE);

     $fields['lastname'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Last Name'))
      ->setDescription(t('The name of the Contact entity.'))
      ->setSettings(array(
        'default_value' => '',
        'max_length' => 255,
        'text_processing' => 0,
      ))
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

     $fields['firstname'] = BaseFieldDefinition::create('string')
      ->setLabel(t('First Name'))
      ->setDescription(t('The name of the Contact entity.'))
      ->setSettings(array(
        'default_value' => '',
        'max_length' => 255,
        'text_processing' => 0,
      ))
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

     return $fields;
  }
}

所以我试图从这个实体编辑 Deleteform。我在 /modules/custom/EntityTest/src/Form/CandidateFormDelete.php 下创建了一个文件

该文件中的代码如下:

<?php
namespace Drupal\EntityTest\Form;

use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

class CandidateDeleteForm extends ContentEntityConfirmFormBase {
  public function getQuestion() {
    return $this->t('Are you sure?');
  }

  public function getCancelUrl() {
    return new Url('entity.entitytest_AuditionCandidate.collection');
  }

  public function getConfirmText() {
    return $this->t('Delete');

  }
}

我还为删除表单添加了一条路线:

entity.entitytest_AuditionCandidate.delete_form:
    path: 'AuditionCandidate/delete/{entitytest_AuditionCandidate}'
    defaults:
        _entity_form: entitytest_AuditionCandidate.delete
        _title: 'Delete Candidate'
    requirements:
        _permission: 'administer candidates'

但是当我尝试打开 /AuditionCandidate/delete/1 时,我收到以下错误消息:

Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException:“entitytest_AuditionCandidate”实体类型未指定“删除”表单类。在 Drupal\Core\Entity\EntityManager->getFormObject() (core/lib/Drupal/Core/Entity/EntityManager.php 的第 309 行)。

这似乎没有意义,因为我已经为 deleteform 定义了一个类。

谁能看到我错过了什么?这可能只是一个错字,但我已经盯着它看了很长一段时间了,我就是想不通。

马特。

4

1 回答 1

0

Drupal 8 为基于​​包的 PHP 命名空间自动加载实现了 PSR-4 标准。在这种情况下,您的类文件的名称与实际使用的类的名称不对应。文件名也应该是“CandidateDeleteForm.php”而不是“CandidateFormDelete”

这就是你得到那个例外的原因。有关该主题的更多信息,请阅读: https ://www.drupal.org/node/2156625

于 2015-09-10T14:13:24.883 回答