Skip to content

Create a generic Field block to display and edit post meta data #1

@bobbingwide

Description

@bobbingwide

Requirements

  • A generic block to display the value of post meta field
  • Optionally to display the field's description
  • Optionally, to allow the value to be edited and saved

Proposed solution

  • Use an attribute which accesses post meta data
  • Dynamically alter the attribute's fields to refer to the chosen field
  • Use the REST API to determine which fields can be displayed
attributes: {
		seenBefore: {
			type: 'string',
			source: 'meta',
			meta: '_seen_before'
		},
	},

Note: I don't yet know how to dynamically alter an attribute's fields,
nor do I know how to obtain each field's properties from the REST results.

Note: Even though I purchased ACF Pro in 2019, I've never been an ACF user.
I'm going to develop the block with server side rendering using oik-fields functionality.
If this can be extended to work with other methods of defining post meta data then that's a bonus.

Background to the requirement / proposed solution

In oik-blocks, the Fields block ( oik-block/fields ) is a Server Side Rendered block that displays post meta data and virtual information about a post. See https://www.oik-plugins.com/block/fields-oik-block-fields/
When the display option is All, it displays the values of all of the Fields associated with the post, including Taxonomy terms and virtual fields.

In order to display an individual field I still need to use a shortcode:

  • [bw_fields _field_name] to display a field with a label
  • [bw_field _field_name] to display a field without a label

An example is the home-body.html post template part in SB.

Here's the progression of development of the Seen before field.

[bw_field _seen_before]
[bw_fields _seen_before]
<!-- wp:oiksb/sb-field-block /-->
<!-- wp:bigram/seen-before /-->

Example result displayed:

1108
Seen before: 1108
Seen before:1,107 times
Seen before:1,108 times

The _seen_before field is now delivered as the bigram/seen-before block.
And Yes! I can explain why the value is 1107 in one place and 1108 elsewhere.
It's to do with the order of execution; blocks are expanded before shortcodes.

  1. oiksb/sb-field-block - increments the post meta value
  2. bigram/seen-before - also increments the post meta value
  3. bw_field just displays the current value
  4. bw_fields just displays the current value

Also from home-body.html, copied from Fizzie's metadates.html this uses [bw_field] since we're displaying properties of the post, rather than post meta.

<p>Published: [bw_field post_date] | Last updated: [bw_field post_modified] [post-edit]</p>

The [post-edit] shortcode is another candidate for a separate block.

Using shortcodes still has some advantages.

  1. They can be typed inline in paragraph blocks
  2. They don't need additional block development

Dynamic blocks have pros and cons relative to shortcodes

  • If a dynamic block is not supported then nothing is displayed.
  • If a shortcode is not supported then the unexpanded shortcode is displayed.

Whether not it's a pro or a con depends on your viewpoint.

Given that the way forward is to use blocks, block templates or block patterns and to eventually eliminate meta boxes
it makes sense to attempt to develop a generic field block that can be used to display and edit post meta data.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions