Closed chrisRidgers closed 5 years ago
I've noticed what i think is similar behaviour elsewhere, my IDE can't autocomplete code off the back of repeater/ conditions.
Example code:
<?php
use StoutLogic\AcfBuilder\FieldsBuilder;
class StoutAcfBridge
{
public function getHeroFields()
{
$heroBuilder = new FieldsBuilder('hero');
$heroBuilder
->addSelect('background_color', [
'choices' => [
'white' => 'White',
'purple' => 'Purple',
'grape' => 'Grape',
'pink' => 'Pink',
'light-pink' => 'Light Pink',
'dark-blue' => 'Dark Blue',
'blue' => 'Blue',
'light-blue' => 'Light Blue',
'red' => 'Red',
'orange' => 'Orange',
],
'default_value' =>
[
'0' => 'purple'
]
])
->addFile('background_video',
[
'mime_types' => 'mp4,avi,flv'
])
->addImage('background_image', [
'return_format' => 'id',
'conditional_logic' => [
[
'field' => 'background_video',
'operator' => '==empty',
]
]
])
->addRange('background_image_opacity', [
'conditional_logic' => [
[
[
'field' => 'background_image',
'operator' => '!=empty'
]
],
[
[
'field' => 'background_video',
'operator' => '!=empty'
]
],
],
'default_value' => 100
])
->setInstructions('The less opacity the more of the background colour will show through.')
->addRadio('background_clamp', [
'choices' => [
'top' => 'Top',
'center' => 'Centre',
'bottom' => 'Bottom'
],
'conditional_logic' => [
[
[
'field' => 'background_image',
'operator' => '!=empty'
]
],
[
[
'field' => 'background_video',
'operator' => '!=empty'
]
],
]
])
->setInstructions('By default the background object will attempt to cover the entire area of the hero aligned to the center, You can clamp the object to the top, centre or bottom instead.')
->addTrueFalse('focus_on_video')
->setInstructions('This will remove the text and increase the size of the video area')
->addNumber('max_height', [
'append' => 'px'
])
->setInstructions('By default selecting "make background focus" will use a 16:9 aspect ratio. You can set a maximum height in pixels here to override this.')
->conditional('focus_on_video', '==', 1)
->addText('heading')
->setInstructions('If left blank page title will be used')
->conditional('focus_on_video', '!=', 1)
->addSelect('heading_colour', [
'required' => 1,
'conditional_logic' => [
[
[
'field' => 'heading',
'operator' => '!=empty'
]
]
],
'choices' => [
'white' => 'White',
'purple' => 'Purple',
'grape' => 'Grape',
'pink' => 'Pink',
'light-pink' => 'Light Pink',
'dark-blue' => 'Dark Blue',
'blue' => 'Blue',
'light-blue' => 'Light Blue',
'red' => 'Red',
'orange' => 'Orange',
],
])
->addText('sub-heading')
->conditional('focus_on_video', '!=', 1)
->addSelect('sub-heading_color', [
'conditional_logic' => [
[
[
'field' => 'sub-heading',
'operator' => '!=empty'
]
]
],
'choices' => [
'white' => 'White',
'purple' => 'Purple',
'grape' => 'Grape',
'pink' => 'Pink',
'light-pink' => 'Light Pink',
'dark-blue' => 'Dark Blue',
'blue' => 'Blue',
'light-blue' => 'Light Blue',
'red' => 'Red',
'orange' => 'Orange',
],
'required' => 1
])
->addTrueFalse('overlap_content', [
'conditional_logic' => [
[
[
'field' => 'focus_on_video',
'operator' => '!=',
'value' => '1',
],
],
]
])
->setInstructions('This will add extra space to the bottom of the hero and pull the page content up to overlap the hero')
->setLocation(
'options_page',
'==',
'acf-options'
)
;
return $heroBuilder->build();
}
public function getHomeFields()
{
$homeFieldsBuilder = new FieldsBuilder('home');
$homeFieldsBuilder->addRepeater('content_rows', [
'layout' => 'block'
])
->setRequired()
->addText('title')
->setRequired()
->addTextarea('content')
->setRequired()
->addPageLink('button', [
'post_type' => [
0 => 'page',
1 => 'case_study'
]
])
->addText('button_text')
->addImage('image')
->setRequired()
->addTrueFalse('image_on_right')
->endRepeater()
->addGroup('link_list', [
'layout' => 'block'
])
->setRequired()
->addText('title')
->setRequired(1)
->setDefaultValue('Our Services')
->addRepeater('links', [
'button_label' => 'Add Link'
])
->addText('label')
->setRequired()
->addPageLink('link_to', [
'post_type' => [
0 => 'page'
]
])
->setRequired()
->addImage('image')
->setRequired()
->endRepeater()
->addPageLink('button', [
'post_type' => [
0 => 'page'
]
])
->setRequired()
->addText('button_text')
->setRequired()
->setDefaultValue('See our work')
->endGroup()
->addGallery('selected_clients', [
'min' => 6,
'mime_types' => 'png,svg'
])
->setRequired()
->addGroup('team', [
'layout' => 'block'
])
->addTextArea('content')
->setRequired()
->setDefaultValue('Pudam, que ommod qui que moditisfacea, et fugia acerum facea conse eum doluptam.')
->addPageLink('button', [
'post_type' => [
0 => 'page'
]
])
->addText('button_text')
->setRequired()
->setDefaultValue('Meet the full team')
->addText('title')
->setRequired()
->setDefaultValue('Meet the Team')
->endGroup()
->setLocation(
'options_page',
'==',
'acf-options'
);
return $homeFieldsBuilder->build();
}
public function getPeopleFields()
{
$peopleFields = new FieldsBuilder('person_fields');
$peopleFields
->addText('person_role')
->addImage('person_image')
->addImage('person_hover_image')
->addEmail('person_email')
->setLocation(
'options_page',
'==',
'acf-options'
);
return $peopleFields->build();
}
public function getSocialMediaFields()
{
$socialMediaFields = new FieldsBuilder('social_media_accounts');
$socialMediaFields
->addGroup('social_media_accounts')
->setInstructions('Configure one account for each service')
->addGroup('facebook', [
'layout' => 'block'
])
->setInstructions('Configure your facebook account details')
->addText('identifier')
->setInstructions('Enter your identifier as show in your facebook page\'s URL')
->endGroup()
->addGroup('twitter', [
'layout' => 'block'
])
->setInstructions('Configure your twitter account details')
->addText('identifier')
->setInstructions('Enter your twitter handle (do no include the @symbol')
->endGroup()
->addGroup('instagram', [
'layout' => 'block'
])
->setInstructions('Configure your instagram account details')
->addText('identifier')
->setInstructions('Enter your instagram handle')
->endGroup()
->addGroup('linkedin', [
'layout' => 'block'
])
->setInstructions('Configure your LinkedIn account here')
->addText('identifier')
->setInstructions('Enter your linked in handle')
->endGroup()
->endGroup()
->setLocation(
'options_page',
'==',
'acf-options'
);
return $socialMediaFields->build();
}
public function getContactFields()
{
$contactFields = new FieldsBuilder('contact_fields');
$contactFields
->addGoogleMap('map_location')
->setRequired()
->setLocation(
'options_page',
'==',
'acf-options'
);
return $contactFields->build();
}
public function getMasonryGridFields()
{
$masonryGridFields = new FieldsBuilder('masonry_grid_fields');
$masonryGridFields
->addRepeater('grid_items', [
'layout' => 'block',
'collapsed' => 'title',
'min' => 1
])
->setRequired()
->addText('title')
->setRequired()
->addPostObject('link_to', [
'post_type' => [
0 => 'case_study'
]
])
->setRequired()
->addImage('main_image')
->addImage('hover_image')
->setInstructions('Image to reveal on hover of the grid block')
->addText('hover_text')
->endRepeater()
->setLocation(
'options_page',
'==',
'acf-options'
);
return $masonryGridFields->build();
}
public function getSquareGridFields()
{
$squareGridFields = new FieldsBuilder('square_grid_fields');
$squareGridFields
->addRepeater('grid_items', [
'layout' => 'block',
'collapsed' => 'title',
'min' => 1
])
->setRequired()
->addText('title')
->setRequired()
->addPageLink('link_to', [
'post_type' => [
0 => 'page'
]
])
->setRequired()
->addImage('main_image')
->addImage('hover_image')
->setInstructions('Image to reveal on hover of the grid block')
->addText('hover_text')
->endRepeater()
->setLocation(
'options_page',
'==',
'acf-options'
);
return $squareGridFields->build();
}
}
I think if you load that into an IDE (im using PHPStorm) you'll see the behaviour im talking about when you try to add fields after repeaters/ conditions in the function chains. Code still works when you run it, its purely an IDE thing.
I've added some other missing annotations to fix the broken chains in my IDE, I've tried to make a point of matching up the annotation with the corresponding function call endRepeater()
returns a Builder
for example instead of a RepeaterBuilder
or FieldBuilder
.
addRange was added to FieldsBuilder as part of 1.5.0 in 2017, however individual functions return instances of FieldBuilder, which doesn't include the function. IDE's display function undefined warnings as a result.
addRange does however work as intended, so this just adds the annotation so IDE's can provide functionality like autocompletion