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

Reuse fields #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/10-containers/10-conditional-display.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Container::make( 'post_meta', 'Custom Data' )
->add_fields( array( ... ) );
```

**Important note:** With [Theme options containers](/docs/containers-theme-options/) only users which have ``manage_options`` capability have access to the container by default. Therefore ``current_user_role`` / ``current_user_capability`` conditions would not work unless [``carbon_fields_theme_options_container_admin_only_access`` filter](/docs/advanced-topics-hooks)is overridden.

##### Nested logic

In order to achieve nested display logic the `where()` and `or_where()` methods also support being invoked with a `callable`:
Expand Down
43 changes: 43 additions & 0 deletions documentation/40-advanced-topics/70-reuse-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Reuse Fields

Fields could be reused via functional programming as well as in class declaration.

```php
function getLinkGroup($name = 'link') {
return Field::make( 'complex', $name )
->add_fields( array(
Field::make( 'text', 'linktext'),
Field::make( 'text', 'url'),
Field::make( 'checkbox', 'newtab' ) )
)
);
}

function getImageGroup( $name = 'image' ) {
return Field::make( 'complex', 'image' )
->add_fields( array(
Field::make( 'image', 'mobile' ),
Field::make( 'image', 'tablet' ),
Field::make( 'image', 'desktop' ) )
)
}

function getWysiwyg( $name = 'content' ) {
return Field::make( 'rich_text', $name )
}

Container::make( 'post_meta', 'Author Info' )
->where( 'post_type', '=', 'post' )
->add_fields( array(
getLinkGroup(),
getImageGroup( 'avatar' ),
getWysiwyg() )
);

Container::make( 'post_meta', 'Call To Action Button' )
->where( 'post_type', '=', 'page' )
->add_fields( array(
getLinkGroup() )
);
```