Support

Account

Home Forums Add-ons Flexible Content Field Is it possible to load all layouts on default?

Solved

Is it possible to load all layouts on default?

  • Hi,

    I wonder if it’s possible to load all flexible content fields when a users creates a post?

    I use this plugin and extension for a grading system for a local school, there are on average 8 tabs with +/- 10 fields per tab for one post. A teacher has 30 childeren to grade, making him have to click 30*80 = allot of times to complete a report.

    All fields are necessary anyway, but they do want the option to remove one if needed. So basically what I’m asking is can I turn the method around? Open all the flexible fields on default and remove if wanted, instead of having to add all of them each time.

  • Hi @Asitis

    I’m not 100% sure, but potentialy you could use the acf/load_value filter to create all the dummy data.

    That way, ACF will then render out all the layouts.

    This is how I would do it:

    1. Read the docs for: http://www.advancedcustomfields.com/resources/filters/acfload_value/
    2. Add the filter to you flexible content field and just debug the $value parameter to see how the data is displayed (you may need to edit a post which already contains data)
    3. Next step is to modify the $value by adding in a new row of data (make not of the layout attribute for each row which defines the layout)
    4. Once you can add an extra blank row, update the function to add 1 of each row type, but only append to the $value if $value is empty (new post)

    Hope that helps

    Thanks
    E

  • Hey Elliot,

    Thanks for the reply, but I dont fully understand (yet). Adding dummy data like this is different from adding a default value to a field right? Because when I do that, I still have to manually add every item.

    I read the docs but they are very unclear to me. This is the code I added to see what would happen, and I got a whole bunch of errors.

    function my_acf_load_value( $value, $post_id, $field )
    {
        // run the_content filter on all textarea values
        $value = apply_filters('the_content',$value); 
     
        return $value;
    }
      
    // acf/load_value/type={$field_type} - filter for a value load based on it's field type
    add_filter('acf/load_value/type=checkbox', 'my_acf_load_value', 10, 3);
    

    This is the most of the errors I get

    preg_split() expects parameter 2 to be string, array given in on line : Invalid argument supplied for foreach() in on line : implode() []: Invalid arguments passed in on line : 
    
    Warning
    : implode() [
    function.implode
    ]: Invalid arguments passed in
    /home/rapport/domains/rapport.pro/public_html/wp-includes/formatting.php
    on line
    130
    

    Almost all fields are checkboxes or text-fields. I cant seem to understand the function, in the docs it declares $value as $value (??) and the $field is never used in the code?

    I’m kinda newbie to all this :$

  • Hi @Asitis

    Ok, it is clear that you don’t understand the docs. Firstly, can you please research WP filters to understand how they work.

    The code above shows that you are hooking into the checkbox field. Why did you choose to do this? Shouldn’t you hook into the ‘flexible_content’ field instead?

    Next, you have run ‘the_content’ filter on the $value array. Why have you done this? From what you have previously explained, running this filter has nothing to do with your needs.

    If this is too much for you, please hire a web developer to help you as I can’t provide free dev support. Only help point you in the right direction and answer specific questions

    Thanks
    E

  • Hey Elliot,

    I work with a developer (I’m a designer myself), he’s pretty skilled in WordPress filters and all that. Still we are having problems to apply the filter to the flexible content fields.

    We did get default values in non-flexible content fields, using this code:

    function my_acf_load_value( $value, $post_id, $field )
    {
        // run the_content filter on all textarea values
        $value = "1"; 
     
        return $value;
    }
    
    add_filter('acf/load_value', 'my_acf_load_value', 10, 3);
    

    The problem here is that the flexible content field isn’t mentioned anywhere. When we tried this, we had no luck

    add_filter('acf/load_value/type=flexible_content', 'my_acf_load_value', 10, 3);

    – I’m just gonna think out loud from here –
    My point is, we can get the filter to work, but how can we apply it to the flexible content so that they are expanded by default?

    (how) Do we apply the filter to the subfields?

    Our fields are set like this:
    – Flexible content field
    — Subfield
    — Checkbox ‘r1’
    — Checkbox ‘r2’
    — Subfield
    — Textfield ‘r1’
    — Textfield ‘r2’
    — Subfield
    etc. etc.

    I imagine it wouldn’t be wise to apply the filter to a fieldname such as ‘r1’ because they could be checkboxes or textfields. Thus I think we have to apply the filter to the field_type, mostly being textfields and checkboxes.

    Meaning I could use this:
    add_filter('acf/load_value/type=checkbox', 'my_acf_load_value', 10, 3);
    To add default values to checkboxes. But we need to know how to apply this filter not to checkboxes, but to checkboxes in flexible content fields (or all checkboxes for all I care) – as long as the fields are shown by default.

    Furthermore I wanted to thank you for a fantastic product. We use it allot and overall it’s going really smooth. I realize we’re asking some pretty specific stuff here. I just hope we can figure this out (for our client’s sake).

  • Hi @Asitis

    I would find the field_key and use that in the filter. You can find the field_key by editing your field group, clicking on the screen options tab, and toggle the option to show field keys.

    This will then allow to you run the filter on and only on that field.

    Next, remove the filter code and edit a post that contains the field.
    Enter all ther default data you wish to appear.
    Save the post.

    Now add back in your filter code and simple dump out the value like so:

    
    <?php 
    
    echo '<pre>';
    	print_r( $value );
    echo '</pre>';
    die; 
    
    ?>
    

    Now you can see how the $value needs to look.

    You can now copy this data, and use it in your filter to override the $value.

    Does that help?

  • Hello Elliot,

    We managed to output the value, only the output is serialized so we don’t know what the value must look like because the fields have sub_fields.

    And another thing, we don’t know what filter to use to place the input and expand al the fields. Can you help on that.

  • Hi @Asitis

    What do you mean by “the output is serialized”? Perhaps an example would help?

    This is exactly what you need to do:
    1. Read the docs for load_value
    2. Create a filter that will hook into only the flexible content via it’s key
    3. print out it’s $value to understand what it looks like when 1 of each layout has been created (explained in previous comment)
    4. Modify your filter to override the value (if it is empty) to that of which you dumped out previously.

    In short, what you are doing is telling ACF that your flexible content field has a value even when it doesnt. This ‘fake’ or ‘dummy’ value will need to be written by hand, but can be found by first creating the value you want, then using your filter to dump it out so you can see it.

    Thanks
    E

  • If all this is too complicated, perhaps you shoudl try the JS alternative.

    Instead of hooking into PHP and applying a fake value, you can look at the source code of the flexible content JS and see how a layout is added.

    You can then use this logic to just add 1 of each layout during the setup_fields action

  • Hey Elliot,

    We thought about the JS method, yeah. But we figured out another workaround. I created all the reports with all the layouts once, added them to user “Demo”, added a menu-item to that users’ post list and intergrated a Duplicate Post plugin. Now the teachers don’t have to make every report themselves, but that can copy a new draft from one of the standard posts I’ve put in.

    This works well for us. Shame we didn’t get it figured out anyhow, but thanks for your effort, time and fantastic work!!

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Is it possible to load all layouts on default?’ is closed to new replies.