Skip to content

Commit

Permalink
Add Bidirectional Relationships convenience method (#161)
Browse files Browse the repository at this point in the history
* Fix Key::resolveParentKey traversal process

* add customFormat method to Field class

* add Bidirectional trait

* Apply Bidirectional trait to Relationship, PostObject, Taxonomy, and User fields

* add Bidirectional documentation to README

* remove remnant of previous open PR

* fix: order traits

* chore: update bidirectional comment

* chore: sort imports

* docs: updated bidirectional

* docs: bidirectional

* docs: update order

---------

Co-authored-by: Vincent Klaiber <[email protected]>
  • Loading branch information
kaelansmith and vinkla authored Feb 22, 2025
1 parent fd50308 commit a910468
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Extended ACF provides an object-oriented API to register groups and fields with
- [Layout](#layout)
- [Location](#location)
- [Conditional Logic](#conditional-logic)
- [Bidirectional Relationships](#bidirectional-relationships)
- [Non-standards](#non-standards)
- [`helperText`](#helperText)
- [`column`](#column)
Expand Down Expand Up @@ -680,6 +681,31 @@ Text::make('Sub Title')
]),
```

## Bidirectional Relationships

The `bidirectional` method establishes a bidirectional relationship between two or more fields. Each field involved in this relationship must use the `key` method to set a custom key. Then, the keys of these related fields should be passed to the `bidirectional` method.

```php
use Extended\ACF\Fields\Relationship;

// This field is attached to a "Project" post type.
Relationship::make('Related Testimonial')
->postTypes(['testimonial'])
->key('field_related_testimonial')
->bidirectional('field_related_project'),

// This field is attached to a "Testimonial" post type.
Relationship::make('Related Project')
->postTypes(['project'])
->key('field_related_project')
->bidirectional('field_related_testimonial'),
```

To learn more about ACF bidirectional relationships and their caveats, please consult the [official ACF documentation](https://www.advancedcustomfields.com/resources/bidirectional-relationships/).

> [!IMPORTANT]
We [usually recommend avoiding](#key) the use of custom field keys. This is an exception to that rule. When using bidirectional relationships, you must set custom field keys.

## Non-standards

### `helperText`
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Extended\ACF\Fields;

use Extended\ACF\Fields\Settings\Bidirectional;
use Extended\ACF\Fields\Settings\ConditionalLogic;
use Extended\ACF\Fields\Settings\FilterBy;
use Extended\ACF\Fields\Settings\HelperText;
Expand All @@ -25,6 +26,7 @@
class PostObject extends Field
{
use ConditionalLogic;
use Bidirectional;
use FilterBy;
use HelperText;
use Multiple;
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Extended\ACF\Fields;

use Extended\ACF\Fields\Settings\Bidirectional;
use Extended\ACF\Fields\Settings\ConditionalLogic;
use Extended\ACF\Fields\Settings\FilterBy;
use Extended\ACF\Fields\Settings\HelperText;
Expand All @@ -23,6 +24,7 @@
class Relationship extends Field
{
use ConditionalLogic;
use Bidirectional;
use FilterBy;
use HelperText;
use Required;
Expand Down
30 changes: 30 additions & 0 deletions src/Fields/Settings/Bidirectional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Copyright (c) Vincent Klaiber
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see https://github.com/vinkla/extended-acf
*/

declare(strict_types=1);

namespace Extended\ACF\Fields\Settings;

trait Bidirectional
{
/**
* Create a bidirectional relationship between two or more fields. This requires setting a custom key for each
* related field using the `key` method and passing the keys of those related fields to this method.
* @see https://www.advancedcustomfields.com/resources/bidirectional-relationships/
*/
public function bidirectional(array|string $keys): static
{
$this->settings['bidirectional'] = 1;
$this->settings['bidirectional_target'] = is_array($keys) ? $keys : [$keys];

return $this;
}
}
2 changes: 2 additions & 0 deletions src/Fields/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Extended\ACF\Fields;

use Extended\ACF\Fields\Settings\Bidirectional;
use Extended\ACF\Fields\Settings\ConditionalLogic;
use Extended\ACF\Fields\Settings\HelperText;
use Extended\ACF\Fields\Settings\Nullable;
Expand All @@ -23,6 +24,7 @@
class Taxonomy extends Field
{
use ConditionalLogic;
use Bidirectional;
use HelperText;
use Nullable;
use Required;
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Extended\ACF\Fields;

use Extended\ACF\Fields\Settings\Bidirectional;
use Extended\ACF\Fields\Settings\ConditionalLogic;
use Extended\ACF\Fields\Settings\HelperText;
use Extended\ACF\Fields\Settings\Multiple;
Expand All @@ -24,6 +25,7 @@
class User extends Field
{
use ConditionalLogic;
use Bidirectional;
use HelperText;
use Multiple;
use Nullable;
Expand Down

0 comments on commit a910468

Please sign in to comment.