|
| 1 | +# Contentstack PHP Utils SDK: |
| 2 | + |
| 3 | +Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More. |
| 4 | + |
| 5 | +This guide will help you get started with Contentstack PHP Utils SDK to build apps powered by Contentstack. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- PHP version 5.5.0 or later |
| 10 | + |
| 11 | +## SDK Installation and Setup |
| 12 | + |
| 13 | +To set up the Utils SDK in your PHP project, install it via gem: |
| 14 | +```sh |
| 15 | +composer require contentstack/utils |
| 16 | +``` |
| 17 | + |
| 18 | +If you are using Contentstack PHP SDK, then “contentstack/utils” is already imported into your project. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +Let’s learn how you can use Utils SDK to render embedded items. |
| 23 | + |
| 24 | +### Create Render Option: |
| 25 | + |
| 26 | +To render embedded items on the front-end, use the renderOptions function, and define the UI elements you want to show in the front-end of your website, as shown in the example code below: |
| 27 | +```php |
| 28 | +<?php |
| 29 | + |
| 30 | +declare(strict_types=1); |
| 31 | + |
| 32 | +namespace Sample/App; |
| 33 | + |
| 34 | +use Contentstack\Utils\Resource\EntryEmbedable; |
| 35 | +use Contentstack\Utils\Resource\RenderableInterface; |
| 36 | +use Contentstack\Utils\Resource\EmbeddedObject; |
| 37 | +use Contentstack\Utils\Model\Option; |
| 38 | +use Contentstack\Utils\Model\Metadata; |
| 39 | +use Contentstack\Utils\Enum\StyleType; |
| 40 | + |
| 41 | +class CustomOption extends Option { |
| 42 | + function renderOptions(array $embeddedObject, Metadata $metadata): string |
| 43 | + { |
| 44 | + switch ($metadata->getStyleType()) { |
| 45 | + case StyleType::get(StyleType::BLOCK): |
| 46 | + if ($metadata->contentTypeUID === 'product') { |
| 47 | + return "<div> |
| 48 | + <h2 >".$embeddedObject["title"]."</h2> |
| 49 | + <img src=".$embeddedObject["product_image"]["url"]. "alt=".$embeddedObject["product_image"]["title"]."/> |
| 50 | + <p>".$embeddedObject["price"]."</p> |
| 51 | + </div>" |
| 52 | + } |
| 53 | + case StyleType::get(StyleType::INLINE): |
| 54 | + return "<span><b>".$embeddedObject["title"]."</b> -".$embeddedObject["description"]."</span>"; |
| 55 | + case StyleType::get(StyleType::LINK): |
| 56 | + return "<a href=".$metadata->getAttribute("href")->value |
| 57 | +.">".$metadata->getText()."</a>" |
| 58 | + case StyleType::get(StyleType::DISPLAY): |
| 59 | + return "<img src=".$metadata->getAttribute("src")->value." alt='".$metadata->getAttribute("alt")->value." />"; |
| 60 | + case StyleType::get(StyleType::DOWNLOAD): |
| 61 | + return "<a href=".$metadata->getAttribute("href")->value |
| 62 | +.">".$metadata->getText()."</a>" |
| 63 | + } |
| 64 | + return $resultString; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | +## Basic Queries |
| 69 | +
|
| 70 | +Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry. |
| 71 | +
|
| 72 | +### Fetch Embedded Item(s) from a Single Entry: |
| 73 | +
|
| 74 | +To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type’s UID, and entry’s UID. Then, use the includeEmbeddedItems function as shown below: |
| 75 | + |
| 76 | +```php |
| 77 | +use Contentstack\Contentstack; |
| 78 | +use Contentstack\Utils\Model\Option; |
| 79 | + |
| 80 | +$stack = Contentstack::Stack('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>'); |
| 81 | +$entry = $stack->ContentType('<CONTENT_TYPE_UID>')->Entry('<ENTRY_UID>')->includeEmbeddedItems()->toJSON()->fetch(); |
| 82 | +$render_rich_text = Contentstack::renderContent($entry['rte_field_uid'], new Option($entry)); |
| 83 | +``` |
| 84 | + |
| 85 | +If you want to render embedded items using the CustomOption function, you can refer to the code below: |
| 86 | +```php |
| 87 | +$rendered_rich_text = Contentstack.render_content($entry['rte_field_uid'], new CustomOption($entry)); |
| 88 | +``` |
| 89 | +### Fetch Embedded Item(s) from Multiple Entries |
| 90 | + |
| 91 | +To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type’s UID. |
| 92 | +```php |
| 93 | +use Contentstack\Contentstack; |
| 94 | +use Contentstack\Utils\Model\Option; |
| 95 | + |
| 96 | +$stack = Contentstack::Stack('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>'); |
| 97 | +$result = $stack->ContentType('<CONTENT_TYPE_UID>')->Query()->toJSON()->includeEmbeddedItems()->find() |
| 98 | +for($i = 0; $i < count($result[0]); $i++) { |
| 99 | + $entry = $result[0][$i]; |
| 100 | + $render_rich_text = Contentstack::renderContent($entry['rich_text_content'], new Option($entry)); |
| 101 | +} |
| 102 | +``` |
0 commit comments