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

New field group registration #89

Merged
merged 17 commits into from
Apr 25, 2018
Merged

New field group registration #89

merged 17 commits into from
Apr 25, 2018

Conversation

stian-overasen
Copy link
Member

@stian-overasen stian-overasen commented Apr 23, 2018

A simpler field group registration logic with new filters for overriding all field group parameters and a lot less code.

Highlights:

  • Register field groups directly into core, just like we do with modules.
  • New filter hogan/field_group/<name>/args for overriding all field group parameters.
  • New filter hogan/field_group/default/supported_post_types for adding Hogan support to custom post types without the hassle of writing the location parameter correctly. (This filter is for the default field group only, custom field groups must define the location parameter.)
  • Deprecated filters:
    • hogan/field_group/<name>/fields_before_flexible_content
    • hogan/field_group/<name>/fields_after_flexible_content
    • hogan/field_group/<name>/location
    • hogan/field_group/<name>/hide_on_screen
    • hogan/supported_post_types
  • Deprecated helper functions:
    • hogan_deregister_default_field_group()
    • hogan_register_field_group()
  • Removed core function get_field_group_default_location()

Test code / Examples:

Add supported for more post types to default field groups.

add_filter( 'hogan/field_group/default/supported_post_types', function( array $post_types ) : array {
  //$post_types[] = 'post'; // Add support for posts.
  return $post_types;
}, 10, 2 );

Disable default field group.

add_filter( 'hogan/field_group/default/enabled', '__return_false' );

Override default field group arguments.

add_filter( 'hogan/field_group/default/args', function( array $args ) : array {

  // Set new field group title.
  $args['title'] = 'Overridden default field group title…';

  // Set fixed modules, text only in this case.
  $args['modules'] = [ 'embed' ];

  // Add field before modules.
  $args['fields_before_flexible_content'] = [
    [
      'type'         => 'text',
      'key'          => 'field_before_modules',
      'name'         => 'before_time',
      'label'        => 'Field before modules…'
    ],
  ];

  // Add field after modules.
  $args['fields_after_flexible_content'] = [
    [
      'type'         => 'text',
      'key'          => 'field_after_modules',
      'name'         => 'aftermath',
      'label'        => 'Field after modules…',
      'placeholder'  => 'After-modules field placeholder',
    ],
  ];

  return $args;
}, 10, 1 );

Do a deprecated hide_on_screen filter for the default field group. Reinstate the format meta box.

add_filter( 'hogan/field_group/default/hide_on_screen', function( array $args ) : array {

  if ( false !== ( $key = array_search( 'format', $args ) ) ) {
    unset( $args[ $key ] );
  }

  return $args;
} );

Add new custom field group.

add_action( 'hogan/include_field_groups', function( \Dekode\Hogan\Core $core ) : void {

  $core->register_field_group( [
    'name' => 'test_group_123',
    'title' => 'Test Group 123',
    'modules' => [
      'text',
    ],
    'location' => [
      [
        [
          'param'  => 'post_type',
          'value' => 'page',
        ],
      ],
    ],
  ] );
}, 10, 1 );

README.md Outdated
'operator' => '==',
'value' => 'page',
[
'param' => 'post_type',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing 'operator' => '==',

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

'param' => 'post_type',
'operator' => '==',
'value' => $post_type,
'param' => 'post_type',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing 'operator' => '==',

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor

@pederan pederan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing operator args in location args. Except from that: works like a charm!

@stian-overasen stian-overasen changed the title New field group registration (Breaking changes!) New field group registration Apr 24, 2018
@stian-overasen stian-overasen merged commit 4a79da6 into master Apr 25, 2018
@stian-overasen stian-overasen deleted the iss87 branch April 25, 2018 05:25
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 this pull request may close these issues.

None yet

2 participants