Support

Account

Home Forums Backend Issues (wp-admin) acf_add_local_field_group – Automated select field based off blocks

Unread

acf_add_local_field_group – Automated select field based off blocks

  • Hello,

    I have created a set of fields and one of those fields is a select which should auto populate with a list of blocks on the page. The issue i am running into is that it seems that the field is generated before i can get the post ID please see code below

    
    public function tabbed_navigation_fields() {
    		$block_ids = array();
    
    		if ( ! is_admin() ) {
    			global $post;
    			$post_id = $post->ID;
    		} else {
    			if ( isset( $_GET['post'] ) ) {
    				$post_id = $_GET['post'];
    			} elseif ( isset( $_POST['post_ID'] ) ) {
    				$post_id = $_POST['post_ID'];
    			}
    		}
    
    		if ( isset( $post_id ) ) { //phpcs:ignore
    			$current_post = get_post( $post_id ); //phpcs:ignore
    			if ( has_blocks( $current_post->post_content ) ) {
    				$blocks = parse_blocks( $current_post->post_content );
    				foreach ( $blocks as $key => $block ) {
    					if ( ( $block['blockName'] && 'acf/tabbed-navigation' !== $block['blockName'] ) && ( $block['attrs']['id'] ) ) {
    						$name                               = str_replace( 'acf/', '', $block['blockName'] );
    						$name                               = str_replace( '-', ' ', $name );
    						$block_ids[ $block['attrs']['id'] ] = ucfirst( $name );
    					}
    				}
    			}
    		} else {
    			$block_ids['no_id'] = 'no post id ' . $post_id;
    		}
    
    		$tabbed_navigation_fields = new \StoutLogic\AcfBuilder\FieldsBuilder(
    			'tabbed_navigation_fields',
    			array(
    				'title' => 'Tabbed Navigation',
    			)
    		);
    		$tabbed_navigation_fields->addRepeater(
    			'tabbed_nav_links',
    			array(
    				'label'        => 'Links',
    				'button_label' => 'Add Link',
    			)
    		)
    		->addSelect(
    			'link_block',
    			array(
    				'label'         => 'Block',
    				'instructions'  => 'Select block to link to.',
    				'choices'       => $block_ids,
    				'allow_null'    => 0,
    				'return_format' => 'key',
    				'wrapper'       => array(
    					'width' => '50',
    				),
    			)
    		)
    		->addText(
    			'link_text',
    			array(
    				'label'    => 'Link Text',
    				'required' => false,
    			)
    		);
    
    		$tabbed_navigation_fields->setLocation( 'block', '==', 'acf/tabbed-navigation' );
    		add_action(
    			'acf/init',
    			function() use ( $tabbed_navigation_fields ) {
    				acf_add_local_field_group( $tabbed_navigation_fields->build() );
    			}
    		);
    	}
    

    So my select box always returns “no id”, but if i var_dump my array the array is complete but this seems to be running after the field is made.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.