Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create a custom Content Entity on drupal 8 (Full)

By editor , 10 October, 2016
Question

Create a new custom entity on Drupal 8, with all necessary user interfaces.

This is a full Example of the Entity 'Vehicle' with user interfaces for:
- Actions (Create, Edit, Delete)
- Listing builder
- Entity settings pages
   - Manage fields
   - Manage form display
   - Manage display

 

Few tips

Read values of an entity

Here : $this = The entity.

Get a field value:
$this->get('fieldname')->value

Get an entity from entity_reference field:
$this->get('entity_field_name')->entity;

Get entity id from entity_reference field:
$quote->get('entity_field_name')->entity->id()
OR
//$this->getEntityKey('entity_field_name'); //Not tested

File Field (Example)

    $fields['fid'] = BaseFieldDefinition::create('file')
      ->setLabel(t('Photo (fid)'))
      ->setDescription(t('The photo'))
      ->setSetting('target_type', 'file')
      ->setSetting('file_extensions', 'jpg jpeg')
      ->setSetting('file_directory', 'images/myphotosdir')
      ->setSetting('max_filesize', 1024*1024)
      ->setDisplayOptions('view', array(
        'type' => 'file', 'weight' => -8,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'file', 'weight' => -8,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setReadOnly(TRUE);

File
vehicle_full_wviews.zip (9.4 KB)

Tags

  • Drupal 8
  • Code
  • Entity
  • Add new comment
Create a custom Content Entity on drupal 8 (Basic)
Edit and Update Entity schema on drupal 8
Custom Views Data handler for a custom Entity on drupal 8
Custom Content Entity Field types of drupal 8

Comments3

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Daniels (not verified)

9 years 10 months ago

Thank you for the File field…

Thank you for the File field example. Very helpful!

  • Reply

Alvaro (not verified)

9 years 8 months ago

About the File field

Could you give as an example to add multiple images?

I want to have my custom entity with multiple images, I understand that this will be done by adding another  table referencing to my custon entity, but I couldn't do that.

  • Reply

editor

9 years 8 months ago

In reply to About the File field by Alvaro (not verified)

Just add cardinality

...
->setLabel(t('Photo'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
....

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer