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

use constants in documentation

use constants for determinate a field type
  • Loading branch information
peter-gribanov committed Jul 22, 2020
1 parent dbd9087 commit 02bb8bd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 52 deletions.
9 changes: 5 additions & 4 deletions src/Action/SetObjectFieldValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistry;
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -119,19 +120,19 @@ 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 && TemplateRegistry::TYPE_DATE === $fieldDescription->getType()) {
$value = new \DateTime($value);
}

// Handle boolean type transforming the value into a boolean
if ('' !== $value && 'boolean' === $fieldDescription->getType()) {
if ('' !== $value && TemplateRegistry::TYPE_BOOLEAN === $fieldDescription->getType()) {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
}

// Handle entity choice association type, transforming the value into entity
if ('' !== $value
&& 'choice' === $fieldDescription->getType()
&& TemplateRegistry::TYPE_CHOICE === $fieldDescription->getType()
&& null !== $fieldDescription->getOption('class')
// NEXT_MAJOR: Replace this call with "$fieldDescription->getOption('class') === $fieldDescription->getTargetModel()".
&& $this->hasFieldDescriptionAssociationWithClass($fieldDescription, $fieldDescription->getOption('class'))
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 02bb8bd

Please sign in to comment.