Create new content entity in drupal 8
Here's a step-by-step guide to creating a new content entity in Drupal 8:
Step 1: Enable the Entity API module
The Entity API module is required for creating custom content entities. To enable it, go to your Drupal 8 site's administration dashboard, click on "Extend" and then click on the "Install" button next to "Entity API".
Step 2: Create a new module
Create a new directory for your module, e.g., my_module
. Inside this directory, create a new file called my_module.info.yml
with the following content:
name: My Module
description: A module for creating a new content entity
type: module
core: 8.x
Step 3: Create the entity class
Create a new file called Entity/MyEntity.php
inside your module directory. This file will contain the definition of your custom content entity. Here's an example:
<?php
namespace Drupal\my_module\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBuilder;
class MyEntity extends \Drupal\Core\Entity\Entity implements EntityInterface {
/**
* {@inheritdoc}
*/
public function getEntityTypeId() {
return 'my_entity';
}
/**
* {@inheritdoc}
*/
public function getBundleEntityType() {
return 'my_entity';
}
/**
* {@inheritdoc}
*/
public function getBundleId() {
return 'my_entity';
}
/**
* {@inheritdoc}
*/
public function getBundleLabel() {
return t('My Entity');
}
/**
* {@inheritdoc}
*/
public function getBundleDescription() {
return t('A custom content entity');
}
}
Step 4: Create the entity type
Create a new file called Entity/MyEntityType.php
inside your module directory. This file will define the entity type for your custom content entity. Here's an example:
<?php
namespace Drupal\my_module\Entity;
use Drupal\Core\Entity\EntityTypeBuilder;
class MyEntityType extends EntityTypeBuilder {
/**
* {@inheritdoc}
*/
public function getEntityType() {
return 'my_entity';
}
/**
* {@inheritdoc}
*/
public function getEntityClass() {
return 'Drupal\my_module\Entity\MyEntity';
}
/**
* {@inheritdoc}
*/
public function getEntityLabel() {
return t('My Entity');
}
/**
* {@inheritdoc}
*/
public function getEntityDescription() {
return t('A custom content entity');
}
}
Step 5: Register the entity type
In your my_module.info.yml
file, add the following lines to register your entity type:
entities:
my_entity:
type: my_entity
label: 'My Entity'
description: 'A custom content entity'
bundle_of: 'my_entity'
Step 6: Create the entity form
Create a new file called Form/MyEntityForm.php
inside your module directory. This file will define the form for creating and editing your custom content entity. Here's an example:
<?php
namespace Drupal\my_module\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
class MyEntityForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array &$form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
// Add your form fields here
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $this->entity->getName(),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save the entity
$this->entity->save();
// Redirect to the entity list
$form_state->setRedirect('entity.my_entity.collection');
return \Drupal::messenger()->addMessage(t('The entity has been saved.'));
}
}
Step 7: Register the entity form
In your my_module.info.yml
file, add the following lines to register your entity form:
forms:
my_entity.form:
type: my_entity_form
route_name: 'my_entity.form'
Step 8: Create the entity list
Create a new file called Controller/MyEntityController.php
inside your module directory. This file will define the controller for listing your custom content entities. Here's an example:
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityListBuilder;
class MyEntityController extends ControllerBase {
/**
* {@inheritdoc}
*/
public function listEntities(EntityManagerInterface $entity_manager) {
$entities = $entity_manager->getStorage('my_entity')->loadMultiple();
return array(
'#theme' => 'my_entity_list',
'#entities' => $entities,
);
}
}
Step 9: Register the entity list
In your my_module.info.yml
file, add the following lines to register your entity list:
routes:
my_entity.list:
path: '/my-entity'
defaults:
_controller: 'Drupal\my_module\Controller\MyEntityController::listEntities'
requirements:
_permission: 'view my entity'
Step 10: Clear the cache
Finally, clear the cache by going to your Drupal 8 site's administration dashboard, clicking on "Configuration" and then clicking on "Development" and then "Clear all caches".
That's it! You should now have a new content entity in your Drupal 8 site. You can create, edit, and list your custom content entities using the forms and controllers you created.