Skip to content

Commit 80320e1

Browse files
committed
A new example for nesting components
1 parent aa3e74c commit 80320e1

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

README.md

+75
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,81 @@ echo " SLUG: " . $storyCreated->slug() . PHP_EOL;
604604

605605
```
606606

607+
## Another Example with a Nested Component
608+
609+
Now that we've seen how to programmatically create a story (content) with fields like title, body, and image, let's dive deeper into handling nested components.
610+
611+
### Scenario
612+
613+
You have a `default-page` content type that includes a *Blok* field. The *Blok* field allows you to nest components within your content. This means you can add a *Hero Section* component, followed by an *Image Text Section* component, and so on.
614+
615+
### Goal
616+
The objective is to:
617+
618+
- Set up a *Hero Section* component.
619+
- Set up an *Image Text Section* component.
620+
- Add these components to a story.
621+
622+
To demonstrate deeper levels of nesting, we will also set up a *Button* component and nest it within the *Image Text Section* component.
623+
624+
```php
625+
626+
$client = new ManagementApiClient("yourpersonalaccesstoken");
627+
$spaceId = "yourspaceid";
628+
$storyApi = new StoryApi($client, $spaceId);
629+
630+
// Setting up the hero-section
631+
$heroSection = new StoryComponent("hero-section");
632+
$heroSection->set("headline", "Hello World");
633+
// We are going to setup an external image as background
634+
$heroSection->setAsset("background_image", Asset::emptyAsset()->setExternalUrl("https://images.pexels.com/photos/18853169/pexels-photo-18853169/free-photo-of-tower-old-north-church-mirroring-in-puddle.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"));
635+
$heroSection->setAsset("background_video", Asset::emptyAsset());
636+
$heroSection->set("text_color", "light");
637+
$heroSection->set("vertical_alignment", "center");
638+
$heroSection->set("horizontal_alignment", "center");
639+
640+
// Setting up the Button
641+
$button = new StoryComponent("button");
642+
$button->set("label", "Click here");
643+
$button->set("style", "default");
644+
$button->set("background_color", "primary");
645+
$button->set("text_color", "light");
646+
$button->set("size", "small");
647+
$button->set("border_radius", "small");
648+
649+
650+
// Setting up the Image Text Section
651+
$imageTextSection = new StoryComponent("image-text-section");
652+
$imageTextSection->set("headline", "Hello World");
653+
$imageTextSection->setAsset("image", Asset::emptyAsset());
654+
// Adding the Button to the Image Text Section (field name `button`)
655+
$imageTextSection->addBlock("button", $button);
656+
657+
// Let's create the content type `default-page`
658+
$page = new StoryComponent("default-page");
659+
// Adding the Hero Section and the Image Text Section to the `body` field
660+
$page->addBlock("body", $heroSection);
661+
$page->addBlock("body", $imageTextSection);
662+
663+
$random = random_int(1_000_000, 9_999_999);
664+
$story = new Story(
665+
"Landing " . $random,
666+
"landing-" . $random,
667+
$page
668+
);
669+
670+
$storyCreated = $storyApi->create($story)->data();
671+
echo "Story created with ID : " . $storyCreated->id();
672+
echo PHP_EOL;
673+
$storyCreated->dump();
674+
echo PHP_EOL;
675+
// If we want to publish the story immediately ...
676+
$storyPublished = $storyApi->publish($storyCreated->id())->data();
677+
echo "Story published with ID : " . $storyPublished->id();
678+
echo PHP_EOL;
679+
echo "Story published at : " . $storyPublished->publishedAt();
680+
echo PHP_EOL;
681+
```
607682

608683
## Handling Workflows
609684

0 commit comments

Comments
 (0)