Skip to content

Commit

Permalink
EZP-29290: As an Editor, I want to have preconfigured "Author" sectio…
Browse files Browse the repository at this point in the history
…n while creating the content (ezsystems#2392)

* WIP prefilling author field type

* 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

* EZP-29290: As an Editor, I want to have preconfigured "Author" section while creating the content
  • Loading branch information
konradoboza authored and Łukasz Serwatka committed Aug 9, 2018
1 parent 3ef0b13 commit 766c4a3
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 26 deletions.
Expand Up @@ -266,8 +266,16 @@
</ul>
{% endblock %}


{% block ezauthor_settings %}{% endblock %}
{% block ezauthor_settings %}
<ul class="ez-fielddefinition-settings ez-fielddefinition-{{ fielddefinition.fieldTypeIdentifier }}-settings">
{% if settings.defaultType == constant( 'eZ\\Publish\\Core\\FieldType\\Author\\Type::DEFAULT_EMPTY' ) %}
{% set defaultValue = 'fielddefinition.default-value.empty'|trans|desc("Empty") %}
{% else %}
{% set defaultValue = 'fielddefinition.default-value.current_author'|trans|desc("Current User") %}
{% endif %}
{{ block( 'settings_defaultvalue' ) }}
</ul>
{% endblock %}

{% block ezurl_settings %}{% endblock %}

Expand Down
Expand Up @@ -34,6 +34,7 @@ parameters:
ezpublish_rest.csrf_listener.class: eZ\Bundle\EzPublishRestBundle\EventListener\CsrfListener
ezpublish_rest.response_listener.class: eZ\Bundle\EzPublishRestBundle\EventListener\ResponseListener

ezpublish_rest.field_type_processor.ezauthor.class: eZ\Publish\Core\REST\Common\FieldTypeProcessor\AuthorProcessor
ezpublish_rest.field_type_processor_registry.class: eZ\Publish\Core\REST\Common\FieldTypeProcessorRegistry
ezpublish_rest.field_type_processor.ezimage.class: eZ\Publish\Core\REST\Common\FieldTypeProcessor\ImageProcessor
ezpublish_rest.field_type_processor.ezdatetime.class: eZ\Publish\Core\REST\Common\FieldTypeProcessor\DateAndTimeProcessor
Expand Down
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\Core\FieldType\Author\Author;
use eZ\Publish\Core\FieldType\Author\AuthorCollection;
use eZ\Publish\Core\FieldType\Author\Type;
use eZ\Publish\Core\FieldType\Author\Value as AuthorValue;
use eZ\Publish\API\Repository\Values\Content\Field;

Expand Down Expand Up @@ -38,7 +39,12 @@ public function getTypeName()
*/
public function getSettingsSchema()
{
return array();
return array(
'defaultAuthor' => array(
'type' => 'choice',
'default' => Type::DEFAULT_CURRENT_USER,
),
);
}

/**
Expand All @@ -48,7 +54,9 @@ public function getSettingsSchema()
*/
public function getValidFieldSettings()
{
return array();
return array(
'defaultAuthor' => Type::DEFAULT_CURRENT_USER,
);
}

/**
Expand Down Expand Up @@ -98,12 +106,10 @@ public function getInvalidValidatorConfiguration()
/**
* Get initial field data for valid object creation.
*
* @return mixed
* @return \eZ\Publish\Core\FieldType\Author\Value
*/
public function getValidCreationFieldData()
{
// We may only create times from timestamps here, since storing will
// loose information about the timezone.
return new AuthorValue(
array(
new Author(
Expand Down Expand Up @@ -192,7 +198,7 @@ public function provideInvalidCreationFieldData()
/**
* Get update field externals data.
*
* @return array
* @return \eZ\Publish\Core\FieldType\Author\Value
*/
public function getValidUpdateFieldData()
{
Expand Down
103 changes: 103 additions & 0 deletions eZ/Publish/Core/FieldType/Author/Type.php
Expand Up @@ -12,6 +12,7 @@
use eZ\Publish\Core\FieldType\Value as BaseValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use eZ\Publish\Core\FieldType\ValidationError;

/**
* Author field type.
Expand All @@ -21,6 +22,25 @@
*/
class Type extends FieldType
{
/**
* Flag which stands for prefilling Author FieldType with current user by default.
* It is used in a Content Type edit view.
*/
const DEFAULT_CURRENT_USER = 1;

/**
* Flag which stands for setting Author FieldType empty by default.
* It is used in a Content Type edit view.
*/
const DEFAULT_EMPTY = -1;

protected $settingsSchema = [
'defaultAuthor' => [
'type' => 'choice',
'default' => self::DEFAULT_CURRENT_USER,
],
];

/**
* Returns the field type identifier for this field type.
*
Expand Down Expand Up @@ -148,4 +168,87 @@ public function isSearchable()
{
return true;
}

/**
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
* @param array $fieldSettings
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validateFieldSettings($fieldSettings)
{
$validationErrors = [];

foreach ($fieldSettings as $name => $value) {
$settingNameError = $this->validateSettingName($name);

if ($settingNameError instanceof ValidationError) {
$validationErrors[] = $settingNameError;
}

switch ($name) {
case 'defaultAuthor':
$settingValueError = $this->validateDefaultAuthorSetting($name, $value);
if ($settingValueError instanceof ValidationError) {
$validationErrors[] = $settingValueError;
}
break;
}
}

return $validationErrors;
}

/**
* Validates the fieldSetting name.
*
* @param string $name
*
* @return \eZ\Publish\SPI\FieldType\ValidationError|null
*/
private function validateSettingName($name)
{
if (!isset($this->settingsSchema[$name])) {
return new ValidationError(
"Setting '%setting%' is unknown",
null,
[
'%setting%' => $name,
],
"[$name]"
);
}

return null;
}

/**
* Validates if the defaultAuthor setting has one of the defined values.
*
* @param string $name
* @param string $value
*
* @return \eZ\Publish\SPI\FieldType\ValidationError|null
*/
private function validateDefaultAuthorSetting($name, $value)
{
$definedValues = [
self::DEFAULT_CURRENT_USER,
self::DEFAULT_EMPTY,
];

if (!in_array($value, $definedValues, true)) {
return new ValidationError(
"Setting '%setting%' has unknown default value",
null,
[
'%setting%' => $name,
],
"[$name]"
);
}

return null;
}
}
101 changes: 88 additions & 13 deletions eZ/Publish/Core/FieldType/Tests/AuthorTest.php
Expand Up @@ -72,7 +72,12 @@ protected function getValidatorConfigurationSchemaExpectation()
*/
protected function getSettingsSchemaExpectation()
{
return array();
return array(
'defaultAuthor' => array(
'type' => 'choice',
'default' => AuthorType::DEFAULT_CURRENT_USER,
),
);
}

/**
Expand Down Expand Up @@ -321,6 +326,88 @@ public function provideInputForFromHash()
);
}

/**
* Provide data sets with field settings which are considered valid by the
* {@link validateFieldSettings()} method.
*
* Returns an array of data provider sets with a single argument: A valid
* set of field settings.
* For example:
*
* <code>
* return array(
* array(
* array(),
* ),
* array(
* array( 'rows' => 2 )
* ),
* // ...
* );
* </code>
*
* @return array
*/
public function provideValidFieldSettings()
{
return array(
array(
array(),
),
array(
array(
'defaultAuthor' => AuthorType::DEFAULT_EMPTY,
),
),
array(
array(
'defaultAuthor' => AuthorType::DEFAULT_CURRENT_USER,
),
),
);
}

/**
* Provide data sets with field settings which are considered invalid by the
* {@link validateFieldSettings()} method. The method must return a
* non-empty array of validation error when receiving such field settings.
*
* Returns an array of data provider sets with a single argument: A valid
* set of field settings.
* For example:
*
* <code>
* return array(
* array(
* true,
* ),
* array(
* array( 'nonExistentKey' => 2 )
* ),
* // ...
* );
* </code>
*
* @return array
*/
public function provideInValidFieldSettings()
{
return array(
array(
array(
// non-existent setting
'useSeconds' => 23,
),
),
array(
array(
//defaultAuthor must be constant
'defaultAuthor' => 42,
),
),
);
}

protected function tearDown()
{
unset($this->authors);
Expand All @@ -339,18 +426,6 @@ public function testValidatorConfigurationSchema()
);
}

/**
* @covers \eZ\Publish\Core\FieldType\FieldType::getSettingsSchema
*/
public function testSettingsSchema()
{
$ft = $this->createFieldTypeUnderTest();
self::assertEmpty(
$ft->getSettingsSchema(),
'The settings schema does not match what is expected.'
);
}

/**
* @covers \eZ\Publish\Core\FieldType\Author\Type::acceptValue
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
Expand Down
Expand Up @@ -8,8 +8,10 @@
*/
namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;

use eZ\Publish\Core\FieldType\Author\Type as AuthorType;
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
use eZ\Publish\Core\FieldType\FieldSettings;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
Expand Down Expand Up @@ -61,7 +63,7 @@ public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
*/
public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
{
// Nothing to store
$storageDef->dataInt1 = (int)$fieldDef->fieldTypeConstraints->fieldSettings['defaultAuthor'];
}

/**
Expand All @@ -72,7 +74,13 @@ public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageField
*/
public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
{
$fieldDef->defaultValue->data = array();
$fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
[
'defaultAuthor' => $storageDef->dataInt1 ? $storageDef->dataInt1 : AuthorType::DEFAULT_CURRENT_USER,
]
);

$fieldDef->defaultValue->data = [];
}

/**
Expand Down Expand Up @@ -123,7 +131,7 @@ private function generateXmlString(array $authorValue)
*
* @param string $xmlString XML String stored in storage engine
*
* @return \eZ\Publish\Core\FieldType\Author\Value
* @return \eZ\Publish\Core\FieldType\Author\Value[]
*/
private function restoreValueFromXmlString($xmlString)
{
Expand Down

0 comments on commit 766c4a3

Please sign in to comment.