acf-composer icon indicating copy to clipboard operation
acf-composer copied to clipboard

How to create a bidirectional custom field?

Open tislars opened this issue 1 year ago • 8 comments

In the current version of ACF Pro you are able to create a custom field with a bidirectional relationship. Is this possible with acf-composer? I do not see any related information in this repository or the cheatsheet.

tislars avatar May 22 '24 12:05 tislars

I'm pretty sure all config options will be passed through to ACF, so it should 'just work' :) give it a try! + PR's always welcome if improvements needed in the docs

mike-sheppard avatar May 22 '24 12:05 mike-sheppard

This should help from the ACF Builder repo - https://github.com/StoutLogic/acf-builder/issues/121#issuecomment-1131587567

mike-sheppard avatar May 22 '24 12:05 mike-sheppard

Currently, the Bidirectional setting is only available for the Relationship, User, Taxonomy, and Post Object fields

Source: ACF docs

mike-sheppard avatar May 22 '24 12:05 mike-sheppard

Thanks for the quick response, looks promising. However, on my end it does not work. Here is my code:

<?php
...
class ProductLocation extends Field
{
    /**
     * The field group.
     */
    public function fields(): array
    {
        $productLocation = Builder::make('product_location');

        $productLocation
            ->setLocation('post_type', '==', 'product-location')
            ->addRelationship('location_product_groups', [
                'post_type'              => 'product-group',
                'instructions'           => '',
                'acfe_bidirectional' => [
                    'acfe_bidirectional_enabled' => true,
                    'acfe_bidirectional_related'   => [
                        'field_group_product_locations',
                    ],
                ],
                'filters' => [
                    0 => 'search',
                ],
            ]);


        return $productLocation->build();
    }
}
<?php
...
class ProductGroup extends Field
{
    /**
     * The field group.
     */
    public function fields(): array
    {
        $productGroup = Builder::make('product_group');

        $productGroup
            ->setLocation('post_type', '==', 'product-group')
            ->addRelationship('group_product_locations', [
                'post_type'              => 'product-location',
                'instructions'           => '',
                'acfe_bidirectional' => [
                    'acfe_bidirectional_enabled' => true,
                    'acfe_bidirectional_related'   => [
                        'field_location_product_groups',
                    ],
                ],
                'filters' => [
                    0 => 'search',
                ],
            ]);

        return $productGroup->build();
    }
}

As you can see I use the value in the array key acfe_bidirectional_related with the name I set in the other fields instance. I also tried the name of what the page source tells me in the dashboard. Over there the name is field_product_location_location_product_groups and field_product_group_group_product_locations. But when I try this it also does not work.

tislars avatar May 22 '24 13:05 tislars

Did you ever manage to figure this out? Can you try the latest version of ACF Composer?

Log1x avatar Jun 06 '24 21:06 Log1x

Sorry for the late response. I missed your reply.

Sadly, I did not figure out what is going wrong. To this day I still have some projects where we'd like to apply bi-directional fields, but it's not working. I am running the latest version of ACF Composer.

tislars avatar Nov 18 '24 14:11 tislars

@tislars 'acfe_bidirectional' is using ACF Extended, which is no longer needed for bidirectional relationship since it's already included in ACF natively. Try with this:

$productGroup
            ->setLocation('post_type', '==', 'product-group')
            ->addRelationship('group_product_locations', [
                'post_type'              => 'product-location',
                'instructions'           => '',
               'bidirectional' => true,
                    'bidirectional_target' => [
                      0 => 'related_field_name_here'
                    ],
                'filters' => [
                    0 => 'search',
                ],
            ]);

lacruzwebdev avatar Jan 14 '25 11:01 lacruzwebdev

I ran into the same issue, but managed to make it work. Here's how I set up the fields:

$fields = Builder::make('actor_fields');
$fields
    ->setLocation('post_type', '==', 'actor')
    ->addPostObject('videos', [
        'name' => 'videos',
        'post_type' => ['video'],
        'return_format' => 'id',
        'multiple' => true,
        'ui' => true,
        'bidirectional' => 1,
        'bidirectional_target' => ['field_video_fields_actors'],
        'allow_null' => false,
    ]);


$fields = Builder::make('video_fields');
$fields
    ->setLocation('post_type', '==', 'video')
    ->addPostObject('actors', [
        'name' => 'actors',
        'post_type' => ['actor'],
        'return_format' => 'id',
        'multiple' => true,
        'ui' => true,
        'bidirectional' => 1,
        'bidirectional_target' => ['field_actor_fields_videos'],
        'allow_null' => false,
    ]);

tcsaba92 avatar May 22 '25 07:05 tcsaba92