htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.38k stars 245 forks source link

PHP error on API fetch of association field value for nav_menu_item #732

Open dfwood opened 5 years ago

dfwood commented 5 years ago

Version

Expected Behavior

Expect that the api would return the current value for the field.

Actual Behavior

The API request errors out due to Carbon_Fields\REST_API\Router\get_association_data() getting a value of null from Carbon_Fields\Helper\Helper\get_field().

Fatal error: Uncaught Error: Call to a member function get_thumbnail_by_type() on null in htmlburger/carbon-fields/core/REST_API/Router.php on line 323

Container definition

<?php

use Carbon_Fields\Container;
use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', function () {
    Container::make( 'nav_menu_item', __( 'Menu Settings', 'verato' ) )
             ->add_fields( [
                 Field::make( 'association', 'mega_menu_featured_item', __( 'Featured Resource/Event', 'verato' ) )
                      ->set_types( [
                          [
                              'type'      => 'post',
                              'post_type' => 'post',
                          ],
                          [
                              'type'      => 'post',
                              'post_type' => 'page',
                          ],
                      ] )
                      ->set_max( 1 )
                      ->set_help_text( 'Only applicable for top level menu items. Appears as part of the sub-menu. Will not appear if menu item has no children.' ),
             ] );
} );

Steps to Reproduce the Problem

  1. Create a new menu and add items.
  2. Select a post in the association field and save.
  3. Reloading the page will show no value for the field. Re-saving will clear the saved value.

Comments

The issue seems to be that when it is looking for the field, it sees the container ID as carbon_fields_container__menu-item-112_carbon_fields_container_menu_settings (or similar), instead of the actual container id of carbon_fields_container_menu_settings.

rajeev-tx commented 3 years ago

I also face the same issue in latest Carbon Fields : 3.3.2