0

I followed How to create a custom entity in Drupal 8 to create a module. When I installed it, I got the following error.

Drupal\Component\Plugin\Exception\PluginNotFoundException: The "dictionary_term" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 150 of /var/www/html/core/lib/Drupal/Core/Entity/EntityTypeManager.php).

What do I need to change in the following code?

dictionary/Entity/Term.php

/**
 * @file
 * Contains \Drupal\content_entity_example\Entity\ContentEntityExample.
 */

namespace Drupal\dictionary\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityChangedTrait;

/**
 * Defines the ContentEntityExample entity.
 *
 * @ingroup dictionary
 *
 *
 * @ContentEntityType(
 * id = "dictionary_term",
 * label = @Translation("Dictionary Term entity"),
 * handlers = {
 * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 * "list_builder" = "Drupal\dictionary\Entity\Controller\TermListBuilder",
 * "form" = {
 * "add" = "Drupal\dictionary\Form\TermForm",
 * "edit" = "Drupal\dictionary\Form\TermForm",
 * "delete" = "Drupal\dictionary\Form\TermDeleteForm",
 * },
 * "access" = "Drupal\dictionary\TermAccessControlHandler",
 * },
 * list_cache_contexts = { "user" },
 * base_table = "dictionary_term",
 * admin_permission = "administer dictionary_term entity",
 * entity_keys = {
 * "id" = "id",
 * "uuid" = "uuid",
 * "user_id" = "user_id",
 * "created" = "created",
 * "changed" = "changed",
 * "pl" = "pl",
 * "en" = "en",
 * },
 * links = {
 * "canonical" = "/dictionary_term/{dictionary_term}",
 * "edit-form" = "/dictionary_term/{dictionary_term}/edit",
 * "delete-form" = "/dictionary_term/{dictionary_term}/delete",
 * "collection" = "/dictionary_term/list"
 * },
 * field_ui_base_route = "entity.dictionary.term_settings",
 * )
 */
class Term extends ContentEntityBase {

  use EntityChangedTrait;

  /**
   * {@inheritdoc}
   *
   * When a new entity instance is added, set the user_id entity reference to
   * the current user as the creator of the instance.
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
// Default author to current user.
    $values += array(
      'user_id' => \Drupal::currentUser()->id(),
    );
  }

  /**
   * {@inheritdoc}
   *
   * Define the field properties here.
   *
   * Field name, type and size determine the table structure.
   *
   * In addition, we can define how the field and its content can be manipulated
   * in the GUI. The behaviour of the widgets used can be determined here.
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

// Standard field, used as unique if primary index.
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the Term entity.'))
      ->setReadOnly(TRUE);

// Standard field, unique outside of the scope of the current project.
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The UUID of the Contact entity.'))
      ->setReadOnly(TRUE);

// Name field for the contact.
// We set display options for the view as well as the form.
// Users with correct privileges can change the view and edit configuration.
    $fields['pl'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Polish'))
      ->setDescription(t('Polish version.'))
      ->setSettings(array(
        'default_value' => '',
        'max_length' => 255,
        'text_processing' => 0,
      ))
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'string',
        'weight' => -6,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string_textfield',
        'weight' => -6,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['en'] = BaseFieldDefinition::create('string')
      ->setLabel(t('English'))
      ->setDescription(t('English version.'))
      ->setSettings(array(
        'default_value' => '',
        'max_length' => 255,
        'text_processing' => 0,
      ))
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'string',
        'weight' => -4,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string_textfield',
        'weight' => -4,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

// Owner field of the contact.
// Entity reference field, holds the reference to the user object.
// The view shows the user name field of the user.
// The form presents a auto complete field for the user name.
    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('User Name'))
      ->setDescription(t('The Name of the associated user.'))
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'author',
        'weight' => -3,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'entity_reference_autocomplete',
        'settings' => array(
          'match_operator' => 'CONTAINS',
          'size' => 60,
          'placeholder' => '',
        ),
        'weight' => -3,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', 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;
  }

}
0

Drupal doesn't find the entity class because the file containing its implementation has been put in the wrong directory. If dictionary is the directory containing the module, the Term.php file for that entity must be in dictionary/src/Entity/Term.php, not dictionary/Entity/Term.php. That is what PSR-4 namespaces require, in Drupal 8, as described in PSR-4 namespaces and autoloading in Drupal 8.

  1. Each module has a namespace that corresponds to its module name.
    Here: Drupal\vegetable\

  2. The module's namespace is mapped to the ./src/ folder in the module directory. Here: Drupal\vegetable\modules/vegetable/src/

Note that the example in that page doesn't mean you have to create a modules directory inside the directory containing the module. It just shows the parent directory of the directory containing the module (which in the example is vegetable). It doesn't even mean the parent directory of the directory containing the module must necessarily be modules.

To have a confirm of the correct directory, it's sufficient to see where Drupal core places the files for its entity classes.

Leaving out core/modules, which is where all Drupal core modules are places, it's clear that, for example, the file containing the Node class is placed in the src/Entity directory inside the directory containing the node.module file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.