CustomBlock has

  public function bundle() {
    return $this->type->value;
  }

This relies on EntityNG magic whizbang.
Depending on where in the callstack this gets called (#1969728: Implement Field API "field types" as TypedData Plugins needs to call $entity->bundle() while the FieldItem object for 'block_body' is being created, during entity_create()), it seems everything might not be properly initialized, and the above returns NULL.

CustomBlock extends EntityNG, and EntityNG just returns $this->bundle, which just works.
Not sure why CustomBlock tries to do differently, but it doesn't seem needed :-)

Same thing for id().

Comments

yched’s picture

Title: CustomBlock::bundle() returns NULL » CustomBlock bundle() and id() needlessly rely on EntityNG getters for its

OK, after some more digging, more precise title, and updated the OP.

yched’s picture

Issue summary: View changes

format

yched’s picture

Title: CustomBlock bundle() and id() needlessly rely on EntityNG getters for its » CustomBlock bundle() and id() needlessly rely on EntityNG getters
yched’s picture

So my first reaction would be to just remove CustomBlock::id() and CustomBlock::bundle(), and let the implementations from EntityNG apply.
But then I see that Node:id() returns $this->get('nid')->value, and Node::bundle() doesn't exist, just lets EntityNG::bundle() return $this->bundle.

So, WTF :-)

id() bundle()
CustomBlock $this->id->value $this->type->value
Node $this->get('nid)->value (defers to EntityNG)
EntityNG $this->id $this->bundle

Seems largely inconsistent :-/

Neither $this->type->value nor $this->get('type')->value work when the bundle() method is called early in the lifecycle of the $entity (in my case, it's in the __construct() of the FieldItem for the 'block_body' field).

yched’s picture

Actually, it seems that CustomBlock is the only class to override EntityNG::bundle() right now.

yched’s picture

Priority: Major » Normal
larowlan’s picture

Assigned: Unassigned » larowlan
larowlan’s picture

Status: Active » Needs review
StatusFileSize
new730 bytes

Lets see what the bot says

larowlan’s picture

green

yched’s picture

Title: CustomBlock bundle() and id() needlessly rely on EntityNG getters » CustomBlock bundle() (and id() ?) needlessly relies on EntityNG getters
Status: Needs review » Reviewed & tested by the community

bundle() at least should be fine.
The situation about id() is still confusing, but I guess we can at least commit this.

FWIW, relying on entityNG getters (magic or not) to access critical properties like id or revision id seems... worrying.

larowlan’s picture

Title: CustomBlock bundle() (and id() ?) needlessly relies on EntityNG getters » CustomBlock bundle() and id() needlessly rely on EntityNG getters
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new873 bytes

drops the id function too

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

back to previous status unless bot disagrees

larowlan’s picture

Issue tags: +Quick fix

I think this qualifies for the Quick Fix tag

yched’s picture

Well, that's where I'm confused. If EntityNG::id() is good enough, then why do Node, Comment (didn't check the other ones) bother adding their own complex override ?

andypost’s picture

@yched Because node and comment have 'nid' (cid) as keys that's not id

yched’s picture

@andypost: of course, but that's not the point :-p

- If EntityNG::id() just does $this->id, why can't Node::id() do $this->nid ?

- node's 'bundle key' is 'type', not 'bundle'. Yet $node->bundle() is EntityNG:bundle(), which is just $this->bundle - and it works.
So why would it work for the "bundle key" and not for the "id key" ?

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

More accurate description of the bug