super-table icon indicating copy to clipboard operation
super-table copied to clipboard

How can i print content for each field using PHP?

Open vanyabrovary opened this issue 1 year ago • 1 comments

Question

How can i print content for each field using PHP?

There is my code:

// Get Entry
$enties = Entry::findOne(['id' => 1525, 'siteId' => 7])->contentBlocks->all();

// Get SuperTable block types
$stFields = Craft::$app->getFields()->getAllFields(
    'superTableBlockType:' .
    SuperTable::$plugin->getService()->getBlockTypesByFieldId($field->id)[0]->uid
);

// Print each SuperTable fielId and fieldHandle
foreach ($stFields as $stField) {
    print $stField->id . " " . $stField->handle  . "\n";
}

Thaks.

Additional context

No response

vanyabrovary avatar Sep 11 '23 06:09 vanyabrovary

There's an example on the docs, but it's really just like Twig

$entry = Entry::find()->id(1234)->one();

foreach ($entry->mySuperTableField->all() as $block) {
    foreach ($block->getFieldLayout()->getCustomFields() as $field) {
        $value = $block->getFieldValue($field->handle);

        var_dump($value);
    }
}

If you know the field you want to fetch content for, that's simpler.

$entry = Entry::find()->id(1234)->one();

foreach ($entry->mySuperTableField->all() as $block) {
    $value = $block->myFieldHandle;
}

engram-design avatar Sep 11 '23 11:09 engram-design