diff --git a/composer.json b/composer.json index 712c557798d86e91a239c4771ea41c4e8228e873..5edb34474f675621845e69d6c90022822a660739 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,6 @@ "source": "http://cgit.drupalcode.org/required_api" }, "require": { - "drupal/core": "^9.1 || ^10 || ^11" + "drupal/core": "^10 || ^11" } } diff --git a/required_api.info.yml b/required_api.info.yml index 08347abf83e02ef88ba036a3cb66491c0de20f50..368d0b965e9bdd88eedd9354ca9ab81a32d3e774 100644 --- a/required_api.info.yml +++ b/required_api.info.yml @@ -1,4 +1,5 @@ name: Required API description: Provides an unified API to managed required property on fields. type: module -core_version_requirement: ^9.1 || ^10 || ^11 +core_version_requirement: ^10 || ^11 +php: 8.1 diff --git a/required_api.module b/required_api.module index 49b4e1137879f7c38448e83aea48fb3c45632cda..4f70530c56c8630146508a26cc5da0a379a5aeaf 100644 --- a/required_api.module +++ b/required_api.module @@ -8,18 +8,18 @@ */ use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\field\FieldConfigInterface; +use Drupal\required_api\Plugin\Required\RequiredDefault; /** * Implements hook_help(). */ -function required_api_help($path, $arg) { - switch ($path) { +function required_api_help(string $route_name, RouteMatchInterface $route_match) { + switch ($route_name) { case 'admin/help#required_api': - $output = ''; - $output .= '<p>' . t('The Required API module provides an API to find out + return '<p>' . t('The Required API module provides an API to find out if a widget is required or not.') . '</p>'; - return $output; } } @@ -28,7 +28,7 @@ function required_api_help($path, $arg) { * * Applies to FORM_ID field_config_edit_form. */ -function required_api_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) { +function required_api_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state): void { $field_definition = $form_state->getFormObject()->getEntity(); // Load unchanged field definition because @@ -105,11 +105,12 @@ function required_api_element_ajax_callback($form, FormStateInterface $form_stat function required_api_field_config_presave(FieldConfigInterface $entity): void { $plugin = $entity->getThirdPartySetting('required_api', 'required_plugin'); - if ($plugin === 'default') { + if ($plugin === RequiredDefault::ID) { // The default (core) plugin saves the required setting back to the entity. $plugin_setting = $entity->getThirdPartySetting('required_api', 'required_plugin_options', FALSE); $entity->setRequired($plugin_setting); - } elseif (!empty($plugin)) { + } + elseif (!empty($plugin)) { // Other plugins are considered required by default. $entity->setRequired(TRUE); } diff --git a/src/Annotation/Required.php b/src/Annotation/Required.php index 4e6c727f4a8d425672174596c10a781783bb2a43..994453908bbaa3479beda816726adffbf2ceeb50 100644 --- a/src/Annotation/Required.php +++ b/src/Annotation/Required.php @@ -3,9 +3,10 @@ namespace Drupal\required_api\Annotation; use Drupal\Component\Annotation\Plugin; +use Drupal\Core\Annotation\Translation; /** - * Defines an field required api annotation object. + * The Required annotation. * * @see hook_required_api_info_alter() * @@ -15,27 +16,21 @@ class Required extends Plugin { /** * The plugin ID. - * - * @var string */ - public $id; + public string $id; /** - * The human-readable name of the api. - * - * @var \Drupal\Core\Annotation\Translation + * The administrative label of the plugin. * * @ingroup plugin_translatable */ - public $label; + public Translation $label; /** - * A brief description of the api. - * - * @var \Drupal\Core\Annotation\Translation + * The description of the plugin. * * @ingroup plugin_translatable */ - public $description = ''; + public ?Translation $description = NULL; } diff --git a/src/Attribute/Required.php b/src/Attribute/Required.php new file mode 100644 index 0000000000000000000000000000000000000000..09b96bc7e26d65bf676236c3ff06d43551e7f4e8 --- /dev/null +++ b/src/Attribute/Required.php @@ -0,0 +1,30 @@ +<?php + +namespace Drupal\required_api\Attribute; + +use Drupal\Component\Plugin\Attribute\Plugin; +use Drupal\Core\StringTranslation\TranslatableMarkup; + +/** + * The Required attribute. + */ +#[\Attribute(\Attribute::TARGET_CLASS)] +class Required extends Plugin { + + /** + * Constructs a Block attribute. + * + * @param string $id + * The plugin ID. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label + * The administrative label of the plugin. + * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description + * (optional) The description of the plugin. + */ + public function __construct( + public readonly string $id, + public readonly TranslatableMarkup $label, + public readonly ?TranslatableMarkup $description = NULL, + ) {} + +} diff --git a/src/Form/RequiredDefaultPluginForm.php b/src/Form/RequiredDefaultPluginForm.php index 483d48add7e4475944c0b6ef9582b50c25da0f4e..47174012c2ae65c229414d6688e2648150698d81 100644 --- a/src/Form/RequiredDefaultPluginForm.php +++ b/src/Form/RequiredDefaultPluginForm.php @@ -4,6 +4,7 @@ namespace Drupal\required_api\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\required_api\RequiredManager; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -13,10 +14,8 @@ class RequiredDefaultPluginForm extends ConfigFormBase { /** * The required plugin manager. - * - * @var \Drupal\required_api\RequiredManager */ - protected $requiredManager; + protected RequiredManager $requiredManager; /** * {@inheritdoc} diff --git a/src/Plugin/Required/RequiredBase.php b/src/Plugin/Required/RequiredBase.php index 0810cd5cb1549f6fd773267f839a8c2cbdbd80e1..fe0dbdfcccdc68591c82edd8f77d2e5b82bd35c1 100644 --- a/src/Plugin/Required/RequiredBase.php +++ b/src/Plugin/Required/RequiredBase.php @@ -13,13 +13,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ abstract class RequiredBase extends PluginBase implements RequiredPluginInterface { - /** - * The configuration entity's dependencies. - * - * @var array - */ - protected $dependencies = []; - /** * Return a form element to use in form_field_ui_field_instance_edit_form. * @@ -29,7 +22,7 @@ abstract class RequiredBase extends PluginBase implements RequiredPluginInterfac * @return array * Form element to configure the required property. */ - public function formElement(FieldDefinitionInterface $field) { + public function formElement(FieldDefinitionInterface $field): array { $element = $this->requiredFormElement($field); return $element + [ @@ -71,16 +64,6 @@ abstract class RequiredBase extends PluginBase implements RequiredPluginInterfac return []; } - /** - * Calculates dependencies for the configured plugin. - * - * @see \Drupal\Core\Config\Entity\ConfigDependencyManager - * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName() - */ - public function calculateDependencies() { - return $this->dependencies; - } - /** * {@inheritdoc} */ diff --git a/src/Plugin/Required/RequiredDefault.php b/src/Plugin/Required/RequiredDefault.php index 73d35cfbdfdb0984e16f75b46273d0afbf1d6a8a..400b0b723430cdc642fd88a18073f041cf755d1c 100644 --- a/src/Plugin/Required/RequiredDefault.php +++ b/src/Plugin/Required/RequiredDefault.php @@ -4,39 +4,38 @@ namespace Drupal\required_api\Plugin\Required; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\required_api\Attribute\Required; /** * Default required plugin using core implementation. - * - * @Required( - * id = "default", - * label = @Translation("Core"), - * description = @Translation("Required based on core implementation.") - * ) */ +#[Required( + id: self::ID, + label: new TranslatableMarkup('Core'), + description: new TranslatableMarkup('Required based on core implementation.'), +)] class RequiredDefault extends RequiredBase { + public const ID = 'default'; + /** * {@inheritdoc} */ - public function isRequired(FieldDefinitionInterface $field, ContentEntityInterface $entity) { + public function isRequired(FieldDefinitionInterface $field, ContentEntityInterface $entity): bool { return $field->isRequired(); } /** * {@inheritdoc} */ - public function requiredFormElement(FieldDefinitionInterface $field) { - - $element = [ + public function requiredFormElement(FieldDefinitionInterface $field): array { + return [ '#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => $field->isRequired(), '#weight' => -5, ]; - - return $element; } } diff --git a/src/Plugin/RequiredPluginInterface.php b/src/Plugin/RequiredPluginInterface.php index eabf19b0610290ac33aecc631d6edadee3367331..77362efd4594a18bd8399d88ed68694bb3778eaf 100644 --- a/src/Plugin/RequiredPluginInterface.php +++ b/src/Plugin/RequiredPluginInterface.php @@ -8,7 +8,6 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Session\AccountInterface; /** * Defines the interface for image effects. @@ -26,7 +25,7 @@ interface RequiredPluginInterface extends PluginInspectionInterface, Configurabl * @return bool * TRUE on required. FALSE otherwise. */ - public function isRequired(FieldDefinitionInterface $field, ContentEntityInterface $entity); + public function isRequired(FieldDefinitionInterface $field, ContentEntityInterface $entity): bool; /** * Return a form element to use in form_field_ui_field_instance_edit_form. @@ -37,7 +36,7 @@ interface RequiredPluginInterface extends PluginInspectionInterface, Configurabl * @return array * Form element to configure the required property. */ - public function requiredFormElement(FieldDefinitionInterface $field); + public function requiredFormElement(FieldDefinitionInterface $field): array; /** * Optional submit handler for the field config form. diff --git a/src/RequiredApiFormErrorHandler.php b/src/RequiredApiFormErrorHandler.php index 61f93c08bd0055c52c97f82dd9fbc5ca8b3c78cb..544c48d871dc35047900366e45dc0952ef48fdd9 100644 --- a/src/RequiredApiFormErrorHandler.php +++ b/src/RequiredApiFormErrorHandler.php @@ -14,34 +14,18 @@ use Drupal\Core\StringTranslation\TranslatableMarkup; */ class RequiredApiFormErrorHandler implements FormErrorHandlerInterface { - /** - * The form error handler. - * - * @var \Drupal\Core\Form\FormErrorHandlerInterface - */ - protected FormErrorHandlerInterface $formErrorHandler; - - /** - * The required manager. - * - * @var \Drupal\required_api\RequiredManager - */ - protected RequiredManager $requiredManager; - /** * Constructs a RequiredApiFormErrorHandler object. * - * @param \Drupal\Core\Form\FormErrorHandlerInterface $inner + * @param \Drupal\Core\Form\FormErrorHandlerInterface $formErrorHandler * The inner form error handler service. * @param \Drupal\required_api\RequiredManager $requiredManager * The required manager. */ public function __construct( - FormErrorHandlerInterface $formErrorHandler, - RequiredManager $requiredManager + protected FormErrorHandlerInterface $formErrorHandler, + protected RequiredManager $requiredManager, ) { - $this->formErrorHandler = $formErrorHandler; - $this->requiredManager = $requiredManager; } /** @@ -84,7 +68,8 @@ class RequiredApiFormErrorHandler implements FormErrorHandlerInterface { foreach ($errors as $name => $message) { if ($message instanceof TranslatableMarkup) { $rawMessage = $message->getUntranslatedString(); - } else { + } + else { $rawMessage = $message; } diff --git a/src/RequiredManager.php b/src/RequiredManager.php index eaeab031eb129a4932826972b2a6e8046bc64abc..23ac11c716a0c51875e7b6efd53bda899029fe29 100644 --- a/src/RequiredManager.php +++ b/src/RequiredManager.php @@ -7,19 +7,15 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Plugin\DefaultPluginManager; +use Drupal\required_api\Annotation\Required as RequiredAnnotation; +use Drupal\required_api\Attribute\Required; +use Drupal\required_api\Plugin\RequiredPluginInterface; /** * Manages required by role plugins. */ class RequiredManager extends DefaultPluginManager { - /** - * The config factory. - * - * @var \Drupal\Core\Config\ConfigFactoryInterface - */ - protected $configFactory; - /** * Constructs a new RequiredManager object. * @@ -37,17 +33,17 @@ class RequiredManager extends DefaultPluginManager { \Traversable $namespaces, CacheBackendInterface $cacheBackend, ModuleHandlerInterface $moduleHandler, - ConfigFactoryInterface $configFactory, + protected ConfigFactoryInterface $configFactory, ) { parent::__construct( 'Plugin/Required', $namespaces, $moduleHandler, - 'Drupal\required_api\Plugin\RequiredPluginInterface', - 'Drupal\required_api\Annotation\Required' + RequiredPluginInterface::class, + Required::class, + RequiredAnnotation::class, ); $this->setCacheBackend($cacheBackend, 'required_api_required_plugins'); - $this->configFactory = $configFactory; } /** @@ -64,20 +60,20 @@ class RequiredManager extends DefaultPluginManager { * @param \Drupal\Core\Field\FieldDefinitionInterface $field * A field instance. * - * @return string|NULL + * @return string|null * The plugin id. */ - public function getPluginId(FieldDefinitionInterface $field) { + public function getPluginId(FieldDefinitionInterface $field): ?string { return $field->getThirdPartySetting('required_api', 'required_plugin', $this->getDefaultPluginId()); } /** * Gets the default plugin_id for the system. * - * @return string|NULL + * @return string|null * The plugin id. */ - public function getDefaultPluginId() { + public function getDefaultPluginId(): ?string { return $this->configFactory ->get('required_api.plugins') ->get('default_plugin'); @@ -86,14 +82,14 @@ class RequiredManager extends DefaultPluginManager { /** * Provides the definition ids. */ - public function getDefinitionsIds() { + public function getDefinitionsIds(): array { return array_keys($this->getDefinitions()); } /** * Provides the definitions as options just to inject to a select element. */ - public function getDefinitionsAsOptions() { + public function getDefinitionsAsOptions(): array { $definitions = $this->getDefinitions(); return array_map(function ($definition) { diff --git a/src/TypedData/RequiredApiTypedDataManager.php b/src/TypedData/RequiredApiTypedDataManager.php index 3be6e67cd12fa0febda41e002696960a4aff57a0..c64865bb2f1a558036057c80e6354f719658ad7c 100644 --- a/src/TypedData/RequiredApiTypedDataManager.php +++ b/src/TypedData/RequiredApiTypedDataManager.php @@ -3,9 +3,10 @@ namespace Drupal\required_api\TypedData; use Drupal\Core\TypedData\TypedDataManager; -use Drupal\Core\Validation\ExecutionContextFactory; +use Drupal\Core\TypedData\Validation\ExecutionContextFactory as OldExecutionContextFactory; use Drupal\Core\Validation\ConstraintValidatorFactory; use Drupal\Core\Validation\DrupalTranslator; +use Drupal\Core\Validation\ExecutionContextFactory; /** * Provides a typed data manager that returns our overridden RecursiveValidator. @@ -19,11 +20,12 @@ class RequiredApiTypedDataManager extends TypedDataManager { if (!isset($this->validator)) { $translator = new DrupalTranslator(); - // TODO: Remove this if statement once the minimum supported Drupal version is 10.3.0. + // @todo Remove this if statement once the minimum supported Drupal version is 10.3.0. // @see https://www.drupal.org/node/3396238 - if (class_exists(\Drupal\Core\TypedData\Validation\ExecutionContextFactory::class)) { - $executionContextFactory = new \Drupal\Core\TypedData\Validation\ExecutionContextFactory($translator); - } else { + if (class_exists(OldExecutionContextFactory::class)) { + $executionContextFactory = new OldExecutionContextFactory($translator); + } + else { $executionContextFactory = new ExecutionContextFactory($translator); } diff --git a/tests/modules/required_api_test/required_api_test.info.yml b/tests/modules/required_api_test/required_api_test.info.yml deleted file mode 100644 index 9274f3047d0c2f4185df40e7e07e08272d00cdaa..0000000000000000000000000000000000000000 --- a/tests/modules/required_api_test/required_api_test.info.yml +++ /dev/null @@ -1,10 +0,0 @@ -name: "Required API tests" -type: module -description: "Support module for Required API testing." -package: Testing -core_version_requirement: ^9.1 || ^10 || ^11 -hidden: true - -dependencies: - - drupal:field_ui - - required_api:required_api diff --git a/tests/modules/required_api_test/required_api_test.module b/tests/modules/required_api_test/required_api_test.module deleted file mode 100644 index cb146551b4e2bc8ca053c66223e111e8eff0ffca..0000000000000000000000000000000000000000 --- a/tests/modules/required_api_test/required_api_test.module +++ /dev/null @@ -1,6 +0,0 @@ -<?php - -/** - * @file - * Required API test module. - */ diff --git a/tests/src/Functional/RequiredApiTest.php b/tests/src/Functional/RequiredApiTest.php index 2a4d3d0fe7192d0dde042e2e8b45f81ca67ebaff..91ff6da47d8dfa08d95a2d2125a3314b2d670115 100644 --- a/tests/src/Functional/RequiredApiTest.php +++ b/tests/src/Functional/RequiredApiTest.php @@ -3,9 +3,14 @@ namespace Drupal\Tests\required_api\Functional; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\field\Entity\FieldConfig; +use Drupal\required_api\Plugin\Required\RequiredDefault; +use Drupal\required_api\RequiredManager; /** * Tests the functionality of the 'Manage fields' screen. + * + * @group required_api */ class RequiredApiTest extends RequiredApiTestBase { @@ -18,52 +23,33 @@ class RequiredApiTest extends RequiredApiTestBase { /** * The field name. - * - * @var string */ - protected $fieldName; + protected string $fieldName; /** * The field instance. - * - * @var \Drupal\field\Entity\FieldConfig */ - protected $instance; + protected FieldConfig $instance; /** * The required manager. - * - * @var \Drupal\required_api\RequiredManager */ - protected $manager; + protected RequiredManager $manager; /** * The admin path. - * - * @var string */ - protected $adminPath; + protected string $adminPath; /** * The node type. - * - * @var string */ - protected $type; + protected string $type; /** * The node type label. - * - * @var string - */ - protected $typeLabel; - - /** - * The node type label. - * - * @var string */ - protected $container; + protected string $typeLabel; /** * {@inheritdoc} @@ -105,10 +91,10 @@ class RequiredApiTest extends RequiredApiTestBase { /** * Tests that default value is correctly validated and saved. */ - public function testExpectedPluginDefinitions() { + public function testExpectedPluginDefinitions(): void { $expected_definitions = [ // Core behavior plugin replacement. - 'default', + RequiredDefault::ID, // Testing plugins. 'required_true', ]; @@ -120,9 +106,9 @@ class RequiredApiTest extends RequiredApiTestBase { /** * Tests the default Required Plugin. */ - public function testRequiredDefaultPlugin() { + public function testRequiredDefaultPlugin(): void { // Setting default (FALSE) and checking the form. - $this->setRequiredPlugin('default', FALSE); + $this->setRequiredPlugin(RequiredDefault::ID, FALSE); $add_path = 'node/add/' . $this->type; $this->drupalGet($add_path); @@ -146,9 +132,9 @@ class RequiredApiTest extends RequiredApiTestBase { /** * Tests that default value is correctly validated and saved. */ - public function testRequiredTestTruePlugin() { + public function testRequiredTestTruePlugin(): void { // Setting true and checking the form. - $this->setRequiredPlugin('required_true', 1); + $this->setRequiredPlugin('required_true', TRUE); $this->drupalGet('node/add/' . $this->type); $edit = [ @@ -167,10 +153,10 @@ class RequiredApiTest extends RequiredApiTestBase { * @param bool $plugin_value * TRUE if the plugin is required. */ - public function setRequiredPlugin($plugin_id, $plugin_value) { - $fieldname = "required_api[third_party_settings][required_plugin]"; + public function setRequiredPlugin(string $plugin_id, bool $plugin_value): void { + $fieldName = "required_api[third_party_settings][required_plugin]"; $edit = [ - $fieldname => $plugin_id, + $fieldName => $plugin_id, 'instance[required]' => $plugin_value, ]; diff --git a/tests/src/Functional/RequiredApiTestBase.php b/tests/src/Functional/RequiredApiTestBase.php index 8a8765faae8062fcf045e88d62762950aef638c9..03a03c65b89aa5f539875712ca31c407f8201ee0 100644 --- a/tests/src/Functional/RequiredApiTestBase.php +++ b/tests/src/Functional/RequiredApiTestBase.php @@ -19,7 +19,6 @@ abstract class RequiredApiTestBase extends BrowserTestBase { 'field_ui', 'field_test', 'required_api', - 'required_api_test', ]; /** diff --git a/tests/src/Unit/RequiredManagerTest.php b/tests/src/Unit/RequiredManagerTest.php index 15b961ca3bc8bc1e586438a74ee648dd8da308da..01450810bd42e70abc2df7f2040ac64bb0537f64 100644 --- a/tests/src/Unit/RequiredManagerTest.php +++ b/tests/src/Unit/RequiredManagerTest.php @@ -8,17 +8,14 @@ use Drupal\required_api\RequiredManager; /** * Tests the breadcrumb manager. * - * @group Drupal - * @group Required API + * @group required_api */ class RequiredManagerTest extends UnitTestCase { /** * The tested required manager. - * - * @var \Drupal\required_api\RequiredManager */ - protected $requiredManager; + protected RequiredManager $requiredManager; /** * {@inheritdoc} @@ -34,28 +31,15 @@ class RequiredManagerTest extends UnitTestCase { $this->requiredManager = new RequiredManager($namespaces, $cache_backend, $module_handler, $config_factory); } - /** - * {@inheritdoc} - */ - public static function getInfo() { - return [ - 'name' => 'Required manager', - 'description' => 'Tests the required manager.', - 'group' => 'Required API', - ]; - } - /** * Tests creating a Required Manager instance. */ - public function testCreateManagerInstance() { - + public function testCreateManagerInstance(): void { $is_object = is_object($this->requiredManager); $is_instance_of_required_manager = $this->requiredManager instanceof RequiredManager; $this->assertTrue($is_object, 'The requiredManager property is an object'); $this->assertTrue($is_instance_of_required_manager, 'The requiredManager is instance of RequiredManager'); - } } diff --git a/tests/src/Unit/RequiredTestTrue.php b/tests/src/Unit/RequiredTestTrue.php index 986ceee3dbc46fcefc3d94a0215c5765f9a53735..a257414cbfade4ba394e33492b572aa6ffc98ac7 100644 --- a/tests/src/Unit/RequiredTestTrue.php +++ b/tests/src/Unit/RequiredTestTrue.php @@ -4,40 +4,39 @@ namespace Drupal\Tests\required_api\Unit; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\required_api\Attribute\Required; use Drupal\required_api\Plugin\Required\RequiredBase; /** * Test plugin where the required value is always TRUE. - * - * @Required( - * id = "required_true", - * label = @Translation("Required TRUE"), - * description = @Translation("Required TRUE for testing.") - * ) */ +#[Required( + id: self::ID, + label: new TranslatableMarkup('Required TRUE'), + description: new TranslatableMarkup('Required TRUE for testing.'), +)] class RequiredTestTrue extends RequiredBase { + public const ID = 'required_true'; + /** * {@inheritdoc} */ - public function isRequired(FieldDefinitionInterface $field, ContentEntityInterface $entity) { + public function isRequired(FieldDefinitionInterface $field, ContentEntityInterface $entity): bool { return TRUE; } /** * {@inheritdoc} */ - public function requiredFormElement(FieldDefinitionInterface $field) { - - $element = [ + public function requiredFormElement(FieldDefinitionInterface $field): array { + return [ '#type' => 'checkbox', '#title' => $this->t('Required field'), '#default_value' => TRUE, '#weight' => -5, ]; - - return $element; } }