0
votes

I have been struggling to create a custom entity type programmatically in Drupal 8.2.x. The entity type should be translatable and revisionable.

Creating a default configuration (https://www.drupal.org/node/2087879) wouldn't solve the problem, as it doesn't allow defining a class with its handlers.

When trying an example from a book (Drupal 8 Development Cookbook, 2016 by Matt Glaman, Chapter Creating a content entity type), after enabling the module got the error:

Fatal error: Declaration of Drupal\mymodule\Entity\Message::baseFieldDefinitions(Drupal\mymodule\Entity\EntityTypeInterface $entity_type) must be compatible with Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions(Drupal\Core\Entity\EntityTypeInterface $entity_type) in /srv/www/vhosts/test/modules/custom/mymodule/src/Entity/Message.php on line 45

It looked as if the code in the book was outdated. So I was wondering what made the code incompatible. If someone could help me out, please...

The class definition and declaration was as below.

/**
* @file Contains \Drupal\mymodule\Entity\Message
*/
namespace Drupal\mymodule\Entity;
use Drupal\Core\Entity\ContentEntityBase;

/**
* Defines the profile entity class.
*
* @ContentEntityType(
*   id = "message",
*   label = @Translation("Message"),
*   handlers = {
*     "list_builder" = "Drupal\mymodule\MessageListBuilder",
*     "form" = {
*       "default" = "Drupal\Core\Entity\ContentEntityForm",
*       "add" = "Drupal\Core\Entity\ContentEntityForm",
*       "edit" = "Drupal\Core\Entity\ContentEntityForm",
*       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
*     },
*     "route_provider" = {
*       "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
*     },
*   },
*   base_table = "message",
*   fieldable = TRUE,
*   entity_keys = {
*     "id" = "message_id",
*     "label" = "title",
*     "langcode" = "langcode",
*     "uuid" = "uuid"
*   },
*   links = {
*     "canonical" = "/messages/{message}",
*     "edit-form" = "/messages/{message}/edit",
*     "delete-form" = "/messages/{message}/delete",
*     "collection" = "/admin/content/messages"
*   },
*  )
*/

/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface entity_type) {
    $fields['message_id'] = BaseFieldDefinition::create('integer')
        ->setLabel(t('Message ID'))
        ->setDescription(t('The message ID.'))
        ->setReadOnly(TRUE)
        ->setSetting('unsigned', TRUE);
    $fields['langcode'] = BaseFieldDefinition::create('language')
        ->setLabel(t('Language code'))
        ->setDescription(t('The message language code.'))
        ->setRevisionable(TRUE);
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
        ->setLabel(t('UUID'))
        ->setDescription(t('The message UUID.'))
        ->setReadOnly(TRUE);
    $fields['title'] = BaseFieldDefinition::create('string')
        ->setLabel(t('Title'))
        ->setRequired(TRUE)
        ->setTranslatable(TRUE)
        ->setRevisionable(TRUE)
        ->setSetting('max_length', 255)
        ->setDisplayOptions('view', array(
            'label' => 'hidden',
            'type' => 'string',
            'weight' => -5,
        ))
        ->setDisplayOptions('form', array(
            'type' => 'string_textfield',
            'weight' => -5,
        ))
        ->setDisplayConfigurable('form', TRUE);
    $fields['content'] = BaseFieldDefinition::create('text_long')
        ->setLabel(t('Content'))
        ->setDescription(t('Content of the message'))
        ->setTranslatable(TRUE)
        ->setDisplayOptions('view', array(
            'label' => 'hidden',
            'type' => 'text_default',
            'weight' => 0,
        ))
        ->setDisplayConfigurable('view', TRUE)
        ->setDisplayOptions('form', array(
            'type' => 'text_textfield',
            'weight' => 0,
        ))
        ->setDisplayConfigurable('form', TRUE);
    return $fields;
}
2

2 Answers

1
votes

Author here, sorry about that. I think the errata has since been updated. But here is a fix.

It looks like the reason is that EntityTypeInterface was not imported. Try adding use Drupal\Core\Entity\EntityTypeInterface; to the beginning of your code.

<?php
/**
 * @file Contains \Drupal\mymodule\Entity\Message
 */

namespace Drupal\mymodule\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
0
votes

You forgot the class definition like:

class MyEntity extends ContentEntityBase {}