2

使用 php SDK,我想获取登录页面的所有内容,这将包括具有不同内容类型/组件的部分,每个条目具有不同的字段。

例如。

第 1 节 - 英雄

第 2 节 - 文章

第 3 部分 - 图像

问:如何循环并获取链接的部分内容。

如果条目包含指向资产或其他条目的链接,SDK 将自动加载它。

我正在努力理解文档。我究竟如何打破这些价值观?

<?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'];
    } 
}
?>
4

1 回答 1

0

我很想知道是否有更好的方法,文档没有很多示例。下面是我结束的地方,但觉得必须有一个更简单的方法。

<?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'];
    }
}
于 2021-04-06T20:47:36.160 回答