Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i print content for each field using PHP? #533

Open
vanyabrovary opened this issue Sep 11, 2023 · 1 comment
Open

How can i print content for each field using PHP? #533

vanyabrovary opened this issue Sep 11, 2023 · 1 comment
Labels

Comments

@vanyabrovary
Copy link

vanyabrovary commented Sep 11, 2023

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

@engram-design
Copy link
Member

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants