Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_context('carbon_fields_after_title') causes blank add/edit post/page/cpt screen #851

Closed
dboune opened this issue Feb 9, 2020 · 6 comments · May be fixed by htmlburger/carbon-fields-docs#63

Comments

@dboune
Copy link

dboune commented Feb 9, 2020

Version

  • Carbon Fields: v3.1.13
  • WordPress: v5.3.2
  • PHP: 7.3.14

Expected Behavior

Add post/page/cpt screen functions as intended

Actual Behavior

Add post/page/cpt screen is blank, and errors are present in the browser console.

First error is:

metaboxes.min.js?ver=3.1.13:1 Could not find DOM element for container "carbon_fields_container_additional_fields".

Minimal Repro

This is a repro as a plugin. As written it requires Carbon Fields to be installed via Composer.

The critical line here is the call to set_context.

<?php
/**
 * Plugin Name:     Carbon Repro
 * Plugin URI:      PLUGIN SITE HERE
 * Description:     Repro for set_context issue
 * Author:          Damian Boune
 * Author URI:      YOUR SITE HERE
 * Text Domain:     carbon-repro
 * Domain Path:     /languages
 * Version:         0.1.0
 *
 * @package         Carbon_Repro
 */

// Your code starts here.

use Carbon_Fields\Container;
use Carbon_Fields\Field;

require plugin_dir_path(__FILE__) . '/vendor/autoload.php';

add_action( 'plugins_loaded', array( 'Carbon_Fields\\Carbon_Fields', 'boot' ) );

add_action( 'carbon_fields_register_fields', 'crb_attach_fields' );
function crb_attach_fields() {
    Container::make('post_meta', __( 'Additional Fields' ) )
        ->set_context('carbon_fields_after_title')  // Comment this line to resolve issue
        ->add_fields( array(
            Field::make( 'text', 'crb_text', 'Text Field' ),
        ) );
}

Steps to Reproduce the Problem

  1. Install WordPress, create plugin scaffold
  2. Install Carbon Fields into plugin directory using Composer
  3. Use above code for plugin
  4. Activate plugin
  5. Add or edit any post/page/cpt
  6. See blank screen, and view browser console

Comments

Error can be prevented by disabling Gutenberg, disabling the set_context call, or by using a context other than carbon_fields_after_title.

Related Issues

#631
Other values seem to work fine, so it appears that context is now meaningful again.

#735
This appears to be directly related, and results in same errors, but the conditions are not the same. Using where is not required to cause the issue, nor does it resolve it.

@lstoyanoff
Copy link
Contributor

Hi, I'm afraid that currently you can't change container position to be above the Gutenberg editor if that's your intention. carbon_fields_after_title position works only for the Classic Editor.

@dboune
Copy link
Author

dboune commented Feb 10, 2020

Unfortunately that was the intent. I wasn’t sure if the problem was here with Carbon Fields or with Gutenberg.

As opposed to simply not working it does fail a bit more harshly.

I’ll go ahead and close this out since it is not possible (yet I hope) to use that option in this way. It does work just fine with Gutenberg disabled.

I’ll also leave a suggestion of a doc mention since Gutenberg being enabled is the default now.

@dboune
Copy link
Author

dboune commented Feb 10, 2020

I’d been away from WP long enough that I’m coming back with Gutenberg as new to me, so grabbing onto these new concepts is a bit bumpy though more in line with what I’m used to these days on the browser side.

Looks like the way Gutenberg wants to do this is through “meta blocks”.

https://developer.wordpress.org/block-editor/tutorials/metabox/meta-block-3-add/

There seem to be some troubles here with hi consistency and non-optional fields.

Will keep looking at this in the morning.

@lstoyanoff
Copy link
Contributor

Hi, as far as I understand you need to create a Gutenberg Block not a Post Meta Container. For that purpose you can use the Carbon Fields Gutenberg Blocks instead -- https://docs.carbonfields.net/#/containers/gutenberg-blocks

@dboune
Copy link
Author

dboune commented Feb 10, 2020

Yes, that’s what I’ve gleaned thus far. Now to see if the functionality I need in this way is also supported by Gutenberg and by Carbon Fields.

At the root I feel weird about it being a block, as for this case it should not be reusable in any other way than as a required and always present meta field within a specific CPT.

It looks like there are ways to accomplish this at least in part.

I digress, that is a bit outside the scope of this ticket as I originally wrote it, but I’ll at least update again once I have a suitable base replacement for the broader scope for anyone who searches for it.

@dboune
Copy link
Author

dboune commented Feb 13, 2020

The solution is to use Gutenberg blocks, and possibly a block template, and possibly template lock. Relevant doc linked below. Unfortunately it is not possible to lock specific blocks using a template, however you can place one or more group blocks (core/group), or a custom block supporting innerBlocks, if you need to allow freeform sections among required blocks.

https://developer.wordpress.org/block-editor/developers/block-api/block-templates/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants