Create a custom Content Entity on drupal 8 (Full)

Submitted by editor on Mon, 10/10/2016 - 17:47
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);

Add new comment