Support

Account

Home Forums Backend Issues (wp-admin) Best way to manage large exported flexible content functions

Unread

Best way to manage large exported flexible content functions

  • We use ACF pro extensively to create content fields for data which is exposed in an API. We export these fields as code and end up with very large, hard to manage custom field files often with duplicate code.

    Usually these fields are built as flexible content blocks which will have rows such as ‘text’, ‘text and image’, ‘gallery’, ‘call to action’, ‘block quote’, and have rules based on page_template type.

    A lot of these custom fields need to be applied to some page_template types but not others but still needs to be nested within the parent flexible content array / function.

    For example: ‘text’, ‘text and image’ need to apply to all page_template types, ‘gallery’ needs to only apply to page_template types == ‘About us’, and ‘call to action’ needs to only apply to page_template types == ‘Contact us’ and ‘Home’.

    Because we want all of these nested within the one flexible content module, it means we are forced to create multiple definitions for each of our content blocks and end up with something similar to this:

    Pseudo code:

    flexible_content_all = ['text', 'text and image', 'gallery', 'call to action', 'block quote']
    flexible_content_home_&_contact = ['call to action']
    flexible_content_about_us = ['gallery']

    So in each template, we have to redefine each content module and when it comes to updating, it is very messy.

    Ideally each flexible content type (‘text’, ‘gallery’, etc) could be abstracted out into a function.

    i.e:

    function build_image_module(){
    	return 	
    					array (
    						'key' => '57c4eb774d57a',
    						'name' => 'image',
    						'label' => 'Image',
    						'display' => 'block',
    						'sub_fields' => array (
    							array (
    								'key' => 'field_57c4eba99286f',
    								'label' => 'Title',
    								'name' => 'title',
    								'type' => 'text',
    								'instructions' => '',
    								'required' => 0,
    								'conditional_logic' => 0,
    								'wrapper' => array (
    									'width' => '50',
    									'class' => '',
    									'id' => '',
    								),
    								'default_value' => '',
    								'placeholder' => '',
    								'prepend' => '',
    								'append' => '',
    								'maxlength' => '',
    							),
    							array (
    								'key' => 'field_57c4ebb192870',
    								'label' => 'Image',
    								'name' => 'image',
    								'type' => 'image',
    								'instructions' => '',
    								'required' => 1,
    								'conditional_logic' => 0,
    								'wrapper' => array (
    									'width' => '50',
    									'class' => '',
    									'id' => '',
    								),
    								'return_format' => 'array',
    								'preview_size' => 'thumbnail',
    								'library' => 'all',
    								'min_width' => '',
    								'min_height' => '',
    								'min_size' => '',
    								'max_width' => '',
    								'max_height' => '',
    								'max_size' => '',
    								'mime_types' => '',
    							),
    						),
    						'min' => '',
    						'max' => '',
    					);
    }

    But where this gets messy is if we want to us the same module in two seperate flexible content modules on the same page. We can’t redefine the same key on the same page and therefore, in the function, we would need to pass in all the keys we require. Not necessarily the best solution.

    Has anybody else come across this issue? How do you manage your flexible content fields?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.