Seems the default image procedure as it is now doesn't store a file ID or something more useful in that image field. Means the image field doesn't get populated properly.
What I'd try is to create a custom module that adds a config page with a form and a '#type' => 'file_managed'
field. The resulting file ID on upload now becomes variable via \Drupal::configFactory()->getEditable()->set()
(and together with the right config/install and settings YML files).
Then I'd alter the node form via hook_form_BASE_FORM_ID_alter
(see comments) and add a custom validate and/or submit function. Maybe just an element validate callback on the image field. And then check on submit if this field is empty, and if so then retrieve the file ID from the config page via \Drupal::config()->get()
, load the file and save it into the image field (maybe the file ID is just enough).
Now the image field should be populated properly and everything else should work as expected.
Proof of Concept
OK I just tried what I wrote above and it's working quite fine. Except I didn't use the node form but hook_entity_presave
instead to check if the image field is empty before setting the file ID retrieved from the settings value of the uploaded file on said config page.
- Download or clone https://gitlab.com/leymannx/tdi
- into
/modules/custom
.
- Enable the module.
- Go to
/admin/config/media/tdi
and upload a default image, save.
- Now in your IDE or code editor open
/modules/custom/tdi/tdi.module
and
- update the bundle
if ($entity->bundle() == 'XX-YOUR-NODE-TYPE-HERE-XX') {
- and the field name
$entity->get('field_XX-YOUR-FIELD-NAME-HERE-XX')->setValue([['target_id' => $fid]]);
- Save the module file.
- Go to a node with an empty image field, edit and resave it, done.
On every node creation or update this module will check if the image field is empty and add a real managed image with a real file ID then. Now the metatag module should work just fine. If updating all nodes manually is too fiddly I'd recommend you let some code do that instead :)
I'm thinking about extending this approach to let admins choose content types and field names this default image should be set automatically. Maybe tomorrow ;)
Alternatively
If this doesn't work or if it's just too messy, the next thing I'd try is to hook into hook_page_attachments_alter
same as schema_metatag_page_attachments_alter
does to actually attach its values to the HTML head, and try to alter the values there. Which might become messy as well, depending on what methods you have available when you get there or if all you get is just a string to be manipulated quick and dirty.