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

Displaying fields conditionally #16

Closed
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
1 change: 1 addition & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Limited format support is available:
* Date fields can have their format specified with the same format as [CiviCRM's date display](https://docs.civicrm.org/user/en/latest/initial-set-up/dates/), e.g. `[api4:start_date:%B %E, %Y]`
* File upload fields can be output as images with width, height, and alt text specified, e.g. `[api4:My_Custom_Field_Group.Image_Upload:img:800x300:alt=A picture]`
* A line break tag can be output with fields only when they contain data with `:br`, e.g. `[api4:My_Custom_Field_Group.Optional_Field:br]`
* Conditional display of fields has some support, using `[api4:display_name|sort_name]` syntax, which will return the `display_name` if defined, then the `sort_name` if defined. Formatting options cannot be applied conditionally, so this should only be used for the same content type (i.e. don't mix text/images in results, as this may apply invalid formatting options).

### CiviCRM API trouble-shooting

Expand Down
24 changes: 18 additions & 6 deletions shortcodes/civicrm/api4-get.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function shortcode_callback( $atts = [], $content = NULL, $tag = '' ) {
$output_regex = '/ (?: ( \[ ) | ( {{ ) ) api4: (?<field> [^][[:space:]:{}]+ (?::(?:label|value|name|id))?) (?: : (?<format> [^][{}]+ ) )? (?(1) \] | }} ) /sx';

if ( preg_match_all( $output_regex, $content, $match ) ) {
$params['select'] = array_values( $match['field'] );
$params['select'] = array();
foreach ( $match['field'] as $field ) {
$params['select'] = array_merge($params['select'], explode( '|', $field));
}
}

$params = apply_filters( $this->get_shortcode_name() . '/params', $params, $atts );
Expand Down Expand Up @@ -136,11 +139,20 @@ public function shortcode_callback( $atts = [], $content = NULL, $tag = '' ) {

foreach ( $results as $result ) {
$output = preg_replace_callback( $output_regex, function ( $match ) use ( $result, $fields ) {
$output = $result[ $match['field'] ] ?? '';

if ( ! $output ) {
return '';
}

$field_array = explode( '|', $match['field']);

while ( ! $output ) {
if ( 1 > count( $field_array ) ) {
return '';
}
$current = array_shift( $field_array );
if ( $result[$current] ) {
$output = $result[$current];
}
}

$match['field'] = $current;

$field = $fields[ $match['field'] ] ?? [];

Expand Down