Support

Account

Home Forums General Issues Add local field issue

Unread

Add local field issue

  • Hi. I’m trying to generate some fields with acf_add_local_field. Beforehand I build up an array to loop through and add each local field. The array in the first foreach is based on values set in another ACF options page. When I loop through the 2nd foreach with just the initial $fields array. It all works fine. When I add the items to the $fields array from the first foreach. It breaks and no fields are being generated. Although the print_r shows the new items are listed the same way as the first two. My questions is mainly. Why does it break and how can I fix this. (I know the code is less efficient as it should be).

    function my_acf_add_local_field_groups() {
    
        $fields = ['test', 'test2'];
        
        foreach(get_mockup_objects() as $key => $object){
            if($object['hasPlaceholder']) {
                $fields[] = $object['placeholder']['id'];
            }
        }
    
        foreach($fields as $field) {
            acf_add_local_field([
                    'label' => $field,
                    'name' => $field,
                    'type' => 'post_object',
                    'parent' => 'field_5eac34065e6c1',
                    'post_type' => 'project'
                ]);
        }
    }
    
    add_action('acf/init', 'my_acf_add_local_field_groups');
    
    function get_mockup_objects(){
        $homepageFields = get_fields(get_option('page_on_front', true));
        $mockups = get_fields('mockups');
        $selectedMockup = '';
    
        foreach($mockups['homepage'] as $mockup){
            if($homepageFields['hero']['mockupHomepage'] === $mockup['id']){
                $selectedMockup = $mockup['objects'];
            }
        }
    
        $temp = [];
    
        foreach($selectedMockup as $object){
            if($object['hasPlaceholder']){
                $temp[] = $object;
            }
        }
    
        return $temp;
    }

    This is the return of the get_mockup_objects() function:

    Array
    (
        [0] => Array
            (
                [coordinates] => Array
                    (
                        [top] => -2
                        [left] => 50
                    )
    
                [hasPlaceholder] => 1
                [placeholder] => Array
                    (
                        [size] => Array
                            (
                                [width] => 776
                                [height] => 417
                            )
    
                        [id] => Ac1kkPPwdg
                    )
    
            )
    
        [1] => Array
            (
                [coordinates] => Array
                    (
                        [top] => 349
                        [left] => 142
                    )
    
                [hasPlaceholder] => 1
                [placeholder] => Array
                    (
                        [size] => Array
                            (
                                [width] => 422
                                [height] => 265
                            )
    
                        [id] => TZeotr4IRH
                    )
    
            )
    
    )

    Anyone any idea’s? Thanks in advance!

Viewing 1 post (of 1 total)

The topic ‘Add local field issue’ is closed to new replies.