0

I'm building a module that creates a content type when it installs. I already have my content type, let's call it MyContentType and I have a field body that I created based on Drupal documentation (https://www.drupal.org/docs/8/api/entity-api/programming-custom-fields-into-your-content-type) and I could create the two additional fields but I can't find a way to add a field of type date, datetime or timestamp. I tried to replace 'type: text_default' to 'type: datetime' but when I did it all my fields disappeared! I know I should also have a file named field.field.node.mycontenttype.field_date.yml with the correct configuration but what is the way to tell drupal that the type is datetime ? And how can I set it to current time?

field.storage.node.field_date.yml

langcode: en
status: true
dependencies:
  module:
    - node
    - text
id: node.field_date
field_name: field_date
entity_type: node
type: datetime
settings: {  }
module: datetime
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

field.field.node.mycontenttype.field_brand_information.yml

langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_date
    - node.type.mycontenttype
  module:
    - text
id: node.mycontenttype.field_date
field_name: field_date
entity_type: node
bundle: mycontenttype
label: 'Date'
description: 'Date'
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  display_summary: false
field_type: datetime

And then I referenced it like:

core.entity_form_display.node.mycontenttype.default.yml

langcode: en
status: true
dependencies:
  config:
    - field.field.node.mycontenttype.field_date
    - node.type.mycontenttype
  module:
    - file
    - path
    - text
_core:
  default_config_hash: qZE-qJ04DTTNggVVQdVOPQmpE_I69GQ_LqB32kXivVg
id: node.mycontenttype.default
targetEntityType: node
bundle: mycontenttype
mode: default
content:
  created:
    type: datetime_timestamp
    weight: 2
    settings: {  }
    third_party_settings: {  }
    (...)

and

core.entity_view_display.node.mycontenttype.default.yml

langcode: en
status: true
dependencies:
  config:
    - field.field.node.mycontenttype.field_date
    - node.type.mycontenttype
  module:
    - file
    - text
    - user
_core:
  default_config_hash: Nfnv6VMugBKl6EOqi_U0I_LQ1ZQpbNDN3a9GXHWUBz4
id: node.mycontenttype.default
targetEntityType: node
bundle: mycontenttype
mode: default
content:
  field_date:
    weight: 101
    label: above
    settings: {  }
    third_party_settings: {  }
    type: datetime

Thank you.

0

Like 4k4 suggested, I used https://www.drupal.org/docs/8/api/entity-api/using-the-ui-to-export-the-code-for-your-custom-fields to create and export all my needed fields and it worked really well.

Note: With a lot of changes you may need to delete config for the already created settings. Remove it with: drush config-delete field.storage.node.field_name Found that here

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.