How can I display the field specified in field.blocks.text.name
in layouts.php
?
I’ve looked at the guide, but I’m not sure how to write it.
This is because blocks appear directly in the column within the layout, and I am unsure how to proceed.
snippets/layouts.php
<?php
?>
<?php foreach ($field->toLayouts() as $layout): ?>
<section class="grid margin-xl" id="<?= esc($layout->id(), 'attr') ?>" style="--gutter: 1.5rem">
<?php foreach ($layout->columns() as $column): ?>
<div class="column" style="--columns:<?= esc($column->span(), 'css') ?>">
<div class="text" style="font-family: <?= $column->text()->fontfamily() ?>;">
<?= $column->blocks() ?>
</div>
</div>
<?php endforeach ?>
</section>
<?php endforeach ?>
blueprints/blocks/text.php
# The blocks/image template is automatically assigned to all uploaded
# images in the blocks field. We don't really need a separate blueprint
# for this and can therefor extend the already existing files/image blueprint
name: field.blocks.text.name
icon: text
wysiwyg: true
preview: text text
fields:
fontfamily:
label: Font family
type: text
I want the field specified in field.blocks.text.name
to be applied as shown below. You can see the part of “font-family” (I know it is wrong. but I don’t know how to write)
snippets/layouts.php
...
<div class="text" style="font-family:<?= $column->blocks()->block()->text()->fontfamily() ?>;">
<?= $column->blocks() ?>
</div>
Somehow, this does not make sense. You have a text block with a field fontfamily, but that block is not available at the stage where you want to add that information. Why is the standard text block overwritten with a block that only contains a fontfamily field? Doesn’t seem to make sense (unless there are more fields in that block type).
@texnixe
Yes, the code I wrote doesn’t make sense. I wrote nonsensical code to express that I am unsure how to write it correctly.
Is there a way to retrieve and use the field within field.blocks.text.name
? I want to input the fontfamily that the user specified for each column. (In fact, there are several fields besides the fontfamily, but I mentioned the font family as a representative example since it’s not necessary to list them all.)
What if there are multiple text fields with different fontfamily specifications in a single column?
// first text block in column
$textBlock = $column->blocks()->filterBy('type', 'text')->first();
if ($textBlock) {
echo $textBlock->fontfamily();
}
@texnixe
I’m sorry, but I’m not sure if I understood correctly.
I don’t plan to apply different fontfamilies within a single column. The ‘fontfamily’ will be applied only once for each column. Therefore, I intended to apply styles by including style="...code..."
within each “div”.
I wanted to ask if it’s possible to have the following behavior:
If you specify fontfamily A in a single column, it will be rendered as style="font-family: A;"
, and if you specify fontfamily B, it will be rendered as style="font-family: B;"
.
Is this achievable?
I think using “fontfamily” as an example may have made you more confused.