Skip to content

Commit

Permalink
Allow identifier and content type to be set via template=
Browse files Browse the repository at this point in the history
* Special thanks to @AndrewRMillar for standing beside me ❤️
  • Loading branch information
JeroenBoersma committed May 31, 2024
1 parent f8300b9 commit 812fb50
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Block/StaticBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,34 @@ protected function _toHtml(): string
*/
private function createPrismicDocument(): void
{
$data = $this->getData('data') ?? [];
if (! (isset($this->contentType, $this->identifier) || isset($data['uid']) || isset($data['identifier']))) {
$contentType = $this->contentType;
$identifier = $this->identifier;


// Allow using "template" to reference a document (saves XML)
$reference = $this->getReference();
if ($reference !== '*') {
$this->setReference('*');

$elements = explode('.', $reference);

if (count($elements) > 1) {
[$contentType, $identifier] = $elements;
} else {
[$identifier] = $elements;
}
}

$data = $this->getData('data') ?? $this->getData() ?? [];
if (! ($identifier || isset($data['uid']) || isset($data['identifier']))) {
return;
}

$document = new stdClass();
$document = new \stdClass;
$options = $this->api->getOptions();

$document->uid = $data['uid'] ?? $data['identifier'] ?? $this->identifier;
$document->type = $data['content_type'] ?? $this->contentType;
$document->uid = $data['uid'] ?? $data['identifier'] ?? $identifier;
$document->type = $data['content_type'] ?? $contentType;
$document->lang = $data['lang'] ?? $options['lang'];

$this->setDocument($document);
Expand Down

0 comments on commit 812fb50

Please sign in to comment.