Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Change User control to store an object, like the Post control #294

Merged
merged 5 commits into from
Apr 27, 2019

Conversation

kienstra
Copy link
Collaborator

Question About Storing As An Object

Hi @lukecarbis,
Making this backwards-compatible was harder than I thought, though I might think of a better way.

It'll require clicking "Update" for each block (in the screenshot below), and the previous values in the block editor won't persist.

Is this PR important? As it is now, the User control stores the user slug, which is immutable.

The only improvement in this PR is a better UI, as it shows a "nicename" in the block editor like John Doe, instead of the slug, like jdoe.

It's an improvement, but wondering if you think it's worth a backwards-compatibility break.

Instead of storing the value as the string user slug, This stores the value as:

{ 
        id: 24,
        userName: 'John Doe',
}

The main difference is the display of the userName in the block editor. Before, it was the user slug, which could be like jdoe.

But this now displays the username, which could be like John Doe. It's what the user selected as "Display name publicly as" in /wp-admin > Users > Your Profile.

There's no change here in the front-end display of the template. block_field() still echoes the username like John Doe, and block_value() still returns the WP_User object.

Backwards-Compatibility Issues

Right now, it also requires clicking "Update" in the Block Lab UI:

clicking-here

This is because the logic saves the block type as object in that UI. This PR might be able to hack that by parsing $this->blocks and changing the saved value to object.

Also there's no backward compatibility for the block in the block editor. It's possible to filter the $attributes for the render_callback, but that filter doesn't have access to the field type.

So it won't know that it's a User control, in order to change the value from a string to an object.

Thanks, Luke!

Closes #288

This will break backwards-compatibility.
Maybe in the meantime I'll think about a way
to avoid this break,
but so far I can't see a way to avoid it.
If it's a string, ignore it and replace it with
an empty object.
The Travis builds failed,
so change this to account for the new object value.
The value used to be a string,
and now it's an object.
So assert the proper output for those cases.
@lukecarbis
Copy link
Member

My view is that for Pro features like this one, as long as we’re in “Early Access”, introducing breaking changes is reasonable.

Personally, I think that keeping the return values for these types of controls consistent will create a simpler and better experience for developers.

I really want to stress though that this is just my opinion. Ultimately, as tech lead, it’s completely up to your best judgement.

@kienstra
Copy link
Collaborator Author

Hi @lukecarbis,

My view is that for Pro features like this one, as long as we’re in “Early Access”, introducing breaking changes is reasonable.

Good point, this PR is an improvement. The control is more intuitive, with the "nicename" (like John Doe) instead of the user slug (like jdoe).

I agree, we can break backwards-compatibility given that it's for "Early Access" users.

…e user ID

Before, this stored the user slug,
so that was meaningful.
@kienstra
Copy link
Collaborator Author

Request For Review

Hi @lukecarbis,
Could you please review this when you have a chance?

The main change for the user will be that the block's <input> displays a "nicename," not simply the user slug.

Before

before-only-slug

After

nicename-displays

And the block saves the value as an object, not a string:

object-here

But there's no difference in the return or echo values of block_field() or block_value().

However, there's a complete backwards-compatibility break. Users will have to select a new value from the block <input>.

@kienstra kienstra changed the title [WIP] Change User control to store an object, like the Post control Change User control to store an object, like the Post control Apr 26, 2019
Copy link
Member

@lukecarbis lukecarbis left a comment

Choose a reason for hiding this comment

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

Tested both block_field and block_value. Everything looks great. For some reason I thought we used to return the username, not the full user object, but I prefer the full user object as it is in this PR. Ready to merge.

@kienstra
Copy link
Collaborator Author

@lukecarbis, thanks for reviewing this!

@kienstra kienstra merged commit e83ddc2 into develop Apr 27, 2019
@kienstra kienstra deleted the update/user-control-values branch April 27, 2019 23:53
lukecarbis pushed a commit to lukecarbis/block-lab that referenced this pull request Aug 18, 2019
…ol-values

Change User control to store an object, like the Post control
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User input should store ID as well as username.
2 participants