Skip to content

Commit

Permalink
feat: Add support for MetaBox relationships (#17)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Brent Vanwildemeersch <[email protected]>
Co-authored-by: Nicolas Lemoine <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2024
1 parent 28d187f commit 2e68d32
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,54 @@ Hellonico\Fixtures\Entity\Post:
# repeater field
features:
- label: <words(2, true)>
value: <sentence()
value: <sentence()>
- label: <words(2, true)>
value: <sentence()>
- label: <words(2, true)>
value: <sentence()>
```

Be careful with duplicate field keys, if you have multiple field with the same key, prefer using ACF field key (`field_948d1qj5mn4d3`).

### MetaBox Support

#### MetaBox Custom Fields
MetaBox fields can be adressed using the `meta` key
```yaml
Hellonico\Fixtures\Entity\Post:
post{1..30}:
post_title: <words(3, true)>
post_date: <dateTimeThisDecade()>
meta:
# number field
number: <numberBetween(10, 200)>
meta_box_custom_field: <sentence()>
```

#### MetaBox Relationships (https://docs.metabox.io/extensions/mb-relationships/#using-code)
When using the MB Relationships extension, the relationships can be set/defined using the key `mb_relations`. For each relationship you want to create a fixture for, you use the relationship-ID which is used to register the MB-relation
```php
MB_Relationships_API::register( [
'id' => 'post_to_term',
'from' => 'post',
'to' => [
'object_type' => 'term',
'taxonomy'=> 'custom_term'
],
] );
```
and the post/term ID of the object you want it to have a relationship with.
```yaml
Hellonico\Fixtures\Entity\Post:
post{1..30}:
post_title: <words(3, true)>
post_date: <dateTimeThisDecade()>
mb_relations:
post_to_term: '1x @custom_term*->term_id'
post_to_post: '1x @custom_post*->ID'
```


### Custom formatters

In addition to formatters provided by [fzaninotto/Faker](https://github.com/fzaninotto/Faker#formatters), you can use custom formatters below.
Expand Down
2 changes: 2 additions & 0 deletions examples/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Hellonico\Fixtures\Entity\Post:
# CUSTOM POST TYPE
product{1..15}:
post_type: product
mb_relations:
post_to_term: '1x @places*->term_id'
acf:
# number field
price: <numberBetween(10, 200)>
Expand Down
9 changes: 9 additions & 0 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Post extends Entity {
public $post_category;
public $meta_input;
public $acf;
public $mb_relations;
private $extra = [ 'meta_input', 'acf' ];

/**
Expand Down Expand Up @@ -126,6 +127,14 @@ public function persist() {
}
}

if ( defined( 'RWMB_VER' ) && class_exists( 'MB_Relationships_API' ) && ! empty( $this->mb_relations ) && is_array( $this->mb_relations ) ) {
foreach ( $this->mb_relations as $mb_rel_id => $object_ids ) {
foreach ( $object_ids as $id ) {
\MB_Relationships_API::add( $post_id, $id, $mb_rel_id );
}
}
}

return true;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Entity/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Term extends Entity {
public $description;
public $parent;
public $acf;
public $mb_relations;

/**
* Constructor.
Expand Down Expand Up @@ -98,6 +99,15 @@ public function persist() {
}
}

// Save MetaBox Relationships
if ( defined( 'RWMB_VER' ) && class_exists( 'MB_Relationships_API' ) && ! empty( $this->mb_relations ) && is_array( $this->mb_relations ) ) {
foreach ( $this->mb_relations as $mb_rel_id => $object_ids ) {
foreach ( $object_ids as $id ) {
\MB_Relationships_API::add( $post_id, $id, $mb_rel_id );
}
}
}

return true;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class User extends Entity {
public $use_ssl;
public $show_admin_bar_front;
public $acf;
public $mb_relations;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -88,6 +89,15 @@ public function persist() {
}
}

// Save MetaBox Relationships
if ( defined( 'RWMB_VER' ) && class_exists( 'MB_Relationships_API' ) && ! empty( $this->mb_relations ) && is_array( $this->mb_relations ) ) {
foreach ( $this->mb_relations as $mb_rel_id => $object_ids ) {
foreach ( $object_ids as $id ) {
\MB_Relationships_API::add( $post_id, $id, $mb_rel_id );
}
}
}

return true;
}

Expand Down

0 comments on commit 2e68d32

Please sign in to comment.