Closed nathangross closed 1 year ago
What does Button.php and it's view look like?
My workflow is typically building out blocks as components (so they can be used in the theme its self if needed) and then using the component inside of my block view.
This does mean I have to maintain both the block and component, but generally 99% of the logic will live inside of the component and my block logic is just pulling the fields and handling the data to then pass to the component inside of the block view.
Hope that makes sense.
Yep, that makes a lot of sense—especially for smaller, reusable bits like buttons, avatars, etc. I'll likely adopt that as I go. I'm already using the Field Partials so I can reuse field groups (one of the main reasons I came across your acf-composer package!! 😀)
In this case, I was getting the error when working on a Card block. I'll paste that code below but it seems like something else is going on.
To perhaps clarify a bit, I've got a blade component (used as a parent wrapper for all my block views) views/components/acf-block.blade.php
that currently looks like:
<div class="m-2 flex w-full border p-2">
{{ $slot }}
</div>
Then in a card view block views/blocks/card.blade.php
something like:
<x-acf-block>
<div class="flex max-w-2xl flex-col">
@if ($image)
<img src={{ $image['url'] }} alt="{{ $image['alt'] }}">
@endif
<div class="text-label-light-primary flex w-full flex-col space-y-4">
@if ($title)
<div class="font-serif text-2xl md:text-4xl">
{{ $title }}
</div>
@endif
<InnerBlocks />
</div>
</div>
</x-acf-block>
On my card block, I'd like to add a new class max-w-lg
to the parent acf-block component so I update it to utilize the $attributes->merge()
function (I use this all the time in laravel projects)
<div {{ $attributes->merge(['class' => 'm-2 flex w-full border p-2']) }}>
{{ $slot }}
</div>
and that's what causes the error pasted above. I feel like I'm missing something very obvious 😅
My current Card.php file
<?php
namespace App\Blocks;
use Log1x\AcfComposer\Block;
use StoutLogic\AcfBuilder\FieldsBuilder;
class Card extends Block
{
/**
* The block name.
*
* @var string
*/
public $name = 'Card';
/**
* The block description.
*
* @var string
*/
public $description = 'A simple Card block.';
/**
* The block category.
*
* @var string
*/
public $category = 'berea-next';
/**
* The block icon.
*
* @var string|array
*/
public $icon = 'editor-ul';
/**
* The block keywords.
*
* @var array
*/
public $keywords = [];
/**
* The block post type allow list.
*
* @var array
*/
public $post_types = [];
/**
* The parent block type allow list.
*
* @var array
*/
public $parent = [];
/**
* The default block mode.
*
* @var string
*/
public $mode = 'preview';
/**
* The default block alignment.
*
* @var string
*/
public $align = '';
/**
* The default block text alignment.
*
* @var string
*/
public $align_text = '';
/**
* The default block content alignment.
*
* @var string
*/
public $align_content = '';
/**
* The supported block features.
*
* @var array
*/
public $supports = [
'align' => true,
'align_text' => false,
'align_content' => false,
'full_height' => false,
'anchor' => false,
'mode' => false,
'multiple' => true,
'jsx' => true,
];
/**
* The block styles.
*
* @var array
*/
public $styles = [];
/**
* The block preview example data.
*
* @var array
*/
public $example = [];
/**
* Data to be passed to the block before rendering.
*
* @return array
*/
public function with()
{
return [
'title' => $this->title(),
'image' => $this->image(),
];
}
/**
* The block field group.
*
* @return array
*/
public function fields()
{
$nextCard = new FieldsBuilder('next_card');
$nextCard
->addText('title')
->addImage('image')
->addSelect('layout', [
'choices' => [
'horizontal' => 'Horizontal',
'vertical' => 'Vertical',
],
'default_value' => 'default',
]);
return $nextCard->build();
}
/**
* Return the title field.
*
* @return array
*/
public function title()
{
return get_field('title') ?: "Title goes here";
}
/**
* Return the image field.
*
* @return array
*/
public function image()
{
return get_field('image');
}
/**
* Assets to be enqueued when rendering the block.
*
* @return void
*/
public function enqueue()
{
//
}
}
Even if I just use {{ $attributes }} it give me that error
<div {{ $attributes }}>
{{ $slot }}
</div>
That's really strange. Ideally what you want to do would work. I'll try to look into it and see if I can figure something out.
Weird this is working for me.
# app/View/Components/AcfBlock.php
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class AcfBlock extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.acf-block');
}
}
# views/components/acf-block.blade.php
<div {{ $attributes->merge(['class' => 'test']) }}>
{!! $slot !!}
</div>
# views/blocks/google-map.blade.php
<x-acf-block>
<div class="{{ $block->classes }} relative my-8 last:mb-0">
<iframe
class="w-full border shadow-sm h-96 {{ $block->preview ? 'pointer-events-none' : '' }}"
src="{{ $uri }}"
></iframe>
</div>
</x-acf-block>
That is strange. At first, I thought it was because I was using an anonymous component—but still getting the same error when using a class component.
Also strange that it says I'm passing a Button Block. If I {{ dd($attributes) }}
on the acf-block component, sure enough it shows a Button
I must have a typo or missing semicolon somewhere
I don't see any typo's in the example you showed. Very strange.
The only difference I can really spot is I used {!! $slot !!}
what if you remove <InnerBlocks />
? I didn't test on a block that had it. I don't really know why that'd be an issue but worth a shot I guess.
Ugh 🤦♂️ I figured it out. I was passing the whole block object :block="$block"
on my button view block. Error messages are actually pretty spot on 😂
I don't exactly remember what I was doing with that but it was when I was playing with another sage acf package.
Thanks for your help and sorry for dragging you along for my error!
My workflow is typically building out blocks as components (so they can be used in the theme its self if needed) and then using the component inside of my block view.
This does mean I have to maintain both the block and component, but generally 99% of the logic will live inside of the component and my block logic is just pulling the fields and handling the data to then pass to the component inside of the block view.
I'm curious if you could provide any additional insights to how you handle this. In my case I've got a button block, and a button-stack block. The stack is setup to just have a repeatable button block with some wrapper layout styles.
So, like you mentioned, I'd like to maintain just one button component and utilize it in both the button block and the button stack block. I'm finding it tricky with how to handle the data though.
In the single button, I have direct access to the label like $label
but in the button stack, I'm in a loop ($buttons as $button)
so I access it through $button['label']
. How do you typically handle this in a single component? Is there a way to just pass the whole button as an object? So it could just be a consistent $button->label
whether it's in a single button or a loop of buttons?
Does that make sense?
I've created a component
acf-block.blade.php
that has some very basic styles applied that I'd like on all my ACF blocks.Nesting the blocks (/views/blocks) inside components seems to work fine until I tried to utilize the $attributes bag. I wanted to be able to add or override some of these classes so I tried to utilize
$attributes->merge()
but when I do so, I get the following error:Is it possible to nest these ACF block views inside normal laravel components?