How can I use a custom field with a custom entity?
Based on Drupal examples, how I can use field_example_rgb
(from field_example) in my own custom content entity with bundles?
I will add this field to the main entity table.
I tried the following.
$fields['rgb'] = BaseFieldDefinition::create('string')
->setLabel(t('RGB'))
->setDescription(t('RGB Color'))
->setSettings(array(
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'field_example_rgb',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
I can install the module without errors, but when I create an new type and go to "Manage form display" I get the following error:
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "field_example_rgb" plugin does not exist. in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 52 of core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).
I don't know what was the problem, after make the module completely new I get no error.
But I get a normal text field and not the "field_example_rgb
".
I try to explain more in detail what I have made.
Create new module
drupal@server:~/public_html$ drupal generate:module
Enter the new module name:
> customfield
Enter the module machine name [customfield]:
>
Enter the module Path [/modules/custom]:
> /modules/testing
Enter module description [My Awesome Module]:
> Testing custom field
Enter package name [Custom]:
> Testing
Enter Drupal Core version [8.x]:
>
Do you want to generate a .module file (yes/no) [yes]:
>
Define module as feature (yes/no) [no]:
>
Do you want to add a composer.json file to your module (yes/no) [yes]:
>
Would you like to add module dependencies (yes/no) [no]:
> field_example_rgb
Do you confirm generation? (yes/no) [yes]:
>
Generated or updated files
Site path: /home/drupal/public_html
1 - modules/testing/customfield/customfield.info.yml
2 - modules/testing/customfield/customfield.module
3 - modules/testing/customfield/composer.json
drupal@server:~/public_html$ drupal generate:module
Create a custom content entity with bundles:
drupal@server:~/public_html$ drupal generate:entity:content
Enter the module name [customfield]:
>
Enter the class of your new content entity [DefaultEntity]:
> Test
Enter the machine name of your new content entity [test]:
>
Enter the label of your new content entity [Test]:
>
Enter the base-path for the content entity routes [/admin/structure]:
>
Do you want this (content) entity to have bundles (yes/no) [no]:
> yes
Is your entity translatable (yes/no) [yes]:
> no
Generated or updated files
Site path: /home/drupal/public_html
1 - modules/testing/customfield/customfield.permissions.yml
2 - modules/testing/customfield/customfield.links.menu.yml
3 - modules/testing/customfield/customfield.links.task.yml
4 - modules/testing/customfield/customfield.links.action.yml
5 - modules/testing/customfield/src/TestAccessControlHandler.php
6 - modules/testing/customfield/src/Entity/TestInterface.php
7 - modules/testing/customfield/src/Entity/Test.php
...
18 - modules/testing/customfield/customfield.module
// generate:entity:config
Enter the base-path for the config entity routes [/admin/structure]:
>
drupal@server:~/public_html$
Add only the following code to modules/testing/customfield/src/Entity/Test.php
:
$fields['rgb'] = BaseFieldDefinition::create('string')
->setLabel(t('RGB'))
->setDescription(t('RGB'))
->setSettings(array(
'max_length' => 50,
'text_processing' => 0,
))
->setDefaultValue('')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'field_example_rgb',
'weight' => -4,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
Install the module:
drupal@server:~/public_html$ drupal module:install customfield
Installing module(s) customfield
[OK] The following module(s) were installed successfully: customfield
// cache:rebuild
Rebuilding cache(s), wait a moment please.
[OK] Done clearing cache(s).
drupal@server:~/public_html$
Then I create a new type of Test an show for the RGB-Field, but i only get a textfield (see screenshot).
I'm on the wrong way? Is this not possible? I think it is ver uggly to gerate for every custom field a new table...