Skip to content

Commit

Permalink
register a datetime field type as editable
Browse files Browse the repository at this point in the history
correct documentation

add a time field options in documentation

remove deprecated field types from documentation

datetime field type is not editable
  • Loading branch information
peter-gribanov committed Jun 16, 2020
1 parent f807ac1 commit 307a245
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 53 deletions.
28 changes: 25 additions & 3 deletions docs/reference/action_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,22 @@ Available types and associated options
| | catalogue | Translation catalogue. |
+ +---------------------+-----------------------------------------------------------------------+
| | class | Class path for editable association field. |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| currency | currency (m) | A currency string (EUR or USD for instance). |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| date | format | A format understandable by Twig's ``date`` function. |
+ +---------------------+-----------------------------------------------------------------------+
| | timezone | Second argument for Twig's ``date`` function |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| time | format | A format understandable by Twig's ``date`` function. |
+ +---------------------+-----------------------------------------------------------------------+
| | timezone | Second argument for Twig's ``date`` function |
+-----------+---------------------+-----------------------------------------------------------------------+
| datetime | format | A format understandable by Twig's ``date`` function. |
+ +---------------------+-----------------------------------------------------------------------+
Expand All @@ -193,16 +203,22 @@ Available types and associated options
| | subject | Add subject parameter to email link. |
+ +---------------------+-----------------------------------------------------------------------+
| | body | Add body parameter to email link. |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| percent | | Renders value as a percentage. |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| string | | Renders a string. |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| text | | See 'string' |
| textarea | | See 'string' |
+-----------+---------------------+-----------------------------------------------------------------------+
| html | | Renders string as html |
+-----------+---------------------+-----------------------------------------------------------------------+
| time | | Renders a datetime's time with format ``H:i:s``. |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| trans | catalogue | Translates the value with catalogue ``catalogue`` if defined. |
+-----------+---------------------+-----------------------------------------------------------------------+
Expand All @@ -215,6 +231,12 @@ Available types and associated options
| | parameters | Route parameters |
+ +---------------------+-----------------------------------------------------------------------+
| | hide_protocol | Hide http:// or https:// (default: false) |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+
| integer | | Renders a integer. |
+ +---------------------+-----------------------------------------------------------------------+
| | editable | Yes/No; editable allows to edit directly from the list if authorized. |
+-----------+---------------------+-----------------------------------------------------------------------+

If you have the SonataDoctrineORMAdminBundle installed, you have access
Expand Down
4 changes: 2 additions & 2 deletions src/Action/SetObjectFieldValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public function __invoke(Request $request): JsonResponse
$propertyPath = new PropertyPath($field);
}

// Handle date and datetime types have setter expecting a DateTime object
if ('' !== $value && \in_array($fieldDescription->getType(), ['date', 'datetime'], true)) {
// Handle date type has setter expect a DateTime object
if ('' !== $value && 'date' === $fieldDescription->getType()) {
$value = new \DateTime($value);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Resources/views/CRUD/base_list_field.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ file that was distributed with this source code.
) %}

{% if field_description.type == constant('Sonata\\AdminBundle\\Templating\\TemplateRegistry::TYPE_DATE') and value is not empty %}
{% set data_value = value.format('Y-m-d') %}
{# it is a x-editable format https://vitalets.github.io/x-editable/docs.html#date #}
{% set data_value = value|date('Y-m-d') %}
{% elseif field_description.type == constant('Sonata\\AdminBundle\\Templating\\TemplateRegistry::TYPE_BOOLEAN') and value is empty %}
{% set data_value = 0 %}
{% elseif value is iterable %}
Expand All @@ -70,6 +71,9 @@ file that was distributed with this source code.
{% if field_description.label is not same as(false) %}
data-title="{{ field_description.label|trans({}, field_description.translationDomain) }}"
{% endif %}
{% if field_description.type == constant('Sonata\\AdminBundle\\Templating\\TemplateRegistry::TYPE_DATE') %}
data-format="yyyy-mm-dd"
{% endif %}
data-pk="{{ admin.id(object) }}"
data-url="{{ url }}" {% endblock %}>
{{ block('field') }}
Expand Down
47 changes: 0 additions & 47 deletions tests/Action/SetObjectFieldValueActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,53 +173,6 @@ public function testSetObjectFieldValueActionWithDate(): void
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
}

public function testSetObjectFieldValueActionWithDateTime(): void
{
$object = new Bafoo();
$request = new Request([
'code' => 'sonata.post.admin',
'objectId' => 42,
'field' => 'datetimeProp',
'value' => '2020-12-12 23:11:23',
'context' => 'list',
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']);

$fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
$pool = $this->prophesize(Pool::class);
$translator = $this->prophesize(TranslatorInterface::class);
$propertyAccessor = new PropertyAccessor();
$templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
$container = $this->prophesize(ContainerInterface::class);

$this->admin->getObject(42)->willReturn($object);
$this->admin->getCode()->willReturn('sonata.post.admin');
$this->admin->hasAccess('edit', $object)->willReturn(true);
$this->admin->getListFieldDescription('datetimeProp')->willReturn($fieldDescription->reveal());
$this->admin->update($object)->shouldBeCalled();

$this->admin->getTemplate('base_list_field')->willReturn('admin_template');
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
$container->get('sonata.post.admin.template_registry')->willReturn($templateRegistry->reveal());
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
$this->twig->addExtension(new SonataAdminExtension(
$pool->reveal(),
null,
$translator->reveal(),
$container->reveal()
));
$fieldDescription->getOption('editable')->willReturn(true);
$fieldDescription->getAdmin()->willReturn($this->admin->reveal());
$fieldDescription->getType()->willReturn('datetime');
$fieldDescription->getTemplate()->willReturn('field_template');
$fieldDescription->getValue(Argument::cetera())->willReturn('some value');

$this->validator->validate($object)->willReturn(new ConstraintViolationList([]));

$response = ($this->action)($request);

$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
}

public function testSetObjectFieldValueActionOnARelationField(): void
{
$object = new Baz();
Expand Down

0 comments on commit 307a245

Please sign in to comment.