Skip to content
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
wants to merge 6 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 125 additions & 1 deletion src/Contracts/Field/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Collaborator

@OskarStark OskarStark Nov 8, 2023

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

Copy link
Contributor Author

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!

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;
}
23 changes: 23 additions & 0 deletions src/Field/AbstractField.php
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);
}
}
13 changes: 3 additions & 10 deletions src/Field/ArrayField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
11 changes: 3 additions & 8 deletions src/Field/AssociationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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')
Expand Down
13 changes: 3 additions & 10 deletions src/Field/AvatarField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
13 changes: 3 additions & 10 deletions src/Field/BooleanField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)
Expand Down
13 changes: 3 additions & 10 deletions src/Field/ChoiceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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')
Expand Down
13 changes: 3 additions & 10 deletions src/Field/CodeEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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')
Expand Down
13 changes: 3 additions & 10 deletions src/Field/CollectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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')
Expand Down
13 changes: 3 additions & 10 deletions src/Field/ColorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading