I've been trying to programmatically create a custom entity type, yet I keep getting an error message I can't seem to figure out. Here is my code:
/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
I also have src/CarListBuilder.php (content is irrelevant to the problem, I think)
The error I'm getting is:
Route "entity.car.collection" does not exist. in...
I checked in the router table and it looks like only four out of the five links ("canonical","add-form","edit-form","delete-form") were created but not "collection".
I inspected the code generated by Drupal console's generate:entity:content, yet I can't find in any of the code another place the collection link is defined apart from the annotation.
Also, another thing I noticed was that there is no "car" base_table.
What am I missing here?
I have just tested with Drupal 8.6 and it worked fine for me, these are the steps I went throu:
$ 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);
}
}