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

EZP-29290: As an Editor, I want to have preconfigured "Author" section while creating the content #2392

Merged

Conversation

konradoboza
Copy link
Member

@konradoboza konradoboza commented Jul 11, 2018

Question Answer
JIRA issue EZP-29290
Bug/Improvement no
New feature yes
Target version master
BC breaks no
Tests pass yes
Doc needed yes

JIRA excerpt:

Currently, in v2 there is no preconfigured author section (fields filled in with the name and e-mail of the currently logged user). It would be more convenient for the editors to have this fields set automatically while creating a new content. Also, prefilling section with the default data should be configurable at the Author FieldType level.

This is the kernel implementation of this feature. It is followed by the changes in repository-forms and admin-ui-bundle which will enable a user to choose whether Author FieldType should be prefilled by default from the ContentType creation view.

Is required by:

TODO:

  • Implement feature / fix a bug.
  • Implement tests.
  • Fix new code according to Coding Standards ($ composer fix-cs).
  • Ask for Code Review.

@gggeek
Copy link
Contributor

gggeek commented Jul 11, 2018

I'd say default for the fieldType should be Empty, not current-user.

In fact in more than 10 years using eZ, I have never seen anyone using an author field to store the current user. That info is embedded into content metadata anyway. Whenever an author field is used, it is because the person logged in to input the data is not the author that has to be shown as content creator

@konradoboza
Copy link
Member Author

@gggeek thank you for the input. Default option could be easily changed, that's not a problem at all. Prefilling author FieldType with current user's data was present in eZ Publish and eZ Platform v1 so as a matter of consistency we would like to have it also in v2. We also aim to give the Editor possibility to keep current behavior as well.

konradoboza added 2 commits July 12, 2018 10:22
…dtype' into EZP-29290-prefilling_author_fieldtype

# Conflicts:
#	eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/AuthorConverter.php
#	eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldValue/Converter/AuthorTest.php
#	eZ/Publish/SPI/Tests/FieldType/AuthorIntegrationTest.php
@ezsystems ezsystems deleted a comment from ezrobot Jul 12, 2018
@konradoboza
Copy link
Member Author

konradoboza commented Jul 12, 2018

Few things I'm not sure about:

Any advice would be much appreciated :) @andrerom @alongosz

@andrerom
Copy link
Contributor

is injecting Repository into AuthorConverter the best way to solve it? Tests got more complicated, maybe I'm missing something.

That is probably not the way to do it, as Converter is performed down in Storage Engine and should now know about repository, besides circular references.

As for how and the other question maybe @alongosz and @bdunogier can give you some input.

@konradoboza konradoboza changed the title [WiP] EZP-29290: As an Editor, I want to have preconfigured "Author" section while creating the content EZP-29290: As an Editor, I want to have preconfigured "Author" section while creating the content Aug 2, 2018
@konradoboza
Copy link
Member Author

konradoboza commented Aug 2, 2018

I changed approach towards implementing this feature in repository-forms. Followup PR's need to wait for ezsystems/repository-forms#249 as it requires further changes.
In the meantime, please review @andrerom @alongosz.

/**
* Default value empty.
*/
const DEFAULT_EMPTY = -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why you picked -1 over for instance 0? Other cases you found like that?
Asking as I can't right now remember us using negative ints much for int fields.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was having hard times to differentiate this value from this which I got from the database for existing ContentTypes (e.g. Article), as we wanted to have prefilling by default there.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, so you're not using 0 as todays default (Null) for data_int1. is casted to int in Storage engine so it becomes 0, which is causing your trouble to decide on value. Right?

Is 0 mapped to DEFAULT_CURRENT_USER somewhere for us btw, so editing existing data works as intended?

@alongosz SIDE: you see any issues with using negative int's for data_int1 column? Or should we rather keep the values in the positive range?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, so you're not using 0 as todays default (Null) for data_int1. is casted to int in Storage engine so it becomes 0, which is causing your trouble to decide on value. Right?

Yes, that was my point.

Is 0 mapped to DEFAULT_CURRENT_USER somewhere for us btw, so editing existing data works as intended?

So you are suggesting switching values? (0, both for prefilling chosen by editor and also for no value from db, 1 for no-prefilling option). It could be confusing, but probably would work.

Copy link
Contributor

Choose a reason for hiding this comment

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

So you are suggesting switching values?

not necessarily, before even suggesting that I was asking how the system behaves when db has null value. Both when it comes to content type view/edit and for content edit.

context: People upgrading would expect default value to just be current user somehow, so if code here handles that or if we are missing upgrade script where those upgrading can choose between setting these to 1 for user or -1/2 for todays null value.

Copy link
Member Author

Choose a reason for hiding this comment

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

@andrerom I tested adding/editing content/content type with/without prefilling, everything seems to be working fine.

Copy link
Member

Choose a reason for hiding this comment

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

@alongosz SIDE: you see any issues with using negative int's for data_int1 column? Or should we rather keep the values in the positive range?

No, this is fine. data_int1 is NOT NULL anyway, so it has to be something that is non-null and non-zero.

@konradoboza what I don't get is how these constants apply to this solution? What is their purpose?

The naming is very generic and the PhpDoc description says the same thing, which is the perfect example of how not to document code :trollface:

I can guess based on the other parts of the PR, but if I need to guess it, then it implies it needs an improvement :-)
I prefer you find some better naming so code is either self-commenting or requires only brief explanation. If you can't find better name, then at least provide more descriptive doc block.

... or maybe, since this is a default value, it should belong to the Value class, not type? Even despite the fact that you're using it in the Type class.

context: People upgrading would expect default value to just be current user somehow,

@andrerom Actually... No. Why? This is BC break.

I know PMs really want this by default, so AdminUI can set it differently.

From my POV both cases would be BC break, but we should at least make API BC.

cc @SylvainGuittard

@andrerom
Copy link
Contributor

andrerom commented Aug 3, 2018

@konradoboza I'm wondering if this phase 2 on kernel and repo forms should rather go to master as it's a new feature and not requested by customer so we don't need to rush it. Would that work for you?

@konradoboza
Copy link
Member Author

konradoboza commented Aug 3, 2018

@andrerom sure, it is fine with me. So this one should be rebased to master, how about the other two?

@andrerom
Copy link
Contributor

andrerom commented Aug 3, 2018

@konradoboza so which one depends on which? Which PR's are "phase 1", aka minimal scope, and which once are followups to add config in field type?

It's the followups I'm suggesting we take to master basically, leaving phase 1 for the shortly upcoming 2.2.2 release.

@konradoboza
Copy link
Member Author

@andrerom all three PR's should be treated as one feature, so this is "phase 2", I updated descriptions for ezsystems/repository-forms#251 and ezsystems/ezplatform-admin-ui#586 as they depend on kernel.

"Phase 1" was already merged and was about having prefilled Author FieldType, without having a config. Ref: ezsystems/repository-forms#249

@andrerom
Copy link
Contributor

andrerom commented Aug 3, 2018

Ok, clear. Then go for master on the remaining PR's, should not have any conflicts so hopefully simple rebase ;)

@andrerom
Copy link
Contributor

andrerom commented Aug 3, 2018

Actually, we can try to just change target on PR's, if it does not conflict we are good! Done

@andrerom andrerom changed the base branch from 7.2 to master August 3, 2018 08:48
@konradoboza konradoboza force-pushed the EZP-29290-prefilling_author_fieldtype branch 2 times, most recently from db992b2 to 9a91b9c Compare August 3, 2018 13:21
@konradoboza
Copy link
Member Author

Fixed checking if any value came from the database in order to have a default selected radio in the Content Type edit view for existing ones. 9a91b9c#diff-564956cdbfd9b8b86cfb41dc3ddbf243R79

return array_map(
function ($constantName) {
return array(
array('defaultAuthor' => $constantName),
Copy link
Contributor

Choose a reason for hiding this comment

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

[]

Aka use [] in new code and lines that are changed, and leave array in places you don't change to unnecessary branch conflicts.

@@ -98,17 +109,17 @@ public function getFieldDefinitionData()
public function getInitialValue()
{
return new Content\FieldValue(
array(
'data' => array(
array(
Copy link
Contributor

Choose a reason for hiding this comment

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

can skip these changes

@@ -123,20 +131,20 @@ private function generateXmlString(array $authorValue)
*
* @param string $xmlString XML String stored in storage engine
*
* @return \eZ\Publish\Core\FieldType\Author\Value
* @return array
Copy link
Contributor

Choose a reason for hiding this comment

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

\eZ\Publish\Core\FieldType\Author\Value[]

*/
private function restoreValueFromXmlString($xmlString)
{
$dom = new DOMDocument('1.0', 'utf-8');
$authors = array();
$authors = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

can skip these changes

@konradoboza konradoboza force-pushed the EZP-29290-prefilling_author_fieldtype branch from 9a91b9c to 87481f2 Compare August 6, 2018 09:30
@konradoboza konradoboza force-pushed the EZP-29290-prefilling_author_fieldtype branch from 87481f2 to 836060c Compare August 6, 2018 09:33
@konradoboza
Copy link
Member Author

I reverted all the unnecessary changes to array short notation. When there were [] and array() in one file I changed these to array() to be consistent with the previous versions and with the other tests.

@@ -192,7 +198,7 @@ public function provideInvalidCreationFieldData()
/**
* Get update field externals data.
*
* @return array
* @return AuthorValue
Copy link
Member

Choose a reason for hiding this comment

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

Please use FQCN

/**
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
* @param mixed $fieldSettings
Copy link
Member

Choose a reason for hiding this comment

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

this is always array, right? Although we can't change interface prototype, I think here we can safely say what we expect.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should it be changed for every Type class then? As I see mixed is used in all FieldTypes which have settings.

Copy link
Member

Choose a reason for hiding this comment

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

No, let's not get out of scope here. It is indeed inconsistent but let's not duplicate past mistakes.

}

switch ($name) {
case 'defaultAuthor':
Copy link
Member

Choose a reason for hiding this comment

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

This looks really weird. Probably taken from other implementations, right?

It would make sense if there were more settings. Here what I'd do is early continue: if ($name !== 'defaultAuthor') { continue; } and get rid of unnecessary indent of the remainder of the code. We should refactor this to switch only when we get to a point where we need more settings, because effort is not gonna be big (it really is a gut feeling when to prepare for possible future and when not to).

If you still prefer the switch, then I'd see logic for the case moved to a separate method.

Just for readability.

Copy link
Member Author

Choose a reason for hiding this comment

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

The question is whether we want to be consistent with the other implementations or improve things when it comes to FieldTypes, https://github.com/ezsystems/ezpublish-kernel/blob/master/eZ/Publish/Core/FieldType/Date/Type.php#L224.

Copy link
Member

Choose a reason for hiding this comment

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

Each Field Type can be treated a separate, so implementations can differ. What we need to avoid is switching code style approaches between valid alternatives. For example pushing towards short array syntax was my mistake, it should be done for entire codebase at once.

In this case I find Field Type implementations quite confusing, so I would rather improve.

That said, maybe just extract contents of this case block to a separate method? It keeps somewhat consistency and improves readability.

];
if (!in_array($value, $definedTypes, true)) {
$validationErrors[] = new ValidationError(
"Setting '%setting%' is of unknown type",
Copy link
Member

Choose a reason for hiding this comment

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

I think this kinda relates to previous comment about naming - this setting is not a "type" to me. It's a default value, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is actually my bad, $definedValues sounds much better.

@@ -61,7 +63,7 @@ public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
*/
public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
{
// Nothing to store
$storageDef->dataInt1 = $fieldDef->fieldTypeConstraints->fieldSettings['defaultAuthor'];
Copy link
Member

Choose a reason for hiding this comment

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

Please cast it to (int) or PostgreSQL will die should this data come from an actual web form. Unless it is already casted elsewhere?

@@ -22,6 +27,7 @@
*/
class AuthorTest extends TestCase
{
const ADMINISTRATOR_USER_ID = 14;
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't seem it is used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great catch! I must have forgotten to remove it alongside with the mocks from the previous implementation.

use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter;
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition as PersistenceFieldDefinition;
Copy link
Member

Choose a reason for hiding this comment

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

The naming convention is rather SPIFieldDefinition.

/**
* Default value empty.
*/
const DEFAULT_EMPTY = -1;
Copy link
Member

Choose a reason for hiding this comment

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

@alongosz SIDE: you see any issues with using negative int's for data_int1 column? Or should we rather keep the values in the positive range?

No, this is fine. data_int1 is NOT NULL anyway, so it has to be something that is non-null and non-zero.

@konradoboza what I don't get is how these constants apply to this solution? What is their purpose?

The naming is very generic and the PhpDoc description says the same thing, which is the perfect example of how not to document code :trollface:

I can guess based on the other parts of the PR, but if I need to guess it, then it implies it needs an improvement :-)
I prefer you find some better naming so code is either self-commenting or requires only brief explanation. If you can't find better name, then at least provide more descriptive doc block.

... or maybe, since this is a default value, it should belong to the Value class, not type? Even despite the fact that you're using it in the Type class.

context: People upgrading would expect default value to just be current user somehow,

@andrerom Actually... No. Why? This is BC break.

I know PMs really want this by default, so AdminUI can set it differently.

From my POV both cases would be BC break, but we should at least make API BC.

cc @SylvainGuittard

$incomingSettingsHash['defaultAuthor'] = Type::DEFAULT_EMPTY;
break;
default:
$incomingSettingsHash['defaultAuthor'] = Type::DEFAULT_CURRENT_USER;
Copy link
Member

Choose a reason for hiding this comment

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

as I've said above - it's a BC break.

{% endif %}
{{ block( 'settings_defaultvalue' ) }}
</ul>
{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

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

Front-end review ping @dew326 :trollface: Just kidding.

@webhdx, could you take a look? Looks good to me, but with Twigs I don't trust myself :P

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks okay.

@konradoboza
Copy link
Member Author

Suggestions from @alongosz applied. Regarding BC: in v1 and Legacy we had prefilled Author data by default. Within the scope of this (and related) PR we give editors an option in CT creation view. Existing CT's (e.g. article) will be prefilled by default. /cc @lserwatka

zrzut ekranu 2018-08-08 o 16 18 31

@konradoboza konradoboza force-pushed the EZP-29290-prefilling_author_fieldtype branch from a0cd246 to d6728bd Compare August 8, 2018 14:36
@ezsystems ezsystems deleted a comment from ezrobot Aug 8, 2018
@konradoboza konradoboza force-pushed the EZP-29290-prefilling_author_fieldtype branch from d6728bd to 7f4bc34 Compare August 9, 2018 07:54
@lserwatka
Copy link
Member

@barbaragr let's proceed with QA here.

@alongosz
Copy link
Member

alongosz commented Aug 9, 2018

Code looks now good.

If we want to merge a BC break, at least please add a note in doc/bc stating that since new version, Author Field Type will be prefilled with current user by default.

I'm against making BC breaks in places we have a choice (e.g. not forced by bugs), but since this is what PMs want, BC note is the least we could do.

@konradoboza
Copy link
Member Author

BC break note added in: #2411

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
7 participants