我一直在尝试以编程方式创建自定义实体类型,但我不断收到一条错误消息,似乎无法弄清。这是我的代码:
/src/Entity/Car.php
namespace Drupal\car\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
/**
* Defines the Car entity.
*
* @ContentEntityType(
* id = "car",
* label = @Translation("Car"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\car\CarListBuilder",
*
* "form" = {
* "default" = "Drupal\car\Form\CarForm",
* "add" = "Drupal\car\Form\CarForm",
* "edit" = "Drupal\car\Form\CarForm",
* "delete" = "Drupal\car\Form\ContentEntityDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider"
* },
* },
* base_table = "car",
* admin_permission = "administer site entities",
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* },
* links = {
* "canonical" = "/admin/structure/car/{car}",
* "add-form" = "/admin/structure/car/add",
* "edit-form" = "/admin/structure/car/{car}/edit",
* "delete-form" = "/admin/structure/car/{car}/delete",
* "collection" = "/admin/structure/car",
* },
* )
*/
class Car extends ContentEntityBase implements CarInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the car entity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
}
/src/Entity/CarInterface.php
namespace Drupal\car\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
/**
* Provides an interface for defining Car entities.
*/
interface CarInterface extends ContentEntityInterface, EntityChangedInterface {
/**
* Gets the Car name.
*
* @return string
* Name of the Car.
*/
public function getName();
/**
* Sets the Car name.
*
* @param string $name
* The Car name.
*
* @return \Drupal\car\Entity\CarInterface
* The called Car entity.
*/
public function setName($name);
}
/car.links.menu.yml
# Car menu items definition
entity.car.collection:
title: 'Car list'
route_name: entity.car.collection
description: 'List Car entities'
parent: system.admin_structure
weight: 100
我也有src / CarListBuilder.php(我认为内容与问题无关)
我得到的错误是:
路由“ entity.car.collection”不存在。在...
我检查了路由器表,看起来好像创建了五个链接(“规范”,“添加表单”,“编辑表单”,“删除表单”)中的四个,但未创建“集合”
我检查了Drupal控制台的generate:entity:content生成的代码,但是在任何代码中我都找不到除注释之外定义集合链接的其他位置。
此外,我注意到的另一件事是没有“汽车” base_table。
我在这里想念什么?
答案 0 :(得分:0)
我刚刚使用Drupal 8.6进行了测试,对我来说效果很好,这是我经过的步骤:
$ drupal generate:entity:content
// Welcome to the Drupal Content Entity generator
Enter the module name [aa_11]:
> mymodule
Enter the class of your new content entity [DefaultEntity]:
> Car
Enter the machine name of your new content entity [car]:
>
Enter the label of your new content entity [Car]:
>
Enter the base-path for the content entity routes [/admin/structure]:
>
Do you want this (content) entity to have bundles? (yes/no) [no]:
> no
Is your entity translatable? (yes/no) [yes]:
> no
Is your entity revisionable? (yes/no) [yes]:
> no
$ drupal entity-updates -y
namespace Drupal\mymodule;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Link;
/**
* Defines a class to build a listing of Car entities.
*
* @ingroup mymodule
*/
class CarListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('Car ID');
$header['name'] = $this->t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var $entity \Drupal\mymodule\Entity\Car */
$row['id'] = $entity->id();
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.car.edit_form',
['car' => $entity->id()]
);
return $row + parent::buildRow($entity);
}
}