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

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).

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

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;
| 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.