Problem/Motivation
I want to use the signature_pad field SVG on a referenced content type through the inline_entity_form widget. If I try to create a new referenced content having a signature_pad via the inline_entity_form, I receive this error:
Error: Call to a member function getFileUri() on null in Drupal\signature_pad\Plugin\Field\FieldWidget\SignaturePadWidget->getInitDataSvg() (line 492 of /var/www/html/mysite/modules/contrib/signature_pad/src/Plugin/Field/FieldWidget/SignaturePadWidget.php).
Steps to reproduce
1. Implement signature_pad on Basic page
2. Make the Article content type reference the Basic page with the signature_pad.
3. Use the inline_entity_reference as the form widget for the reference field.
4. As soon as you try to create new content via the reference, it throws the error above.
Proposed resolution
The problematic code is here:
$file = $imageItem->entity;
$svg = new \DOMDocument();
$svg->loadXML(file_get_contents($file->getFileUri()));
The problem occurs because the getInitDataSvg()
method that the function implements is trying to access a file entity that doesn't exist yet when creating a new entity by $file = $imageItem->entity;
.
A simple null check will solve the problem.
$file = $imageItem->entity;
// Add null check here
if (!$file) { return NULL; }
$svg = new \DOMDocument();
$svg->loadXML(file_get_contents($file->getFileUri()));