0

I want to add some fields (programmatically from my module) to the content_type "Article".

Is that possible ?

1
  • 2
    why do you want to add fields to article type programmatically when you can easily do it using Field UI by navigating Admin => Structure => Content Types => Articles => Manage Fields?
    – Yogesh
    Feb 29, 2016 at 13:03

1 Answer 1

0

Use hook_entity_bundle_field_info()

Copying from the api docs

function mymodule_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  // Add a property only to nodes of the 'article' bundle.
  if ($entity_type->id() == 'node' && $bundle == 'article') {
    $fields = array();
    $fields['mymodule_text_more'] = BaseFieldDefinition::create('string')
      ->setLabel(t('More text'))
      ->setComputed(TRUE)
      ->setClass('\Drupal\mymodule\EntityComputedMoreText');
    return $fields;
  }
}
1
  • 1
    Should be noted this answer is for Drupal 8. OP doesn't say what version is being used or why the admin UI can't be used.
    – Kevin
    Feb 29, 2016 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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