5

When dealing with nodes on Drupal 8, we usually receive an instance from \Drupal\node\Entity\Node, that is great in many aspects, but i'm not sure on how to extend it so that I can use classes that represent something in my own domain, rather that generically using Node. I can make this here:

class Video extends \Drupal\node\Entity\Node {}

Ok. But to have Video::load() and that sort of things, i'm seeing that I need to probably also extend NodeStorage, because everything returns a Node class and type changing seems to be not so easy in PHP from what i'm seeing until now. I'm not sure if this is really the way to go.

I really like to work extending Node(I used to extend EntityDrupalWrapper on D7), as it becomes easier for newcomers to the project and to have stuff like $Video->getFileUrl()

Any ideas?

1

Been a while since benjy and I cooked this up https://www.drupal.org/project/agcobcau so I am not 100% it still runs but it should be quite close to what you want / need.

  • Great module @chx! After a brief "what does this name means???" moment, I saw that you went even further, because this integration for IDEs is great. Is not exactly what I need, the IDE integration, but this module show me that is possible to do what I want, so i'll try to work on top of it. I'll give feedback on the module issue queue after using it. – pedrorocha Sep 14 '15 at 0:45
1

You shouldn't extend Node but rather \Drupal\Core\Entity\ContentEntityBase or \Drupal\Core\Config\Entity\ConfigEntityBase.

  • I don't see the point in extend \Drupal\Core\Entity\ContentEntityBase instead of \Drupal\Core\Entity\Node. By doing that, i'll loose all methods and properties from Node. What I want is to have a class extending Node for one of the available bundles. Also, regardless we are using Drupal or not, this is totally usual from an OOP perspective. – pedrorocha Sep 10 '15 at 22:41
  • Ivan is correct. The best practice is to extend the ContentEntityBase and provide a new entity. – Eyal Dec 20 '15 at 21:13
1

You can't do that. All entities of the same type have use the same class.

I believe there are some issues to allow this, possibly through a per-bundle classmap instead of a single class entry, but it would be quite complicated at the moment, as there are different places where entity classes are instantiated and you'd have to override them all (create and load, for example).

  • Are you saying that this is a current limitation on Drupal 8 or you disagree that conceptually does make sense to do that? I'm trying to figure out an improved way to work with classes on D8, because on Drupal 7 I simply extended EntityDrupalWrapper, like I said above, but I had to manually instantiate it like $node = node_load($nid); $Video = new Video($node); I'm trying to improve this, now that we have many OO concepts inside D8. However, i'm already thinking that maybe i'm really wrong in trying this way, as Node class is an Entity Type class, not making sense to use as bundle. – pedrorocha Sep 13 '15 at 13:16
  • @Berdir, it is possible to do. See drupal.stackexchange.com/a/199395/5982 – SiliconMind May 18 '16 at 17:31
1

The recommended solution is to extend ContentEntityBase which is tedious task if you want to do it manually, or a quick one if you use drupal console and it's generate:entity:content command.

However

It is still possible to extend Node class and so far my experiments prove that it works fine. The important part is that after extending Drupal\node\Entity\Node you also have to tell drupal to use your class instead by implementing hook_entity_type_build.

You can see my detailed answer for How to alter the entity type class in Drupal 8? question that explains how to do this and why it works.

0

I created a module that is able to override Drupal entities (including nodes per type) via the plugin system. Please see the Entity Type Class module page for examples and the download link ofcourse.

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.