folbert / fewbricks

Write code to create ACF field groups, fields and re-usable modules.

Home Page:https://fewbricks2.folbert.com

Repository from Github https://github.comfolbert/fewbricksRepository from Github https://github.comfolbert/fewbricks

Conditional logic for brick?

zoephilipps opened this issue · comments

Doesn't seem like there's a way to pass settings other than fields when calling add_brick: https://github.com/folbert/fewbricks/blob/master/lib/brick.php#L216

Let me know if I'm missing something, otherwise I'll get to work on a PR.

If I understand correctly what you want to do, something like this is possible (writing this on my phone so I'll blame any errors on that):

$brick = new button('Button', 'button', 'uniqueId');
$brick->set_arg('name', 'value');
$brick->set_setting('name', 'value');
$this->add_brick($brick);

You can then fetch the arg-values with get_arg('name', [default_value]) and settings using the name of the variable like so: $this->setting_name.

Was that what you were looking for?

Ah, no, sorry for my lack of clarity... What I wanted was a way to set conditional logic for a brick.

Something like:

$brick->set_setting('conditional_logic', [
  // conditional logic here
]);

It would be super useful to bulk show/hide fields based on another single field's value!

I have tried a few different methods here... I was able to remove a field from a brick and change field names within a brick, but conditional logic is still giving me grief.

This is a flexible content field; I have tried this method on layout->get_setting( 'sub_fields' ) as well as field_group->get_setting( 'fields' ).

I have also tried the full field key as fewbricks debugging shows it (201705182211h_201705182211i_201706070848a_201706071006a), and the one that gets spit out in var_dump( $fields ) (201706071006a). Both have the result of hiding the fields, but they don't show up with 201706071006a being checked.

Here is the code:

# =============================================================================>
# conditional logic on brick
# =============================================================================>
$fields = $fewbricks_fg->get_setting( 'fields' );

// echo '<pre>';
// var_dump( $fields );
// echo '</pre>';
// wp_die();

foreach( $fields as $field_index => $field ) {
  if( $field['name'] === 'modules_flex' ) {
    // echo '<pre>';
    // var_dump($field);
    // echo '</pre>';
    foreach( $field['layouts'] as $layout_index => $layout ) {
      // echo '<pre>';
      // var_dump($layout);
      // echo '</pre>';
      if( $layout['name'] === 'cta_app_download_override' ) {
        $sub_fields = $layout['sub_fields'];
        // echo '<pre>';
        // var_dump($sub_fields);
        // echo '</pre>';
        foreach( $sub_fields as $sub_index => $sub_field ) {
          // echo '<pre>';
          // var_dump($sub_field);
          // echo '</pre>';
          if(
            $sub_field['name'] !== 'cta_app_download_override_defaults' &&
            $sub_field['type'] !== 'fewbricks_hidden'
          ) {
            $sub_fields[$sub_index]['name'] = str_replace( 'cta_app_download_override_', '', $field['name'] );
            $sub_fields[$sub_index]['conditional_logic'] = [
              [
                [
                  'field' => '201706071006a',
                  'operator' => '==',
                  'value' => '1'
                ]
              ]
            ];
          }
        }
        $fields[$field_index]['layouts'][$layout_index]['sub_fields'] = $sub_fields;
      }
    }
  }
}

$fewbricks_fg->set_setting( 'fields', $fields );

$fields = $fewbricks_fg->get_setting( 'fields' );

// echo '<pre>';
// var_dump( $fields );
// echo '</pre>';
// wp_die();

Any ideas?

Easiest solution is just to stick the brick under a tab and show/hide the tab using conditional logic. 🤷‍♂️

BUT if the brick were to have tabs of its own... no bueno.

Blast from the past! I wont be adding any non-essential new functionality to Fewbricks1 since Fewbricks2 is in the works. But I will take this feature in to consideration in Fewbricks2. Thanks for the idea.