quasel
5/25/2016 - 7:17 PM

Adding virtual Field Grouping to Pods

Adding virtual Field Grouping to Pods

<?php

/*
 * Add virtual Field Grouping to Pods
 * This requires the PR from https://github.com/pods-framework/pods/pull/3548 which will hopefully be
 * available in Pods 2.6.6
 * James Golovich <james@gnuinter.net>
 */


add_filter( 'pods_admin_setup_edit_field_options', 'jmg_pods_admin_setup_edit_field_options', 10, 2 );
function jmg_pods_admin_setup_edit_field_options( $options, $pod ) {

	$options[ 'advanced' ][ 'Visual' ][ 'backend_group' ] = array(
		'name' => 'backend_group',
		'label' => __( 'Field Group on backend editor', 'pods' ),
		'help' => __( 'help', 'pods' ),
		'type' => 'text',
		'default' => 'More Fields'
	);

	return $options;
}

add_filter( 'pods_meta_groups_get', 'jmg_pods_meta_groups_get', 10, 3 );
function jmg_pods_meta_groups_get( $groups, $type, $name ) {
	$new_groups = array();
	foreach ( $groups as $group ) {
		foreach ( $group[ 'fields' ] as $field ) {
			if ( isset( $field[ 'options' ][ 'backend_group' ] ) ) {
				$group_name = $field[ 'options' ][ 'backend_group' ];
			} else {
				$group_name = 'More Fields';
			}
			$new_groups[ $group_name ][ 'fields' ][ $field[ 'name' ] ] = $field;
			// We don't really need to set the following every time, but it's easier than doing another loop afterwards
			$new_groups[ $group_name ][ 'pod' ] = $group[ 'pod' ];
			$new_groups[ $group_name ][ 'context' ] = $group[ 'context' ];
			$new_groups[ $group_name ][ 'priority' ] = $group[ 'priority' ];
			$new_groups[ $group_name ][ 'label' ] = $group_name;

		}
	}

	return $new_groups;
}