Используя php SDK, я хотел бы получить весь контент для целевой страницы, это будет включать разделы с разными типами контента / компонентами с разными полями для каждой записи.
например.
Раздел 1 - герой
Раздел 2 - статья
Раздел 3 - изображение
В. Как пройти и получить содержимое связанного раздела.
If an entry contains a link to an asset or another entry, the SDK will automatically load it.
Я изо всех сил пытаюсь понять документация. Как именно мне проникнуть в эти ценности?
<?php
$query = new \Contentful\Delivery\Query();
$query->setContentType('page_landing')
->where("fields.urlHandle[match]", $slug);
$products = $client->getEntries($query);
foreach ($products as $product) {
echo $product['name'];
// Example of what I am trying to achieve
if ($product['sections']['id']=='hero'){
$title= $product['sections']['hero']['title'];
}
if ($product['sections']['id']=='article'){
$text = $product['sections']['article']['text'];
}
}
?>
🤔 А знаете ли вы, что...
С PHP можно легко интегрировать с другими технологиями, такими как JavaScript и HTML.
Я хотел бы знать, есть ли способ лучше, в документации не так много примеров. Вот где я закончил, но чувствую, что должен быть более простой способ.
<?php
require_once 'vendor/autoload.php';
require_once 'vendor/credentials.php';
// if we need to use rich text
$renderer = new \Contentful\RichText\Renderer();
// Get the section entry id's
foreach ($products as $product) {
$dataJson = json_encode($product);
$revertJson[] = json_decode($dataJson, true);
foreach ($revertJson as $Json) {
foreach ($Json['fields']['sections'] as $entries) {
$entryID[] = $entries['sys']['id'];
}
}
}
// Loop out the sections
foreach ($entryID as $entry) {
// Get individual linked entries
$entry = $client->getEntry($entry);
$type = $entry->getSystemProperties()->getcontentType();
$json = json_encode($type);
$comp = json_decode($json, true);
if ($comp['sys']['id'] == 'Hero') {
// field
echo $entry['urlHandle'];
// rich text
echo $renderer->render($entry['text']);
// asset
$asset = $client->getAsset($entry['imageId']);
echo $asset->getFile()->getUrl();
} elseif ($comp['sys']['id'] == 'Landmark') {
echo $entry['title'];
}
}