Skip to content

Commit

Permalink
EZP-28664: Enabled checkbox should be a part of ezuser fieldtype in C…
Browse files Browse the repository at this point in the history
…ontent Edit (ezsystems#208)
  • Loading branch information
webhdx authored and Łukasz Serwatka committed Dec 22, 2017
1 parent 93cb9d6 commit 16f2835
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 13 deletions.
7 changes: 7 additions & 0 deletions bundle/Resources/config/form_types.yml
@@ -0,0 +1,7 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

EzSystems\RepositoryForms\Form\Type\SwitcherType: ~
1 change: 1 addition & 0 deletions bundle/Resources/config/services.yml
Expand Up @@ -3,6 +3,7 @@ imports:
- {resource: section.yml}
- {resource: role.yml}
- {resource: fieldtypes.yml}
- {resource: form_types.yml}

parameters:
ezrepoforms.field_type_form_mapper.dispatcher.class: EzSystems\RepositoryForms\FieldType\FieldTypeFormMapperDispatcher
Expand Down
5 changes: 5 additions & 0 deletions bundle/Resources/translations/ezrepoforms_content.en.xlf
Expand Up @@ -36,6 +36,11 @@
<target>E-mail</target>
<note>key: content.field_type.ezuser.email</note>
</trans-unit>
<trans-unit id="8ac4ec40153c80fd39d2051802e30ebffb6f1fe1" resname="content.field_type.ezuser.enabled">
<source>Enabled</source>
<target state="new">Enabled</target>
<note>key: content.field_type.ezuser.enabled</note>
</trans-unit>
<trans-unit id="4c65e5d17516c19e311161e374bbd39f053b5e6b" resname="content.field_type.ezuser.password">
<source>Password</source>
<target>Password</target>
Expand Down
24 changes: 14 additions & 10 deletions lib/Data/User/UserAccountFieldData.php
Expand Up @@ -15,25 +15,29 @@
*/
class UserAccountFieldData
{
/**
* @var string
*/
/** @var string */
public $username;

/**
* @var string
*/
/** @var string */
public $password;

/**
* @var string
*/
/** @var string */
public $email;

public function __construct($username, $password, $email)
/** @var bool */
public $enabled;

/**
* @param string $username
* @param string $password
* @param string $email
* @param bool $enabled
*/
public function __construct($username, $password, $email, $enabled = true)
{
$this->username = $username;
$this->password = $password;
$this->email = $email;
$this->enabled = $enabled;
}
}
2 changes: 1 addition & 1 deletion lib/FieldType/Mapper/UserAccountFieldValueFormMapper.php
Expand Up @@ -81,7 +81,7 @@ public function getModelTransformer()
{
return new CallbackTransformer(
function (ApiUserValue $data) {
return new UserAccountFieldData($data->login, null, $data->email);
return new UserAccountFieldData($data->login, null, $data->email, $data->enabled);
},
function (UserAccountFieldData $submittedData) {
return $submittedData;
Expand Down
3 changes: 3 additions & 0 deletions lib/Form/EventSubscriber/UserFieldsSubscriber.php
Expand Up @@ -62,12 +62,14 @@ private function handleUserCreateData(UserCreateData $data)
$data->login = $userAccountFieldData->username;
$data->email = $userAccountFieldData->email;
$data->password = $userAccountFieldData->password;
$data->enabled = $userAccountFieldData->enabled;

/** @var Value $userValue */
$userValue = clone $data->contentType
->getFieldDefinition($fieldData->field->fieldDefIdentifier)->defaultValue;
$userValue->login = $userAccountFieldData->username;
$userValue->email = $userAccountFieldData->email;
$userValue->enabled = $userAccountFieldData->enabled;

$fieldData->value = $userValue;

Expand All @@ -90,6 +92,7 @@ private function handleUserUpdateData(UserUpdateData $data, $languageCode)
$userAccountFieldData = $fieldData->value;
$data->email = $userAccountFieldData->email;
$data->password = $userAccountFieldData->password;
$data->enabled = $userAccountFieldData->enabled;

/** @var Value $userValue */
$userValue = clone $data->user->getField($fieldData->field->fieldDefIdentifier, $languageCode)->value;
Expand Down
8 changes: 8 additions & 0 deletions lib/Form/Type/FieldType/UserAccountFieldType.php
Expand Up @@ -8,6 +8,7 @@
namespace EzSystems\RepositoryForms\Form\Type\FieldType;

use EzSystems\RepositoryForms\Data\User\UserAccountFieldData;
use EzSystems\RepositoryForms\Form\Type\SwitcherType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
Expand Down Expand Up @@ -48,6 +49,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => true,
'label' => 'content.field_type.ezuser.email',
]);

if (in_array($options['intent'], ['create', 'update'], true)) {
$builder->add('enabled', SwitcherType::class, [
'required' => false,
'label' => /** @Desc("Enabled") */ 'content.field_type.ezuser.enabled',
]);
}
}

public function configureOptions(OptionsResolver $resolver)
Expand Down
34 changes: 34 additions & 0 deletions lib/Form/Type/SwitcherType.php
@@ -0,0 +1,34 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryForms\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SwitcherType extends AbstractType
{
public function getParent()
{
return CheckboxType::class;
}

public function getName()
{
return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
return 'switcher';
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('required', false);
}
}
2 changes: 0 additions & 2 deletions lib/Form/Type/User/BaseUserType.php
Expand Up @@ -11,7 +11,6 @@
use EzSystems\RepositoryForms\Form\EventSubscriber\UserFieldsSubscriber;
use EzSystems\RepositoryForms\Form\Type\Content\BaseContentType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -41,7 +40,6 @@ public function getParent()
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('enabled', CheckboxType::class, ['required' => false, 'label' => /** @Desc("Enabled") */ 'user.enabled'])
->add('cancel', SubmitType::class, [
'label' => /** @Desc("Cancel") */ 'user.cancel',
'attr' => ['formnovalidate' => 'formnovalidate'],
Expand Down

0 comments on commit 16f2835

Please sign in to comment.