0

hook_node_insert(NodeInterface $node) gets a NodeInterface parameter. I need to pass it to mymodule_node_operations(EntityInterface $entity) which accepts an EntityInterface parameter.

How do I load this node as EntityInterface type? Basically I need to downcast to the parent class which is Entity.

This is the error I get when attempting to pass $node to the function as NodeInterface.

TypeError: Argument 1 passed to mymodule_node_operations() must be an instance of EntityInterface, instance of Drupal\node\Entity\Node given

2 Answers 2

1

The problem was that I did not include the required class. I added the following to get it to work:

use Drupal\Core\Entity\EntityInterface;
0

Node has EntityBase in its hierarchy. EntityBase implements EntityInterface. This means you can pass a Node to a function that expects EntityInterface, since Node extends a class that implements that interface. This is how OOP works, and why classes are type hinted with interfaces (eg. EntityInterface) rather than a specific class (eg. EntityBase).

2
  • 3
    Perhaps you don't have use Drupal\Core\Entity\EntityInterface; in your code?
    – anonymous
    Commented Jun 16, 2020 at 5:23
  • @anonymous You are absolutely right!!! Thank you :)
    – hga77
    Commented Jun 16, 2020 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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