我开始使用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-&gt; getFormObject()中(core / lib / Drupal / Core / Entity / EntityManager.php的第309行)。
它似乎没有意义,因为我已经为删除形式定义了一个类。
任何能看到我失踪的人?这可能只是一个错字,但我已经盯着它看了很长一段时间,我只是想不出来。
太