1

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...

Only text field and no custom field

Manage form display

  • You have enabled the field_example module, right? – Clive Jul 21 '16 at 13:51
  • Yes, if i add it over the admin GUI to a new node type, it works. But only when I define it in my custome entity code (with bundles), I have this problem. I can create a new type of my custome entity, but when I switch to "Manage form display", I get this error. – ursk83 Jul 21 '16 at 13:56
  • In the module that defines the above entity, did you add a dependency to the field example module? – Eyal Jul 21 '16 at 15:37
0

I'm not certain, but I think the reason why you are not given the option for the rgb widget on your field, is that your field is not an rgb field. I think you're creating a text field and calling it rgb.

You need to create a field of type "rgb". I suspect you want BaseFieldDefinition::create('rgb'), but have a look at the docs or a different example. You may well also need a "use" statement that refers to rgb.

  • Now I have change from BaseFieldDefinition::create('string') to BaseFieldDefinition::create('field_example_rgb') and it seems to work. Thanks! – ursk83 Jul 25 '16 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.