Skip to content

Commit 5174bf1

Browse files
authored
Add bidirectional trait and support for some field types (#125)
1 parent 67564c6 commit 5174bf1

File tree

7 files changed

+131
-2
lines changed

7 files changed

+131
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
[Unreleased]
88

9+
### Added
10+
- Support for ACF native bidirectional relationships. See https://www.advancedcustomfields.com/resources/bidirectional-relationships/.
11+
912
## [1.40.0]
1013

1114
### Added

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ $pseudo->add_field( $some_field )
233233
->add_field( $another_field );
234234
```
235235

236+
#### Bidirectional relationships
237+
238+
Codifier supports bidirectional relationships for the field types ACF is supporting the feature (currently PostObject, Relationship, Taxonomy and User).
239+
240+
```php
241+
$field->set_bidirectional()
242+
->set_bidirectional_targets( [ 'field_name_or_key' ] );
243+
```
244+
236245
## Gutenberg
237246

238247
Codifier has a feature to register Gutenberg blocks using ACF's register block feature internally. It works in a very similar fashion than the basic field creation in Codifier as well.

src/Field/Common/Bidirectional.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* ACF Codifier bidirectional trait
4+
*/
5+
6+
namespace Geniem\ACF\Field\Common;
7+
8+
/**
9+
* Bidirectional trait
10+
*/
11+
trait Bidirectional {
12+
/**
13+
* Field bidirectional status.
14+
*
15+
* @var boolean
16+
*/
17+
protected $bidirectional = 0;
18+
19+
/**
20+
* Bidirectional targets.
21+
*
22+
* @var array
23+
*/
24+
protected $bidirectional_target = [];
25+
26+
/**
27+
* Set bidirectional.
28+
*
29+
* @return self
30+
*/
31+
public function set_bidirectional() {
32+
$this->bidirectional = 1;
33+
34+
return $this;
35+
}
36+
37+
/**
38+
* Set unidirectional (disable bidirectional).
39+
*
40+
* @return self
41+
*/
42+
public function set_unidirectional() {
43+
$this->bidirectional = 0;
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* Get bidirectional status.
50+
*
51+
* @return boolean
52+
*/
53+
public function get_bidirectional() {
54+
return $this->bidirectional;
55+
}
56+
57+
/**
58+
* Set bidirectional targets.
59+
*
60+
* @param array $targets Target field names or keys.
61+
* @return self
62+
*/
63+
public function set_bidirectional_targets( array $targets = [] ) {
64+
$this->bidirectional_target = $targets;
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Add bidirectional target.
71+
*
72+
* @param string $target Target field name or key.
73+
* @return self
74+
*/
75+
public function add_bidirectional_target( string $target ) {
76+
$this->bidirectional_target[] = $target;
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* Remove bidirectional target.
83+
*
84+
* @param string $target Target field name or key.
85+
* @return self
86+
*/
87+
public function remove_bidirectional_target( string $target ) {
88+
$key = array_search( $target, $this->bidirectional_target );
89+
90+
if ( $key !== false ) {
91+
unset( $this->bidirectional_target[ $key ] );
92+
}
93+
94+
return $this;
95+
}
96+
97+
/**
98+
* Get bidirectional targets.
99+
*
100+
* @return array
101+
*/
102+
public function get_bidirectional_targets() {
103+
return $this->bidirectional_target;
104+
}
105+
}

src/Field/PostObject.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55

66
namespace Geniem\ACF\Field;
77

8+
use Geniem\ACF\Field\Common\Bidirectional;
9+
810
/**
911
* Class PostObject
1012
*/
1113
class PostObject extends \Geniem\ACF\Field {
14+
15+
use Bidirectional;
16+
1217
/**
1318
* Field type
1419
*

src/Field/Relationship.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
namespace Geniem\ACF\Field;
77

8+
use Geniem\ACF\Field\Common\Bidirectional;
89
use Geniem\ACF\Field\Common\MinMax;
910

1011
/**
1112
* Class Relationship
1213
*/
1314
class Relationship extends \Geniem\ACF\Field {
1415

15-
use MinMax;
16+
use Bidirectional, MinMax;
1617

1718
/**
1819
* Field type

src/Field/Taxonomy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
namespace Geniem\ACF\Field;
77

8+
use Geniem\ACF\Field\Common\Bidirectional;
89
use Geniem\ACF\Field\Common\Disabled;
910

1011
/**
1112
* Class Taxonomy
1213
*/
1314
class Taxonomy extends \Geniem\ACF\Field {
1415

15-
use Disabled;
16+
use Bidirectional, Disabled;
1617

1718
/**
1819
* Field type

src/Field/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55

66
namespace Geniem\ACF\Field;
77

8+
use Geniem\ACF\Field\Common\Bidirectional;
9+
810
/**
911
* Class User
1012
*/
1113
class User extends \Geniem\ACF\Field {
14+
15+
use Bidirectional;
16+
1217
/**
1318
* Field type
1419
*

0 commit comments

Comments
 (0)