Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a bidirectional custom field? #247

Open
tislars opened this issue May 22, 2024 · 5 comments
Open

How to create a bidirectional custom field? #247

tislars opened this issue May 22, 2024 · 5 comments
Labels
question Further information is requested

Comments

@tislars
Copy link

tislars commented May 22, 2024

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.

@mike-sheppard
Copy link
Sponsor Contributor

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
Copy link
Sponsor Contributor

This should help from the ACF Builder repo - StoutLogic/acf-builder#121 (comment)

@mike-sheppard
Copy link
Sponsor Contributor

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

Source: ACF docs

@tislars
Copy link
Author

tislars commented May 22, 2024

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.

@Log1x
Copy link
Owner

Log1x commented Jun 6, 2024

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

@Log1x Log1x added the question Further information is requested label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants