I want to add a value from my option field as a class to an element.
Normally I do it like this:
<div class="block <?php if($block->textAlign() == "center"): ?>ta-center<?php endif ?> <?php if($block->textAlign() == "right"): ?>ta-right<?php endif ?>">
But now I am using a plugin which uses a kind of short version of php and I don’t know how I can insert the value now.
I want to add the value as a class to the following element inside the foreach loop:
<?php foreach ($form->fields() as $field) : ?>
<?php $ltag = $isFieldGroup ? 'fieldset' : 'div' ?>
<<?= $ltag ?> class="block block-field-<?= $field->type(true) ?>" data-id="<?= $field->slug() ?>">
<?php endforeach ?>
I tried many things and searched a lot on php websites but couldn’t make it work.
It has to look roughly like this:
<<?= $ltag ?> class="block <?= $field->textAlign() ?> block-field-<?= $field->type(true) ?>" data-id="<?= $field->slug() ?>">
Blueprint:
textAlign:
label: Text align
type: select
options:
center: center
right: right
Thank you in advance for any help!
texnixe
February 7, 2023, 12:21pm
2
It would be helpful to know which plugin you are using, I would assume you have to use something like $field->value()
, but the context how this field code relates to the block is not clear.
Thank you @texnixe ,
I tried both
<?= $field->textAlign()->value() ?>
and
<?= $field->value() ?>
but unfortunately does not insert any text, but also throws no error.
I am using a plugin for a form block: Form Block | Kirby CMS
This is the whole form.php which renders every field inside a form block:
<?php $form = $block; ?>
<?php if ($form->showForm()) : ?>
<form method="post" id="<?= $form->id() ?>" action="<?= $page->url() . "#" . $form->id() ?>" novalidate>
<div class="form-block max-width">
<?php foreach ($form->fields() as $field) : ?>
<?php $isFieldGroup = ($field->type() === 'formfields/radio' || $field->type() === 'formfields/checkbox') ?>
<?php $gtag = $isFieldGroup ? 'legend' : 'label' ?>
<?php $ltag = $isFieldGroup ? 'fieldset' : 'div' ?>
<<?= $ltag ?> class="form-block-field <?= MY CLASS SHOULD BE INSERTED HERE ?> form-block-field-<?= $field->type(true) ?>" data-id="<?= $field->slug() ?>">
<<?= $gtag ?> for="<?= $field->slug() ?>">
<span class="form-block-field-label-text"><p><?= $field->label() ?></p></span>
<span class="form-block-field-label-required" aria-hidden="true"><p><?= $field->required('asterisk') ?></p></span>
</<?= $gtag ?>>
<?php if (!$field->isValid()) : ?>
<span id="<?= $field->id() ?>-error-message" class="form-block-message form-block-field-invalid"><p><?= $field->errorMessage() ?></p></span>
<?php endif ?>
<?= $field->toHtml() ?>
</<?= $ltag ?>>
<?php endforeach ?>
<div class="form-block-field form-block-field-hpot">
<label for="<?= $form->honeypotId() ?>" aria-hidden="true"> <?= ucfirst($form->honeypotId()) ?></label>
<input type="search" id="<?= $form->honeypotId() ?>" name="<?= $form->honeypotId() ?>" value="" autocomplete="off" tabindex="1000" required />
</div>
<?php if (!$form->isValid()) : ?>
<div class="form-block-message form-block-invalid column">
<p>
<?= $form->errorMessage() ?>
</p>
</div>
<?php endif ?>
<div class="form-block-button form-block-submit column">
<input type="submit" name="<?= $form->id() ?>" value="<?= $form->message('send_button') ?>">
</div>
</div>
</form>
<?php endif ?>
<?php if ($form->isFatal()) : ?>
<div class="form-block-message form-block-fatal column">
<p>
<?= $form->errorMessage() ?>
</p>
</div>
<?php endif ?>
<?php if ($form->isSuccess()) : ?>
<div class="form-block-message form-block-success column">
<p>
<?= $form->successMessage() ?>
</p>
</div>
<?php endif ?>
<style>
.form-block-field-hpot {
opacity: 0.001;
position: absolute;
z-index: -1;
}
</style>
Please let me know what else you would need.
Just solved it, I don’t know why it’s suddenly working but it’s just
<?= $field->textAlign() ?>