1

Using the Drupal CLI I've generated a D8 module. Now I want to generate a content type with additional fields, for which I can define my own CRUD, forms and other logic. I used drupal generate:entity:bundle which generates the new content type and makes it possible to add new fields through the admin interface.

I don't want to add fields through the UI; I want to add fields through code, so I can add new logic in the (CRUD) controller. This will be adding fields, defining new views, adding validation etc. I tried to use drupal generate:entity:content, but it doesn't seem to get me anywhere.

How could I do this?

  • This quenstion for drupalconsole.com guys. Maybe ask in git repo issues? – user21641 Feb 15 '16 at 7:56
1

The main purpose of content types/entity bundles is to allow users to configure/create subtypes/bundles of your entity type and add configurable fields to them.

If you want to have two different things, just create two different entity types. Each will have its own table then and its own set of fields. Completely separated.

If you want to have a single entity type/table with different sets of additional fields, then you can for example use entity bundles and just don't expose a UI for them, buy removing all forms, routes and the field_ui_base_route. Then the only way to add per-bundle fields would be through the API/Code.

Or you implement it all yourself using bundleFieldDefinitions(), hook_entity_field_storage_info() and so on. But that would require quite a bit of code as you need to take care of installing the field storage definitions and so on.

0

I agree with eveything Berdir has said. I'm just adding an additional answer to show an example of how to add additional properties using baseFieldDefinitions().


I't important to understand the difference between entity properties and fields. Properties are just extra (database) fields in the entity base table. The advantages of this is you reduce the number of database tables and speed up queries retrieving data. This disadvantages of this is you cannot have multi cardinality fields or attach the same field to multiple bundles or entity types. Choosing whether to use a property of a field will depend on your specific requirements and the data you are trying to store.


If you decide that you want to add additional properties to your custom entity then you can follow these instructions.

After drupal generate:entity:bundle has finished running it will have created a new class for your custom entity. You can find this at src/Entity/MyEntity.php.

In this class there is a method called baseFieldDefinitions(). You can add additional field defintions in this method that will automatically get created when your module is enabled.

I've found it difficult to find any really good documentation on exactly what configuration options are available for each field type but I've found this page that has quite a good description of of the different field types that can be added and their properties. Looking at what the core modules do is also a good way to learn.

Here are a couple of examples I've used in my own modules:

String

$fields['type'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Type'))
  ->setSettings([
    'max_length' => 50,
    'text_processing' => 0,
  ])
  ->setDefaultValue('')
  ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'string',
    'weight' => 1,
  ])
  ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 1,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

Entity reference

fields['campus'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Campus'))
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default:taxonomy_term')
  ->setSetting('handler_settings', [
      'target_bundles' => [
        'campus' => 'campus'
      ]
  ])
  ->setDisplayOptions('view', [
    'label' => 'above',
    'weight' => 2,
  ])
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'weight' => 2,
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => '20',
      'placeholder' => '',
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

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.