Skip to content

Commit 812fb50

Browse files
committed
Allow identifier and content type to be set via template=
* Special thanks to @AndrewRMillar for standing beside me ❤️
1 parent f8300b9 commit 812fb50

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/Block/StaticBlock.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,34 @@ protected function _toHtml(): string
5656
*/
5757
private function createPrismicDocument(): void
5858
{
59-
$data = $this->getData('data') ?? [];
60-
if (! (isset($this->contentType, $this->identifier) || isset($data['uid']) || isset($data['identifier']))) {
59+
$contentType = $this->contentType;
60+
$identifier = $this->identifier;
61+
62+
63+
// Allow using "template" to reference a document (saves XML)
64+
$reference = $this->getReference();
65+
if ($reference !== '*') {
66+
$this->setReference('*');
67+
68+
$elements = explode('.', $reference);
69+
70+
if (count($elements) > 1) {
71+
[$contentType, $identifier] = $elements;
72+
} else {
73+
[$identifier] = $elements;
74+
}
75+
}
76+
77+
$data = $this->getData('data') ?? $this->getData() ?? [];
78+
if (! ($identifier || isset($data['uid']) || isset($data['identifier']))) {
6179
return;
6280
}
6381

64-
$document = new stdClass();
82+
$document = new \stdClass;
6583
$options = $this->api->getOptions();
6684

67-
$document->uid = $data['uid'] ?? $data['identifier'] ?? $this->identifier;
68-
$document->type = $data['content_type'] ?? $this->contentType;
85+
$document->uid = $data['uid'] ?? $data['identifier'] ?? $identifier;
86+
$document->type = $data['content_type'] ?? $contentType;
6987
$document->lang = $data['lang'] ?? $options['lang'];
7088

7189
$this->setDocument($document);

0 commit comments

Comments
 (0)