0

What I want to do is give a possibility to the user to save as many images as he likes. For this, I have created a specific field type

    $fields['photo_ids'] = BaseFieldDefinition::create('map')
  ->setLabel(t('Photos'))
  ->setDescription(t('The photo ids of the Item entity.'))
  ->setSettings([
    'max_length' => 50,
    'text_processing' => 0,
  ])
  ->setDefaultValue('');

here I want to save, add, delete IDs of pictures. Not sure if this is the best approach to this problem. The problem is I can't find the way in Drupal 8 to serialize/deserialize and save these data into the database;

7
  • Sounds like a multi-value field?
    – Kevin
    Commented Nov 26, 2018 at 14:57
  • yes I plan to store all picture ids in this field
    – serj
    Commented Nov 26, 2018 at 14:58
  • Why don't you use the built-in field api and ui? Check "setCardinality" for your BaseFieldDefinition approach. Commented Nov 26, 2018 at 15:00
  • because I don't know about it :), could share an example or link of how to do it?
    – serj
    Commented Nov 26, 2018 at 15:03
  • There are plenty on youtube. Search for something like "content type field create drupal 8". Commented Nov 26, 2018 at 15:04

1 Answer 1

2

You should not create your content type programmatically, if there's no special reason. Take a look at any intro like this https://www.youtube.com/watch?v=Nfu6nckjuno - then create a content type and fields with the excellent admin UI.

Serialization to DB is done by the Drupal core.

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.