This question already has an answer here:

I'm adding field to my content type programmatically but for some reason they end up being disabled in the Manage form display and Manage display settings page.

I'm using a yml file to create the fieldstorage:

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

Then I use the following to add the field:

$fieldName = 'seo_page_title';
$label = t('Page title');
$field_type = 'string';
$description = 'Description';
$entityTypemanager = \Drupal::entityTypeManager();

$field = FieldConfig::loadByName('node', $bundle, $fieldName);
if (empty($field)) {
            'field_name'   => $fieldName,
            'entity_type'  => 'node',
            'bundle'       => $bundle,
            'label'        => $label,
            'required'     => TRUE,
            'translatable' => TRUE,
            'description'  => t($description),

    // Assign widget settings for the 'default' form mode.
    /** @var TYPE_NAME $entityTypemanager */
    $displayForm = $entityTypemanager
        ->load('node.' . $bundle . '.default')
        ->setComponent($fieldName, [
            'type'   => $field_type,
            'weight' => 100,


    // Assign display settings for the 'default' and 'teaser' view modes.
    /** @var TYPE_NAME $entityTypemanager */
    $displayDefault = $entityTypemanager
        ->load('node.' . $bundle . '.default')


    // The teaser view mode is created by the Standard profile and therefore
    // might not exist.
    $viewModes = \Drupal::service('entity_display.repository')
    if (isset($viewModes['teaser'])) {
        /** @var TYPE_NAME $entityTypemanager */
        $displayTeaser = $entityTypemanager
            ->load('node.' . $bundle . '.teaser')


What am I doing wrong? Thanks in advance.

marked as duplicate by 4k4 8 Jan 8 at 7:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What’s with the unsets? Curious. – Kevin Jan 8 at 3:43
  • @Kevin, the unset's are also in the answer trying to update the code in the duplicate. Joost, better use the original code from the node module and if you want, replace the deprecated functions one-by-one. – 4k4 Jan 8 at 7:22
  • This is what I tried but the fields keep turning up disabled. That's why I created a new thread. – Joost Jan 8 at 7:26

Browse other questions tagged or ask your own question.