0

I created a bundle class that extends the taxonomy term class, but I get this error.

Error: Interface 'Drupal\my_artists\Entity\ArtistTermInterface' not found in include() (line 15 of /xxx/drupal/web/modules/private/my_artists/src/Entity/ArtistTerm.php)

I set the bundle class in the my_artists.module file.

use Drupal\my_artists\Entity\ArtistTerm;

function my_artists_entity_bundle_info_alter(array &$bundles): void {
  $bundles['taxonomy_term']['artist']['class'] = ArtistTerm::class;
}

In /src/ArtistTermInterface.php I defined the bundle class interface.

namespace Drupal\my_artists\Entity;
    
use Drupal\taxonomy\TermInterface;
    
/**
  * Represents a ArtistTerm entity.
  */
interface ArtistTermInterface extends TermInterface {}

In /src/Entity/ArtistTerm.php, I defined the bundle class.

namespace Drupal\my_artists\Entity;
    
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\taxonomy\Entity\Term;
    
/**
 * Defines the ArtistTerm class.
 */
class ArtistTerm extends Term implements ArtistTermInterface {

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    $this->set('field_some_field', 'some value');
    parent::preSave($storage);
  }

}

Why do I get the error?

2
  • Did you put use Drupal\my_artists\Entity\Artist; on the top of the file containing my_artists_entity_bundle_info_alter()?
    – apaderno
    Jun 15 at 14:59
  • Either is that, or there is a typo in the code or the filenames. I take the module is installed, but there could be another module that is referring the Artist class.
    – apaderno
    Jun 15 at 15:27

1 Answer 1

1

Entity bundle classes go in /mymodule/src/Entity/Bundle/MyClass.php.

To easily generate the bundle class and see what may be wrong, try the drush command drush generate entity:bundle-class and follow the prompts.

Drush docs

1

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