Matrix initializes its blocks’ field types multiple times – once for each existing block in the field, and one last time to get the initial input HTML that should be used when new blocks are added.
For existing blocks,
$this->element will be set on the field types, referring back to the existing MatrixBlockModel objects representing each of those blocks.
That won’t be the case the time that your field type is just telling Matrix what the initial input HTML should be, however, since no corresponding Matrix block exists yet.
When you call this:
it’s not giving you any errors because
$this->element will just be
null when there is no element set, and there’s nothing wrong with dumping a
But when you call this;
now you’re trying to access a method on
$this->element, which won’t necessarily be set, and that’s when you’re getting that exception.
So what you need to do is ensure that
$this->element is actually set before trying to access
$owner = $this->element->getOwner();
Note that I’m also not assuming that
getOwner() will actually return anything, because that’s not a certainty either. For example if you create/save a new entry but there are validation errors, when the Edit Entry page reloads, you will have MatrixBlockModel objects for each of the blocks you had created, which
$this->element will point to, but they won’t actually have IDs nor an owner yet.