0

I have installed the Poll module in Drupal 8. I need to add a field to it, such as a text area for the poll description.

How can I add a field to a content entity like Poll, which is not a content type?

1

Poll is not a fieldable entity, so this is not possible in ui.

You can do this in code:

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
 * Implements hook_entity_base_field_info().
 */
function mymodule_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'poll') {
    $fields['description'] = BaseFieldDefinition::create('text')
      ->setLabel(t('New discription field'))
      ->setTranslatable(TRUE);
    return $fields;
  }
}

Apply the schema update on the database:

drush entity-updates
  • Thanks for your response. I am having an error implementing it: Notice: Undefined index: description__value in Drupal\Core\Entity\Sql\SqlContentEntityStorage->loadFromSharedTables() (line 558 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php). – Alexei Rayu Jul 15 '16 at 18:30
  • Yeah it worked. Cleared cache and updated the entity type. There are problems with the display though - but thanks for pointing me into the right direction. – Alexei Rayu Jul 15 '16 at 18:48
  • Ah, I must have totally dreamed making the field UI for poll accessible at /admin/config/content/poll then :) But yes, you an add fields, but they are by default not visible since viewing a poll is actually a form and doesn't use the standard entity render process. You can however alter the form and add things yourself. – Berdir Jul 15 '16 at 20:36
  • I am glad that there is an UI. I in fact spent lots of time trying to find a work-around and also taking other people's time. If that UI followed the conventions and were easily discoverable, would be even better :( – Alexei Rayu Jul 16 '16 at 7:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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