-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Abstracted more repeating code, added abstract for fields, made creat… #6008
Open
bytes-commerce
wants to merge
6
commits into
EasyCorp:4.x
Choose a base branch
from
bytes-commerce:add-field-fqcn-to-field-dtos
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9757151
Abstracted more repeating code, added abstract for fields, made creat…
bytes-commerce 239a0fb
Merge branch '4.x' into add-field-fqcn-to-field-dtos
bytes-commerce 025c06c
Add deprecation notice for 'false', which should not be an acceptable…
bytes-commerce c63f0b2
Applied PHPCS.
bytes-commerce 8f35c77
Remove redundant call to Fqcn setter.
bytes-commerce 25c99f4
Remove deprecation notice for false
bytes-commerce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,138 @@ | |
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Contracts\Field; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Config\Asset; | ||
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; | ||
use Symfony\Contracts\Translation\TranslatableInterface; | ||
|
||
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
interface FieldInterface | ||
{ | ||
public static function new(string $propertyName, ?string /* TranslatableInterface|string|false|null */ $label = null); | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): self; | ||
|
||
public function getAsDto(): FieldDto; | ||
|
||
public function setFieldFqcn(string $fieldFqcn): self; | ||
|
||
public function setProperty(string $propertyName): self; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public function setLabel($label): self; | ||
|
||
public function setValue($value): self; | ||
|
||
public function setFormattedValue($value): self; | ||
|
||
public function formatValue(?callable $callable): self; | ||
|
||
public function setVirtual(bool $isVirtual): self; | ||
|
||
public function setDisabled(bool $disabled = true): self; | ||
|
||
public function setRequired(bool $isRequired): self; | ||
|
||
public function setEmptyData($emptyData = null): self; | ||
|
||
public function setFormType(string $formTypeFqcn): self; | ||
|
||
public function setFormTypeOptions(array $options): self; | ||
|
||
/** | ||
* @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class') | ||
*/ | ||
public function setFormTypeOption( | ||
string $optionName, | ||
$optionValue | ||
): self; | ||
|
||
/** | ||
* @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class') | ||
*/ | ||
public function setFormTypeOptionIfNotSet( | ||
string $optionName, | ||
$optionValue | ||
): self; | ||
|
||
public function setSortable(bool $isSortable): self; | ||
|
||
public function setPermission(string $permission): self; | ||
|
||
/** | ||
* @param string $textAlign It can be 'left', 'center' or 'right' | ||
*/ | ||
public function setTextAlign(string $textAlign): self; | ||
|
||
public function setHelp(TranslatableInterface|string $help): self; | ||
|
||
public function addCssClass(string $cssClass): self; | ||
|
||
public function setCssClass(string $cssClass): self; | ||
|
||
public function setTranslationParameters(array $parameters): self; | ||
|
||
public function setTemplateName(string $name): self; | ||
|
||
public function setTemplatePath(string $path): self; | ||
|
||
public function addFormTheme(string ...$formThemePaths): self; | ||
|
||
public function addWebpackEncoreEntries(Asset|string ...$entryNamesOrAssets | ||
): self; | ||
|
||
public function addCssFiles(Asset|string ...$pathsOrAssets): self; | ||
|
||
public function addJsFiles(Asset|string ...$pathsOrAssets): self; | ||
|
||
public function addHtmlContentsToHead(string ...$contents): self; | ||
|
||
public function addHtmlContentsToBody(string ...$contents): self; | ||
|
||
public function setCustomOption( | ||
string $optionName, | ||
$optionValue | ||
): self; | ||
|
||
public function setCustomOptions(array $options): self; | ||
|
||
public function hideOnDetail(): self; | ||
|
||
public function hideOnForm(): self; | ||
|
||
public function hideWhenCreating(): self; | ||
|
||
public function hideWhenUpdating(): self; | ||
|
||
public function hideOnIndex(): self; | ||
|
||
public function onlyOnDetail(): self; | ||
|
||
public function onlyOnForms(): self; | ||
|
||
public function onlyOnIndex(): self; | ||
|
||
public function onlyWhenCreating(): self; | ||
|
||
public function onlyWhenUpdating(): self; | ||
|
||
/** | ||
* @param int|string $cols An integer with the number of columns that this field takes (e.g. 6), | ||
* or a string with responsive col CSS classes (e.g. 'col-6 col-sm-4 col-lg-3') | ||
*/ | ||
public function setColumns(int|string $cols): self; | ||
|
||
/** | ||
* Used to define the columns of fields when users don't define the | ||
* columns explicitly using the setColumns() method. | ||
* This should only be used if you create a custom EasyAdmin field, | ||
* not when configuring fields in your backend. | ||
* | ||
* @internal | ||
*/ | ||
public function setDefaultColumns(int|string $cols): self; | ||
|
||
public function setIcon(?string $iconCssClass, string $invokingMethod = 'FormField::setIcon()'): self; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Field; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface; | ||
use Symfony\Contracts\Translation\TranslatableInterface; | ||
|
||
abstract class AbstractField implements FieldInterface | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_ICON = 'icon'; | ||
|
||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new static()) | ||
->setFieldFqcn(static::class) | ||
->setProperty($propertyName) | ||
->setLabel($label); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,18 +10,11 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class ArrayField implements FieldInterface | ||
final class ArrayField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
return parent::new($propertyName, $label) | ||
->setTemplateName('crud/field/array') | ||
->setFormType(CollectionType::class) | ||
->addCssClass('field-array') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,8 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class AssociationField implements FieldInterface | ||
final class AssociationField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_AUTOCOMPLETE = 'autocomplete'; | ||
public const OPTION_EMBEDDED_CRUD_FORM_CONTROLLER = 'crudControllerFqcn'; | ||
/** @deprecated since easycorp/easyadmin-bundle 4.4.3 use AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER */ | ||
|
@@ -36,12 +34,9 @@ final class AssociationField implements FieldInterface | |
public const OPTION_EMBEDDED_CRUD_FORM_NEW_PAGE_NAME = 'crudNewPageName'; | ||
public const OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME = 'crudEditPageName'; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
return parent::new($propertyName, $label) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
->setTemplateName('crud/field/association') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,21 +10,14 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class AvatarField implements FieldInterface | ||
final class AvatarField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_IS_GRAVATAR_EMAIL = 'isGravatarEmail'; | ||
public const OPTION_HEIGHT = 'height'; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
return parent::new($propertyName, $label) | ||
->setTemplateName('crud/field/avatar') | ||
->setFormType(TextType::class) | ||
->addCssClass('field-avatar') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,8 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class BooleanField implements FieldInterface | ||
final class BooleanField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_RENDER_AS_SWITCH = 'renderAsSwitch'; | ||
public const OPTION_HIDE_VALUE_WHEN_TRUE = 'hideValueWhenTrue'; | ||
public const OPTION_HIDE_VALUE_WHEN_FALSE = 'hideValueWhenFalse'; | ||
|
@@ -23,14 +21,9 @@ final class BooleanField implements FieldInterface | |
/** @internal */ | ||
public const CSRF_TOKEN_NAME = 'ea-toggle'; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
return parent::new($propertyName, $label) | ||
->setTextAlign(TextAlign::CENTER) | ||
->setTemplateName('crud/field/boolean') | ||
->setFormType(CheckboxType::class) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,8 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class ChoiceField implements FieldInterface | ||
final class ChoiceField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_ALLOW_MULTIPLE_CHOICES = 'allowMultipleChoices'; | ||
public const OPTION_AUTOCOMPLETE = 'autocomplete'; | ||
public const OPTION_CHOICES = 'choices'; | ||
|
@@ -27,14 +25,9 @@ final class ChoiceField implements FieldInterface | |
public const WIDGET_AUTOCOMPLETE = 'autocomplete'; | ||
public const WIDGET_NATIVE = 'native'; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
return parent::new($propertyName, $label) | ||
->setTemplateName('crud/field/choice') | ||
->setFormType(ChoiceType::class) | ||
->addCssClass('field-select') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,8 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class CodeEditorField implements FieldInterface | ||
final class CodeEditorField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_INDENT_WITH_TABS = 'indentWithTabs'; | ||
public const OPTION_LANGUAGE = 'language'; | ||
public const OPTION_NUM_OF_ROWS = 'numOfRows'; | ||
|
@@ -22,14 +20,9 @@ final class CodeEditorField implements FieldInterface | |
|
||
private const ALLOWED_LANGUAGES = ['css', 'dockerfile', 'js', 'javascript', 'markdown', 'nginx', 'php', 'shell', 'sql', 'twig', 'xml', 'yaml-frontmatter', 'yaml']; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
return parent::new($propertyName, $label) | ||
->setTemplateName('crud/field/code_editor') | ||
->setFormType(CodeEditorType::class) | ||
->addCssClass('field-code_editor') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,8 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class CollectionField implements FieldInterface | ||
final class CollectionField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_ALLOW_ADD = 'allowAdd'; | ||
public const OPTION_ALLOW_DELETE = 'allowDelete'; | ||
public const OPTION_ENTRY_IS_COMPLEX = 'entryIsComplex'; | ||
|
@@ -25,14 +23,9 @@ final class CollectionField implements FieldInterface | |
public const OPTION_ENTRY_CRUD_NEW_PAGE_NAME = 'entryCrudNewPageName'; | ||
public const OPTION_ENTRY_CRUD_EDIT_PAGE_NAME = 'entryCrudEditPageName'; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
return parent::new($propertyName, $label) | ||
->setTemplateName('crud/field/collection') | ||
->setFormType(CollectionType::class) | ||
->addCssClass('field-collection') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,21 +9,14 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
*/ | ||
final class ColorField implements FieldInterface | ||
final class ColorField extends AbstractField | ||
{ | ||
use FieldTrait; | ||
|
||
public const OPTION_SHOW_SAMPLE = 'showSample'; | ||
public const OPTION_SHOW_VALUE = 'showValue'; | ||
|
||
/** | ||
* @param TranslatableInterface|string|false|null $label | ||
*/ | ||
public static function new(string $propertyName, $label = null): self | ||
public static function new(string $propertyName, TranslatableInterface|string|false|null $label = null): FieldInterface | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
return parent::new($propertyName, $label) | ||
->setTemplateName('crud/field/color') | ||
->setFormType(ColorType::class) | ||
->addCssClass('field-color') | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding methods to an interface is a BC break, you should use @method annotations instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, I am afraid, but using @method will not improve the situation; the opposite is the case, as IDEs would indicate methods that are not even there maybe, creating a booby trap for developers. I am considering a individual Interface for the existing fields, which then would be BC compatible by just extending the given Interface. 🤔 Something like
FieldTraitAwareInterface
or similar. Thanks for the heads up!